-- 建表模板
-- 外键表
create table key_test
(
id int auto_increment,
bh int not null unique,
constraint primary key pid(id desc )#指定使用倒序排列
);
drop table key_test;
alter table key_test add name1 varchar(20) comment '名称';
alter table key_test drop name1;
alter table key_test alter column name1 varchar(20);#有些数据库无法使用
select * from key_test order by id desc ;#倒序排列
insert into key_test(bh) values (12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22);
create table test_creat
(
id int,
for_key int comment '外键,参照表key_test',
name varchar(20) unique not null comment '学生姓名',
age int comment '年龄' check (age>0 and age <100 ),
sex char(2) check ( sex = '男' or sex= '女'),
address varchar(60) comment '家庭住址',
primary key pid(id),
foreign key fid(for_key) references key_test(id),
constraint id_check check ( id is not null)
# constraint primary key pt_id(id),
# constraint foreign key f_key(for_key) references key_test(id)
);
drop table test_creat;