数据库进阶
create table student(
id int,
name varchar(20),
address varchar(20) default '江西赣州' -- 默认值
);
insert into student(id,name) values(1,'张三');
insert into student(id,name address) values(1,'李四',null)
-- 2.非空(not null)
-- 需求:gender字段必须有值(不为null)
create table student(
id int,
name varchar(20),
address varchar(20) not null
);
create table student(
id int unique, --唯一
name varchar(20),
address varchar(20)
);
create table student(
id int primary key;
name varchar(20),
address varchar(20)
);
create table student(
id int primary key auto_increment;
id int(4) zerofill primary key auto_increment;
name varchar(20),
address varchar(20)
);
create table dept(
id int primary key,
deptName varchar(20)
);
create table employee(
id int primary key,
empName varchar(20),
deptId int , -- 把部门名称改为部门id
-- 声明一个外键约束
constraint emlyee_dept_fk foreign key(deptId) references dept(id)
-- 外键名称 外键 参考表
)
insert into dept(id,deptName) values(1,'软件开发部');
insert into dept(id,deptName) values(2,'应用维修部');
insert into dept(id,deptName) values(3,'秘书部');
insert into employee values(1,'张三',1);
insert into employee values(2,'李四',2);
insert into employee values(3,'王五',3);
insert into employee values(4,'陈六',4);