show tables;
create table tb_name(
id int not null auto_increment,
value int(2) not null default 0,
name char(10) default 'aa',
area char(20) default null,
primary key(id,value),
unique key(area),
constraint fk_name foreign key (name) references tb_other(name)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
ps:
外键的好处:可以使得两张表关联,保证数据的一致性和实现一些级联操作;
唯一键作用:一是约束作用(constraint),规范数据的唯一性,但同时也在这个key上建立了一个唯一索引;
unique key 与 primary key 区别:unique key 可为null;
添加列:
alter table 表名 add 列名 类型
删除列:
alter table 表名 drop column 列名
修改列:
alter table 表名 modify column 列名 类型; -- 类型
alter table 表名 change 原列名 新列名 类型; -- 列名,类型
添加主键:
alter table 表名 add primary key(列名);
删除主键:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int, drop primary key;
添加外键:
alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键:
alter table 表名 drop foreign key 外键名称
修改默认值:
ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:
ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
清空表:
delete from tb_name;
删除表:
drop table tb_name;
数据库第二讲
最新推荐文章于 2024-06-18 20:15:29 发布
本文介绍了数据库中创建表、显示表、定义主键、唯一键和外键的关系,以及如何进行列的增删改、默认值的设置和删除、表的清理和删除等基本操作。
摘要由CSDN通过智能技术生成