1. 数据库的基本操作
查看数据库
show database;
查看创建数据库语句
show create database 数据库名
创建数据库
create database test_db;
删除数据库
drop database test_db;
使用数据库
use database test_db;
2. 数据库的基本操作
1. 创建数据表
create table tb_emp1
(
id int(11),
name varchar(25),
depeId Int(11),
salary float
);
1.1.使用主键约束
- 设置id为主键
create table tb_emp2
(
id int(11) primary key,
name varchcar(25),
deptId int(11),
salary float
);
- 也可在定义完成后设置主键
create table tb_emp3
(
id int(11),
name varchar(25),
deptId int(11),
salary float,
primary key(id)
);
- 设置多个主键
create table tb_emp4
(
id int(11),
name varchar(25),
deptId int(11),
salary float,
primary key(name,deptId)
);
1.2. 使用外键约束
- 主表(父表):对于两个具有关联关系的表而言,相关联字段中主键所在的那个表即是主表
- 从表(子表):对于两个具有关联关系的表而言,相关联字段中外键所在的那个表即是从表
- 创建一个部门表tb_dept1
create table tb_dept1
(
id int(11) primary key,
name varchar(25) not null,
location varchar(50)
);
- 定义数据表tb_emp5,让它的键deptId作为外键关联到tb_dept1的主键id,SQL语句为:
create table tb_emp5
(
id int(11) primary key,
name varchar(25),
deptId int(11),
salary float,
constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id)
);
以上语句执行成功之后,在表tb_emp5上添加了名称为fk_emp_dept1的外键约束,外键名称为deptId,其依赖于表tb_dept1的主键id。
1.3.使用非空约束
create table tb_emp6
(
id int(11) primary key,
name varchar(25) not null,
deptId int(11),
salary float
);
1.4. 使用唯一性约束
- 唯一性约束要求该列唯一,允许为空,但只能出现一个空值。唯一约束可以确保一列或者几列不出现重复值。
create table tb_dept2
(
id int(11) primary key,
name varchar(22) unique,
location varchar(50)
);
- 也可以在定义完所有列之后指定唯一约束
create table tb_dept3
(
id int(11) primary key,
name varchar(22),
location varchar(50),
constraint sth unique(name)
);
1.5. 使用默认约束
create table tb_emp7
(
id int(11) primary key,
name varchar(25) not null,
deptId int(11) default 1111,
salary float
)
1.6. 设置表的属性值自动增加
create table tb_emp8
(
id int(11) primary key auto_increment,
name varchar(25),
deptId int(11),
salary float
);
2.查看表结构
使用SQL语句创建好数据表之后,可以查看表结构的定义,以确定表的定义是否正确。在MySQL中,查看表结构可以使用
describe和show create table语句。
2.1 查看表基本语句describe
describe 表名;
可简写成
desc 表名;
2.2 查看表详细结构语句show create table 表名;
show create table 表名;
如果不用‘\G’参数显示结果就会很乱
show create table 表名 \G
3. 修改数据表
修改表指的是修改数据库中已经存在的数据表的结构。MySQL使用alter table 语句修改表。
3.1 修改表名
alter table tb_dept3 rename tb_deptment3;
3.2 修改字段的数据类型
alter table tb_dept1 modify name varchar(30);
3.3 修改字段名
将数据表中tb_dept1中的location字段名称改为loc,数据类型保持不变。
alter table tb_dept1 change location loc varchar(50);
3.4 增加字段
- 增加无完整性约束条件的字段
alter table tb_dept1 add managerId int(10);
- 增加有完整性约束条件的字段
alter table tb_dept1 add column1 varchar(12) not null;
- 在表的第一列添加一个字段
alter table tb_dept 1 add column2 int(11) first;
- 在表的指定列之后添加一个字段
在name列侯添加一个int类型的字段column3
alter table tb_dept1 add column3 int(11) after name;
3.5 删除字段
alter table tb_dept1 drop column2;
3.6 修改字段的排列位置
- 修改字段为表的第一个字段
alter table tb_dept1 modify column1 varchar(12) first;
- 修改字段到表的指定列之后
将column1字段插入到location字段之后。
alter table tb_dept1 modify column1 vartchar(12) after location;
3.7 更改表的存储引擎
alter table tb_deptment3 engine=myisam;
3.8 删除表的外键约束
alter table tb_emp9 drop foreign key fk_emp_dept;
4. 删除数据表
删除数据表就是将数据库中已经存在的表从数据库中删除。注意:在删除表的同时,表的定义和表中的数据均会被删除。因此,在进行删除操作前,最好对表中的数据做个备份,以免造成无法挽回的后果。
- 删除没有被关联的表
如果tb_dept2存在那就删除它。
drop table if exists tb_dept2;
- 删除被其他表关联的主表
先删除外键约束。
alter table tb_emp drop foreign key fk_emp_dept;
drop table tb_dept2;