数据表的基本操作
创建数据表
数据表属于数据库,在创建数据表之前,应该使用语句“USE <数据库名>” 指定操作是从哪个数据库中进行,如果没有选择数据库,会报错0
使用库
mysql> use mytest;
语法:create table t表名(字段 数据类型);
create table tb3(id int);
1.使用主键约束
单字段主键
主键约束要求主键列的数据唯一,并且不允许为空。
->create table tb2(
-> id int primary key, 指定主键
-> name varchar(25), 字段 字符
-> deptid int,
-> salary float);
desc tb2; 查看表tb2
往表tb2内插入数据
insert tb2(id,name,deptid,salary) values(1,"zs",123,1000);
查看表tb2
select * from tb2;
insert tb2(id) values(2);
如果设置主键以后只添加别的字段不添加主键会报错
insert tb2(name) values("ls"); 报错字段id没有默认值
也可以在定义完所有列之后指定主键
->create table tb3(
-> id int,
-> name varchar(25),
-> deptid int,
-> salary float,
-> primary key(id));
多字段联合主键
->create table tb4(
-> name varchar(25),
-> deptid int,
-> salary float,
-> primary key(name,deptid));
2.使用外键约束
外键用来在两个表数据之间建立连接,它可以是一列或者多列
语法:constraint 外键名 foreign key(字段) references 主表(主键字段));
创建主表
create table tb5(
id int primary key,
name varchar(22) not null,
location varchar(50));
插入数据
insert into tb5 values (1,"zs","changping");
创建外键表
create table tb6(
id int primary key,
name varchar (25),
deptid int,
salary float,
constraint fk_emp_5 foreign key(deptid) references tb5(id));
验证
先查看一下主表tb5的内容
select * from tb5;
给外键表tb6插入数据
insert tb6 values(1,"ls",1,1000);
插入主表没有的字段会报错
insert tb6 values(2,"ls",2,1000);
解释:deptid引用tb5的id,tb5没有的id在tb6的detpid无法使用
3.使用非空约束
非空约束指字段的值不能为空
语法:字段名 数据类型 not null
> create table tb7(
-> id int primary key,
-> name varchar(25) not null,
-> deptid int,
-> salary float);
4.唯一约束
该字段只能出现一次,但是可以为空
语法:constraint 约束名 unique (字段名)
> create table tb8(
-> id int primary key,
-> name varchar(22),
-> location varchar(26),
-> constraint sth unique (name));
5.使用默认约束
默认约束指定某列的默认值
> create table tb9(
-> id int primary key,
-> name varchar(25),
-> deptid int default 1111,
-> salary float,
-> info varchar(50));
插入两个数据
insert tb9(id,deptid) values(1,123);
insert tb9(id) values(2);
查看一下,如果插入了数据会显示插入的数据,如果没有插入数据,会以默认值显示,而不是为空
select * from tb9;
6.设置表的属性值自动增加
> create table tb10(
-> id int primary key auto_increment,
-> name varchar(25),
-> deptid int,
-> salary float);
desc tb10;
插入不设id的两行数据
insert into tb10(name,salary) values('zs',1000),('ls',2000);
设定id数据以后按设置的增加
insert into tb10(id) values(8);
insert into tb10(name,salary) values('ww',3000);
练习
创建第一个表,包含以下内容,并插入数据
(单列主键 主键自增 非空约束 唯一约束 默认值)
创建第二个表,包含以下内容,并插入数据
(多列主键 外键约束)
> create table tb1(
-> id int primary key auto_increment,
-> name varchar(25) not null,
-> ida int unique,
-> deptid int default 1111);
查看一下结构
desc test1;
插入数据
insert into test1(name) values('zs');
第二个表
> create table test2(
-> id int,
-> deptid int,
-> primary key(id,deptid),
-> constraint fk_1 foreign key(deptid) references test1(id));
查看一下表的结构
show create table test2\G
这个命令下边会讲
插入数据
insert into test2 (id,deptid) values(1,1);
删除数据表
删除表
语法:drop table if exists 表名;
drop table if exists tb1;
也可以删除多个表
drop table if exists tb1,tb3;
删除被其他表关联的主表
这里有两个表,tb5是主表,tb6是有外键约束的表
先试试删除主表tb5
drop table if exists tb5;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
可以看到在外键约束时,主表不能直接删除
解除关联子表tb_dept的外键约束(下一篇会讲)
alter table tb6 drop foreign key fk_emp_5;
表就可以被删除
drop table if exists tb5;