mysql 建表操作集锦

truncate tablename 清空,不会对事务有影响,清空计数器

时间--
date_format(now(),'%Y-%m-%d %H:%i:%s') 
current_timestamp() 当前时间戳

完整性约束关键字
not null
default
unique key    不针对null,可以多个null
primary key    一个表一个主键
auto_increment    不能单独使用自增,只能配合主键约束使用。一个表只能有一个自增。整数类型
foreign key    用法:constraint fk01 foreign key(id2) references staff(id)
联合主键:primary key(id,name)
    或者:constraint pk1 primary key(id,name)

# 添加外键
alter table t2 add constraint fk3 foreign key(id) references foot(id)
# 去掉主键
alter table t2 drop primary key    #有外键约束则无法删除
#添加主键
alter table t2 add primary key(id)
#删除外键
alter table t2 drop foreign key fk3;
#去掉自增
alter table t2 modify id int;

# 外键的级联关系
1、on delete cascade    级联删除
2、on update cascade    级联修改
3、on delete set null        设置为空

alter table t2 add constraint fk3 foreign key(id) references foot(id) on delete cascade

手动建立两张关联表:class 和 student

class表结构:(desc class 查询表结构结果截图)

student表结构:

直接展示建表语句

create table class(
	class_id int primary key auto_increment,
	class_name varchar(20) not null,
	class_address varchar(20) not null
);
desc class;

create table student(
	stu_id int primary key auto_increment,
	cla_id int not null,
	stu_name varchar(20) not null,
	second_name varchar(20) unique key,
	stu_sex varchar(5) not null default '男',
	constraint fk001 foreign key(cla_id) references class(class_id)
);

desc student;
show create table student;

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值