Data Definition Language
1 库的操作
1.1 create 创建
create database books;
为了增加容错性,我们应该写成:
create database if not exists books;
# 如果不存在books这个库,即创建
1.2 alter 修改
如果想对库进行修改,首先要停止服务,直接修改文件夹名
一般来说,不建议这个操作,容易丢失数据
更改库的字符集:
alter database books character set gbk;
1.3 drop 删除
库的删除,不能重复执行,同1.1一样,此时加上if exists
drop database if exists books;
2 表的操作
2.1 表的创建
在books
数据库中创建一个book
表
create table book(
id int, #编号
bName varchar(20), #书名
price double, #价格
authorId int, #作者编号
publishDate datetime #出版日期
);
在books
数据库中创建一个author
表
create table author(
id int,
au_name varchar(20),
au_city varchar(10)
);
2.2 表的修改
2.2.1 修改表名
把author
表修改为book_authors
表
alter table author
rename to book_authors;
2.2.2 修改列名
语法:
alter table 表名
change column 列名 新列名 列的类型
把publishdate
改成pubDate
alter table book
change column publishdate pubDate datetime;
2.2.3 修改列的类型和约束
把pubDate
的类型改成timestamp
alter table book
modify column pubDate timestamp;
2.2.4 添加列
在author
表中添加新列annual
年薪
alter table author
add column annual double;
2.2.5 删除列
把刚刚添加的删了
alter table author
drop column annual;
2.3 表的删除
drop table 表名;
例如:删除book_author
表
drop table book_author;
2.4 表的复制
- 只会复制表的结构,但是不包含值
create table copy
like author;
- 复制表的结构+值
create table copy2
select *
from author;
- 复制部分数据
create table copy3
select id,au_name
from author
where au_city = '中国';
3 练习
#1.
create table dept1(
id int(7),
name varchar(25)
);
#2.
create table dept2
select department_id,department_name
from myemplyees.departments;
#3.
create table emp5(
id int(7),
First_name varchar(25),
Last_name varchar(25),
Dept_id int(7)
);
#4.
alter table emp5
modify column Last_name varchar(50);
#5.
create table employees2
like myemplyees.employees;
#6.
drop table if exists emp5;
#7.
alter table employees2
rename to emp5;
#8.
alter table dept
add column test_column int;
alter table emp5
add column test_column int;
#9.
alter table emp5
drop column dept_id;
4 常见约束
创建表的时候,为了保证表中的数据的准确性和可靠性
- not null 非空 比如姓名、学号
- default 默认值,比如性别
- primary key 保证该字段的值具备唯一性,比如学号、员工编号
- unique 唯一性,可以为空,比如座位号
- check 检查约束,在mysql中不支持,没效果
- foreign key 外键约束,用于限制两个表,比如学员表和专业表,员工表的部门编号,员工表的工种编号。
添加约束的时机:
1.创建表时
2.修改表时
4.1 列级约束
检查和外键约束没有效果
创建学生表:
create table stuinfo(
id int primary key,
stuName varchar(20) not null,
gender char(1) check(gender = '女' or gender = '男'),
seat int unique,
age int default 18, # 默认约束
majorId int references major(id) # 外键
);
创建专业表:
create table major(
id int primary key,
majorName varchar(20) not null
);
4.2 表级约束
除了非空和默认,其他的都支持
create table students(
id int,
stuName varchar(20),
gender char(1),
seat int,
age int,
majorId int,
constraint pk primary key(id),
constraint uq unique(seat),
constraint ck check(gender = '女' or gender = '男',
constraint fk_stuinfo_major foreign key(majorId) references major
);
查看索引
比如查看stuinfo表中的所有索引,包括主键、外键
show index from stuinfo;
通用写法
create table if not exists stuinfo(
id int primary key,
stuName varchar(20) not null,
gender char(1),
seat int unique,
age int default 18,
majorId int,
constraint fk_stuinfo_major foreign key(majorId) references major(id)
);
4.3 主键和唯一键 对比
主键primary key | 唯一键unique | |
---|---|---|
保证唯一性 | ✓ | ✓ |
是否允许为空 | × | ✓ |
一个表中可以有多少个 | 最多1个 | 可以有多个 |
是否允许组合(但不推荐) | ✓ | ✓ |
外键:
外键引用的列必须是主键或者唯一键
插入数据时,先插入主表,再插入从表
删除数据时,先删除从表,再删除主表
4.4 修改表时添加约束
alter table stuinfo
modify column stuname varchar(20) not null;
alter table stuinfo
modify column age int default 18;
alter table stuinfo
modify column id int primary key;
#这种可以改名
alter table emp2
add constraint my_emp_id_pk primary key(id);
4.5 修改表时删除约束
#1.删除非空约束
alter table stuinfo
modify column stuname varchar(20) null;
#2.删除默认约束
alter table stuinfo
modify column age int;
#3.删除主键
alter table stuinfo
drop primary key;
#4.删除唯一键
alter table stuinfo
drop index seat;
练习:
向表emp2中添加列dept_id,并在其中定义FOREIGN KEY约束,与之相关联的列是dept2表中的id列
alter table emp2
add column dept_id int;
alter table emp2
add constraint fk_emp2_dept2 foreign key(dept_id)