第二篇***DDL&&DML操作

一、DDL

DDL:data define language数据定义语言,主要用来对数据库,表进行一些管理操作。

eg:建库,删库,建表,修改表,删除表,对列的增删改查等。

1、库的管理

###用[]包含的内容属于可选项
1、创建库
create database [if not exists] 库名;

2、删除库
drop database [if exists] 库名;

3、建库通用写法
drop database if exists 旧库名;
create database 新库名;

4、例子
mysql> create database test;
Query OK, 1 row affected (0.00 sec)

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

mysql> drop database if exists test;
Query OK, 0 rows affected (0.11 sec)

mysql> show databases like 'test';
Empty set (0.00 sec)

mysql> create database demo;
Query OK, 1 row affected (0.33 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bookmanager        |
| comperformance     |
| covid19_beta       |
| db_dorm            |
| demo               |
| finedb             |
| kettle_demo        |
| mysql              |
+--------------------+
9 rows in set (0.00 sec)

mysql>

2、表的管理

1、创建表
create table 表名(
	字段名1 类型[(宽度)] [约束条件] [comment '字段说明'],
	字段名2 类型[(宽度)] [约束条件] [comment '字段说明'],
	字段名3 类型[(宽度)] [约束条件] [comment '字段说明']
)[表的一些设置];

注意:
@在同一张表中,字段名不能相同;
@宽度和约束条件为可选参数,字段名和类型是必须的;
@最后一个字段后不能加逗号;
@类型是用来限制字段的,必须以某种数据类型来存储记录;
@类型其实也是对字段的约束(约束字段下的记录必须为XX类型);
@类型后写的约束条件,是在类型之外,额外添加的约束;

关于约束:
【1】标识该字段不能为空;
not null 

eg:
mysql> create table test1(a int not null comment '字段a');
Query OK, 0 rows affected (0.25 sec)

mysql> insert into test1 values(null);
ERROR 1048 (23000): Column 'a' cannot be null
mysql> insert into test1 values(1);
Query OK, 1 row affected (0.37 sec)

mysql> select * from test1;
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

mysql>



【2】该字段设置默认值,默认值为value;
default value

eg:
mysql> drop table if exists test2;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

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

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

mysql>
上面插入值时未设置b的值,自动取默认值为0


【3】标识该字段为该表的主键,可以唯一的标识记录。若插入重复的会报错,因为违背主键约束;
primary key

eg:
方法1:跟在后面
mysql> drop table if exists test3;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

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

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

方法2:在所有列定义之后定义
mysql> drop table if exists test4;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 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.18 sec)

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

mysql> insert into test4(a,b) values(1,2);
ERROR 1062 (23000): Duplicate entry '1' for key 1
mysql>
##插入重复的值,会报违背主键约束
方法2支持多字段作为主键,多个之间用逗号隔开
eg:primary key(字段1,字段2,.....字段n),


【4】为表中的字段设置外键;
foreign key

eg:
mysql> drop table if exists test6;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> drop table if exists test5;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> 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.11 sec)

mysql> insert into test5(a) values(1);
Query OK, 1 row affected (0.12 sec)

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

mysql> 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 (`demo/test6`, CONSTRAINT `test6_ibfk_1` FOREIGN KEY (`ts5_a`) REFERENCES `test5` (`a`))
##说明test6中ts5_a字段的值来源于表test5中的字段a。
注意:
@两张表中需要建⽴外键关系的字段类型需要⼀致
@要设置外键的字段不能为主键
@被引⽤的字段需要为主键
@被插⼊的值在外键表必须存在,如上⾯向test6中插⼊ts5_a为2的时候报错了,原因:2的值在test5表中不存在


【5】标识该字段的值是唯一的;
unique key(uq)
⽀持⼀个到多个字段,插⼊重复的值会报违反唯⼀约束,插⼊会失败
定义方法有两种。

方法1:跟在字段后
mysql> drop table if exists test8;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> insert into test8(a) values(1);
Query OK, 1 row affected (0.11 sec)

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

方法2:所有列定义之后
mysql> drop table if exists test9;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> insert into test9(a) values(1);
Query OK, 1 row affected (0.12 sec)

mysql> insert into test9(a) values(1);
ERROR 1062 (23000): Duplicate entry '1' for key 1
mysql>
##方法2支持多字段,多个之间逗号隔开,语法:unique key(a,b,c,.......n)



【6】标识该字段的值自动增长(整数类型,而且为主键)
auto_increment

eg:
mysql> drop table if exists test11;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 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.45 sec)

mysql> insert into test11(b) values(10);
Query OK, 1 row affected (0.03 sec)

mysql> insert into test11(b) values(20);
Query OK, 1 row affected (0.12 sec)

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

mysql>
##字段a为自动增长,默认值从1开始,每次加1
##关于自动增长字段的初始值,步长可以在mysql中设置,比如设置初始值为1万,每次增长10
##自增长列当前值存储在内存中,数据库每次重启之后,会查询当前表中自增列的最大值作为当前值,如果表数据被清空之后,数据库重启了,自增列的值将从初始值开始。
mysql> delete from test11;
Query OK, 2 rows affected (0.14 sec)

mysql> insert into test11(b) values(10);
Query OK, 1 row affected (0.05 sec)

mysql> select *from test11;
+---+----+
| a | b  |
+---+----+
| 3 | 10 |
+---+----+
1 row in set (0.00 sec)
##上⾯删除了test11数据,然后插⼊了⼀条,a的值为3,执⾏下⾯操作:删除test11数据,重启mysql,插⼊数据,然后看a的值是不是被初始化了?
mysql> delete from test11;
Query OK, 2 rows affected (0.14 sec)

mysql> insert into test11(b) values(10);
Query OK, 1 row affected (0.05 sec)

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

mysql> delete from test11;
Query OK, 1 row affected (0.34 sec)

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

mysql> exit
Bye

C:\Windows\system32>net stop mysql
MySQL 服务正在停止..
MySQL 服务已成功停止。


C:\Windows\system32>net start mysql

MySQL 服务已经启动成功。


C:\Windows\system32>mysql -h localhost -P 3306 -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.24a-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use demo;
Database changed

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

mysql> 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、复制表
(1)只复制表结构
create table 表名 like 被复制的表名;

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

mysql> create table test13 as select* from test11;
Query OK, 1 row affected (0.22 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test13;
+---+-----+
| a | b   |
+---+-----+
| 1 | 100 |
+---+-----+
1 row in set (0.00 sec)
##表结构和数据都复制了!

3、表中列的管理

(1)添加列
alter table 表名 add column 列名 类型[列约束];


(2)修改列
alter table 表名 modeify column 列名 新列名 新类型 [约束];
或者
alter table 表名 change column 列名 新列名 新类型 [约束];
#区别:modeify不能修改列名,change可以修改列名


(3)删除列
alter table 表名 drop column 列名;


二、DML

DML(Data Manipulation Language)数据操作语言,以insert,update,delete三种指令为核心,分别代表插入,更新,删除。

DML和SQL中的select称为CRUD(增删改查)

1、插入操作

(1)插入单行操作
方式1: 
insert into 表名[(字段,字段)] values(值,值);
说明:
@如果是字符型或日期类型,值需要用单引号引起来;数值类型不需要用单引号
@字段和值的个数必须一致,位置对应
@字段如果不能为空,则必须插入值
@可以为空的字段可以不用插入值,但需要注意:字段和值都不写;或字段写上,值用null代替
@表名后面的字段可以省略不写,此时表示所有字段,顺序和表中字段顺序一致

方式2:
insert into 表名 set 字段=值,字段=值;
##不常见,一般不用这个

(2)批量插入
方式1:
insert into 表名[(字段,字段)] values (值,值),(值,值),(值,值);

方式2:
insert into 表[(字段,字段)]

注意:select语句有多种写法,需要注意的是select返回的结果和插入数据的字段数量,顺序,类型需要一致。

2、数据更新

1、单表更新
update 表名[[as] 别名] set [别名.]字段= 值,[别名.]字段= 值[where条件];

有些表名可能名称⽐较长,为了⽅便操作,可以给这个表名起个简单的别名,更⽅便操作⼀些。如果⽆别名的时候,表名就是别名。

2、多表更新
可以同时更新多个表中的数据
update 表1 [[as] 别名1],表名2 [[as] 别名2]
set [别名.]字段= 值,[别名.]字段= 值
[where条件]

建议采⽤单表⽅式更新,⽅便维护。

3、删除数据操作

1、使用delete删除

1@delete单表删除
delete [别名] from 表名[[as] 别名] [where条件];

注意:
如果⽆别名的时候,表名就是别名
如果有别名,delete后⾯必须写别名
如果没有别名,delete后⾯的别名可以省略不写。

2@多表删除
可以同时删除多个表中的记录,语法如下:
delete [别名1,别名2] from 表1 [[as] 别名1],表2 [[as] 别名2] [where条件];

说明:
别名可以省略不写,但是需要在delete后⾯跟上表名,多个表名之间⽤逗号隔开。
平时我们⽤的⽐较多的⽅式是delete from 表名这种语法.


2、使用truncate删除
语法   truncate 表名;

注意

#truncate删除内容、释放空间但不删除定义(保留表的数据结构),与drop不同的是,只是清空表数据⽽已。
#truncate不能删除具体⾏数据,要删就要把整个表清空了。
#如果要删除表定义及其数据,请使⽤drop table语句。
#安全性:⼩⼼使⽤ drop 和 truncate,尤其没有备份的时候,否则哭都来不及。
#删除速度,⼀般来说: drop> truncate > delete

猜你喜欢

转载自blog.csdn.net/qq_46009608/article/details/113837162