【MySQL】DDL常见操作

DDL常见操作


DDL:Data De[ine Language数据定义语⾔,主要⽤来对数据库、表进⾏⼀些管理操作。 如:建库、删库、建表、修改表、删除表、对列的增删改等等。 ⽂中涉及到的语法⽤[]包含的内容属于可选项。


一、库的管理

1.创建库
create database [if not exists] 库名; 
2.删除库
drop databases [if exists] 库名; 
3.建库通⽤的写法
drop database if exists 旧库名; create database 新库名;

示例:

sql> show databases like 'user_%';
+-------------------+
| Database (user_%) |
+-------------------+
| user_db           |
+-------------------+
1 row in set (0.00 sec)
sql> drop database if exists java;
Query OK, 0 rows affected (0.10 sec)

sql> create database java;
Query OK, 1 row affected (0.01 sec)

sql> show databases like 'java';
+-----------------+
| Database (java) |
+-----------------+
| java            |
+-----------------+
1 row in set (0.00 sec)

返回顶部


二、表管理

1.创建表
create table 表名( 		

    字段名1 类型[(宽度)] [约束条件] [comment '字段说明'], 			

    字段名2 类型[(宽度)] [约束条件] [comment '字段说明'], 		

	字段名3 类型[(宽度)] [约束条件] [comment '字段说明'] 

)[表的⼀些设置];

注意:

  1. 在同⼀张表中,字段名不能相同
  2. 宽度和约束条件为可选参数,字段名和类型是必须的
  3. 最后⼀个字段后不能加逗号
  4. 类型是⽤来限制 字段 必须以何种数据类型来存储记录
  5. 类型其实也是对字段的约束(约束字段下的记录必须为XX类型)
  6. 类型后写的 约束条件 是在类型之外的 额外添加的约束

返回顶部


约束说明
  • not null:标识该字段不能为空

    sql> create table test1(a int not null comment '字段a');
    Query OK, 0 rows affected (0.01 sec)
    
    sql> insert into test1 values (null);
    ERROR 1048 (23000): Column 'a' cannot be null
    
    sql> insert into test1 values (1);
    Query OK, 1 row affected (0.00 sec)
    
    sql> select * from test1;
    +---+
    | a |
    +---+
    | 1 |
    +---+
    1 row in set (0.00 sec)
    

返回顶部


  • default value:为该字段设置默认值,默认值为value
sql> drop table IF EXISTS test2;
Query OK, 0 rows affected (0.01 sec)

sql> create table test2(
-> a int not null comment '字段a',
-> b int not null default 0 comment '字段b'
-> );
Query OK, 0 rows affected (0.02 sec)

sql> insert into test2(a) values (1);
Query OK, 1 row affected (0.00 sec)

sql> select *from test2;
+---+---+
| a | b |
+---+---+
| 1 | 0 |
+---+---+
1 row in set (0.00 sec)
    ```

**上⾯插⼊时未设置b的值,⾃动取默认值0~**	

******

* **primary	key:标识该字段为该表的主键,可以唯⼀的标识记录,插⼊重复的会报错**

**⽅式1:跟在列后,如下:**

```sql
sql> drop table IF EXISTS test3;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test3(
   -> a int not null comment '字段a' primary key 
   -> );
Query OK, 0 rows affected (0.01 sec)

sql> insert into test3 (a) values (1);
Query OK, 1 row affected (0.01 sec)

sql> insert into test3 (a) values (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

⽅式2:在所有列定义之后定义,如下:

sql> drop table IF EXISTS test4;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test4(
 -> a int not null comment '字段a',
 -> b int not null default 0 comment '字段b',
 -> primary key(a)
 -> );
Query OK, 0 rows affected (0.02 sec)

sql> insert into test4(a,b) values (1,1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test4(a,b) values (1,2);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

插⼊重复的值,会报违法主键约束

⽅式2⽀持多字段作为主键,多个之间⽤逗号隔开,语法:primary key(字段1,字段2,字段n),⽰例:

sql> drop table IF EXISTS test7;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test7(
 -> a int not null comment '字段a',
 -> b int not null comment '字段b',
 -> PRIMARY KEY (a,b) 
 -> );
Query OK, 0 rows affected (0.02 sec)

sql> insert into test7(a,b) VALUES (1,1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test7(a,b) VALUES (1,1);
ERROR 1062 (23000): Duplicate entry '1-1' for key 'PRIMARY'

返回顶部


  • foreign key:为表中的字段设置外键
    • 语法:foreign key(当前表的列名) references 引⽤的外键表(外键表中字段名称)
sql> drop table IF EXISTS test6;
Query OK, 0 rows affected (0.01 sec)

sql> drop table IF EXISTS test5;
Query OK, 0 rows affected (0.01 sec)

sql> create table test5(
 -> a int not null comment '字段a' primary key
 -> );
Query OK, 0 rows affected (0.02 sec)

sql> create table test6(
 -> b int not null comment '字段b',
 -> ts5_a int not null,
 -> foreign key(ts5_a) references test5(a)
 -> );
Query OK, 0 rows affected (0.01 sec)
 
sql> insert into test5 (a) values (1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test6 (b,test6.ts5_a) values (1,1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test6 (b,test6.ts5_a) values (2,2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key 
constraint fails (`javacode2018`.`test6`, CONSTRAINT `test6_ibfk_1` 
FOREIGN KEY (`ts5_a`) REFERENCES `test5` (`a`))

说明:表⽰test6中ts5_a字段的值来源于表test5中的字段a。

注意⼏点:

  • 两张表中需要建⽴外键关系的字段类型需要⼀致
  • 要设置外键的字段不能为主键
  • 被引⽤的字段需要为主键
  • 被插⼊的值在外键表必须存在,如上⾯向test6中插⼊ts5_a为2的时候报错了,原因:2的值在test5表中不存在

返回顶部


  • unique key(uq):标识该字段的值是唯⼀的

    • ⽀持⼀个到多个字段,插⼊重复的值会报违反唯⼀约束,会插⼊失败。

⽅式1:跟在字段后,如下:

sql> drop table IF EXISTS test8;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test8(
 -> a int not null comment '字段a' unique key
 -> );
Query OK, 0 rows affected (0.01 sec)

sql> insert into test8(a) VALUES (1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test8(a) VALUES (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'a'

⽅式2:所有列定义之后定义,如下:

sql> drop table IF EXISTS test9;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test9(
 -> a int not null comment '字段a',
 -> unique key(a)
 -> );
Query OK, 0 rows affected (0.01 sec)

sql> insert into test9(a) VALUES (1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test9(a) VALUES (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'a'

⽅式2⽀持多字段,多个之间⽤逗号隔开,语法:unique key(字段1,字段2,字段n),⽰例:

sql> drop table IF EXISTS test10;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test10(
 -> a int not null comment '字段a',
 -> b int not null comment '字段b',
 -> unique key(a,b)
 -> );
Query OK, 0 rows affected (0.01 sec)

sql> insert into test10(a,b) VALUES (1,1);
Query OK, 1 row affected (0.00 sec)

sql> insert into test10(a,b) VALUES (1,1);
ERROR 1062 (23000): Duplicate entry '1-1' for key 'a'

返回顶部


  • auto_increment:标识该字段的值⾃动增长(整数类型,⽽且为主键)
sql> drop table IF EXISTS test11;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test11(
 -> a int not null AUTO_INCREMENT PRIMARY KEY comment '字段a',
 -> b int not null comment '字段b'
 -> );
Query OK, 0 rows affected (0.01 sec)

sql> insert into test11(b) VALUES (10);
Query OK, 1 row affected (0.00 sec)

sql> insert into test11(b) VALUES (20);
Query OK, 1 row affected (0.00 sec)

sql> select * from test11;
+---+----+
| a | b |
+---+----+
| 1 | 10 |
| 2 | 20 |
+---+----+
2 rows in set (0.00 sec)

字段a为⾃动增长,默认值从1开始,每次+1
关于⾃动增长字段的初始值、步长可以在sql中进⾏设置,⽐如设置初始值为1万,每次增长10

注意:⾃增长列当前值存储在内存中,数据库每次重启之后,会查询当前表中⾃增列的最⼤值作为当前值,如果表数据被清空之后,数据库重启了,⾃增列的值将从初始值开始,我们来演⽰⼀下:

sql> delete from test11;
Query OK, 2 rows affected (0.00 sec)

sql> insert into test11(b) VALUES (10);
Query OK, 1 row affected (0.00 sec)

sql> select * from test11;
+---+----+
| a | b |
+---+----+
| 3 | 10 |
+---+----+
1 row in set (0.00 sec)

上⾯删除了test11数据,没有重启sql,然后紧接着插⼊了⼀条,a的值为3,结果明显数据库会查询当前表中⾃增列的最⼤值作为当前值

下面我们删除test11数据,重启sql,然后再插⼊数据,看a的值是不是被初始化了?如下:

sql> delete from test11;
Query OK, 1 row affected (0.00 sec)

sql> select * from test11;
Empty set (0.00 sec)

sql> exit

C:\Windows\system32>net stop sql
sql 服务正在停⽌..
sql 服务已成功停⽌。
C:\Windows\system32>net start sql
sql 服务正在启动 .sql 服务已经启动成功。
C:\Windows\system32>sql -uroot -p
Enter password: *******
Welcome to the sql monitor. Commands end with ; or \g.
Your sql connection id is 2
Server version: 5.7.25-log sql Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights 
reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input 
statement.

sql> use javacode2018;
Database changed

sql> select * from test11;
Empty set (0.01 sec)

sql> insert into test11 (b) value (100);
Query OK, 1 row affected (0.00 sec)

sql> select * from test11;
+---+-----+
| a | b |
+---+-----+
| 1 | 100 |
+---+-----+
1 row in set (0.00 sec)

返回顶部


2.删除表
drop table [if exists] 表名;
3.修改表名
 alter table 表名 rename [to] 新表名; 
4.表设置备注
alter table 表名 comment '备注信息'; 
5.复制表
只复制表结构
create table 表名 like 被复制的表名

image-20210418114958826

image-20210418115324374

复制表结构+数据
create table 表名 [as] select 字段,... from 被复制的表 [where 条件];

image-20210418115513352

表结构和数据都过来了。

返回顶部


三、表中列的管理

1.添加列
alter table 表名 add column 列名 类型 [列约束];
sql> drop table IF EXISTS test14;
Query OK, 0 rows affected, 1 warning (0.00 sec)

sql> create table test14(
 -> a int not null AUTO_INCREMENT PRIMARY KEY comment '字段a'
 -> );
Query OK, 0 rows affected (0.02 sec)

sql> alter table test14 add column b int not null default 0 comment '字段b';
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
 
sql> alter table test14 add column c int not null default 0 comment '字段c';
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

sql> insert into test14(b) values (10);
Query OK, 1 row affected (0.00 sec)

sql> select * from test14; 
+---+----+---+
| a | b | c |
+---+----+---+
| 1 | 10 | 0 |
+---+----+---+
1 row in set (0.00 sec)

返回顶部


2.修改列
alter table 表名 modify column 列名 新类型 [约束];
  或者
alter table 表名 change column 列名 新列名 新类型 [约束];

2种⽅式区别:modify不能修改列名,change可以修改列名

我们看⼀下test14的表结构:

sql> show create table test14;
+--------+--------+
| Table | Create Table |
+--------+--------+
| test14 | CREATE TABLE `test14` (
 `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
 `b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',
 `c` int(11) NOT NULL DEFAULT '0' COMMENT '字段c',
 PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+--------+--------+
1 row in set (0.00 sec)

我们将字段c名字及类型修改⼀下,如下:

sql> alter table test14 change column c d varchar(10) not null
default '' comment '字段d';
Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0

sql> show create table test14; 
+--------+--------+
| Table | Create Table |
+--------+--------+
| test14 | CREATE TABLE `test14` (
 `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
 `b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',
 `d` varchar(10) NOT NULL DEFAULT '' COMMENT '字段d',
 PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+--------+--------+
1 row in set (0.00 sec)

返回顶部


3.删除列
alter table 表名 drop column 列名;
sql> alter table test14 drop column d;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

sql> show create table test14;
+--------+--------+
| Table | Create Table |
+--------+--------+
| test14 | CREATE TABLE `test14` (
 `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '字段a',
 `b` int(11) NOT NULL DEFAULT '0' COMMENT '字段b',
 PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+--------+--------+
1 row in set (0.00 sec)

返回顶部


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着蜗牛ひ追导弹'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值