日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL建表,DML,DDL,约束,外键策略

發(fā)布時(shí)間:2023/12/3 数据库 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL建表,DML,DDL,约束,外键策略 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

創(chuàng)建數(shù)據(jù)庫表

CREATE TABLE student(
sno int (6),
sname VARCHAR(10),
sex CHAR(1),
age INT(2),
enterdate date,
classname VARCHAR(10),
email VARCHAR(15)
);

– 查看表的結(jié)構(gòu)
desc student

– 查看表的數(shù)據(jù)
SELECT * FROM student

DML

– 查詢表的數(shù)據(jù)
SELECT * FROM student

– insert
insert into student values(1,‘張三’,‘男’,23,‘2021/2/27’,‘一班’,‘zhs@.com’);
insert into student values(2,‘李四’,‘男’,24,‘2021/2/27’,‘一班’,‘lis@.com’);
insert into student (sno,sname,sex,classname) values (3,‘王五’,‘男’,‘二班’)

– update
update student set sex = ‘女’ where sno = 2
update student set sex = ‘男’,age = 25,classname = null where sno = 2

– delete
delete FROM student WHERE classname = ‘二班’

– 刪除表中的所有數(shù)據(jù)
delete FROM student
truncate table student – 速度快,無法回滾 DDL語言 效率高

DDL

– DDL creat alter drop
– DML insert update delete

– 查詢數(shù)據(jù)
select * from student

– 查詢表的結(jié)構(gòu)
desc student

– 修改表的數(shù)據(jù)
– 增加一列
alter table student add score double(5,2);
alter table student add score double(5,2) first
alter table student add score double(5,2) after enterdate;

-- 修改一列

alter table student modify score float(4,1);
alter table student change score score1 double(4,1);

-- 刪除一列

alter table student drop score;

– 刪除表
drop table student

非外鍵約束

– 列級約束
CREATE TABLE student(
sno int (6) primary key auto_increment,
sname VARCHAR(10) not null,
sex CHAR(1) default ‘男’ check(sex = ‘男’ or sex = ‘女’),
age INT(2) check(age>=0 and age<=50),
enterdate date,
classname VARCHAR(10),
email VARCHAR(15)
);

– 表級約束
CREATE TABLE student(
sno int (6) auto_increment,
sname VARCHAR(10) not null,
sex CHAR(1) default’男’,
age INT(2),
enterdate date,
classname VARCHAR(10),
email VARCHAR(15),
constraint pk_stu primary key(sno),
constraint ck_stu_sex check(sex = ‘男’ or sex = ‘女’),
constraint ck_stu_age check(age>=0 and age<=50),
constraint ck_stu_email unique(email)
);

CREATE TABLE student(
sno int (6),
sname VARCHAR(10) not null,
sex CHAR(1) default ‘男’, – not null 和 default 只能使用列級約束
age INT(2),
enterdate date,
classname VARCHAR(10),
email VARCHAR(15)
);

alter table student add constraint pk_stu primary key(sno);
alter table student add constraint ck_stu_sex check(sex = ‘男’ or sex = ‘女’);
alter table student add constraint ck_stu_age check(age>=0 and age<=50);
alter table student add constraint ck_stu_email unique(email);
alter table student modify sno int(6) auto_increment

外鍵約束

create table t_student(
sno int(6)primary key auto_increment,
sname varchar(10) not null,
age int(3),
score double(4,1),
classno int(4)
– constraint fk_stu_classno foreign key(classno) references t_class(cno)
);
– 外鍵
alter table t_student add constraint fk_stu_classno foreign key(classno) references t_class(cno)

外鍵策略

– 1.NO Action:
delete from t_class cno=2
update t_student set classno = null where class =2

– 2.cascade級聯(lián)
– 加外鍵alter table t_student add constraint fk_stu_classno foreign key(classno) references t_class(cno)
– 刪外鍵alter table t_student drop FOREIGN KEY fk_stu_classno

– 級聯(lián)
alter table t_student add constraint fk_stu_classno foreign key(classno) references t_class(cno) on update cascade on delete cascade

– 3.set null
alter table t_student add constraint fk_stu_classno foreign key(classno) references t_class(cno) on update set null on delete set null

總結(jié)

以上是生活随笔為你收集整理的MySQL建表,DML,DDL,约束,外键策略的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。