【mysql】一些知识的补充

1|0mysql8.0

2|01.创建数据库表

##这是一个单行注释 /* 多行注释 多行注释 多行注释 */ /* 建立一张用来存储学生信息的表 字段包含学号、姓名、性别,年龄、入学日期、班级,email等信息 */ -- 创建数据库表: create table t_student( sno int(6), -- 6显示长度 sname varchar(5), -- 5个字符 sex char(1), age int(3), enterdate date, classname varchar(10), email varchar(15) ); -- 查看表的结构:展示表的字段详细信息 desc t_student; -- 查看表中数据: select * from t_student; -- 查看建表语句: show create table t_student; /* CREATE TABLE `t_student` ( `sno` int DEFAULT NULL, `sname` varchar(5) DEFAULT NULL, `sex` char(1) DEFAULT NULL, `age` int DEFAULT NULL, `enterdate` date DEFAULT NULL, `classname` varchar(10) DEFAULT NULL, `email` varchar(15) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci */

3|02.修改删除数据

-- 修改表中数据 update t_student set sex = '女' ; update t_student set sex = '男' where sno = 10 ; UPDATE T_STUDENT SET AGE = 21 WHERE SNO = 10; update t_student set CLASSNAME = 'java01' where sno = 10 ; update t_student set CLASSNAME = 'JAVA01' where sno = 9 ; update t_student set age = 29 where classname = 'java01'; -- 删除操作: delete from t_student where sno = 2;

4|03.修改删除数据库中的表

-- 查看数据: select * from t_student; -- 修改表的结构: -- 增加一列: alter table t_student add score double(5,2) ; -- 5:总位数 2:小数位数 update t_student set score = 123.5678 where sno = 1 ; -- 增加一列(放在最前面) alter table t_student add score double(5,2) first; -- 增加一列(放在sex列的后面) alter table t_student add score double(5,2) after sex; -- 删除一列: alter table t_student drop score; -- 修改一列: alter table t_student modify score float(4,1); -- modify修改是列的类型的定义,但是不会改变列的名字 alter table t_student change score score1 double(5,1); -- change修改列名和列的类型的定义 -- 删除表: drop table t_student;

5|04.外键、非外键约束

5|11.非外键约束

create table t_student( sno int(6) primary key auto_increment, sname varchar(5) not null, sex char(1) default '男' check(sex='男' || sex='女'), age int(3) check(age>=18 and age<=50), enterdate date, classname varchar(10), email varchar(15) unique ); alter table t_student add constraint pk_stu primary key (sno) ; -- 主键约束 alter table t_student modify sno int(6) auto_increment; -- 修改自增条件 alter table t_student add constraint ck_stu_sex check (sex = '男' || sex = '女'); alter table t_student add constraint ck_stu_age check (age >= 18 and age <= 50); alter table t_student add constraint uq_stu_email unique (email);

5|22.外键约束

外键策略4种:no action、restrict(和no action一样,是默认策略)、set null、cascade

空、RESTRICT、NO ACTION

删除:从表记录不存在时,主表才可以删除,删除从表,主表不变。
更新:从表记录不存在时,主表菜可以更新,更新从表,主表不变。

CASCADE

删除:删除主表时自动删除从表。删除从表,主表不变。
更新:更新主表时自动更新从表。更新从表,主表不变。

ET NULL

删除:删除主表时自动更新从表为NULL,删除从表,主表不变。
更新:更新主表时自动更新从表值为NULL。更新从表,主表不变。

CREATE TABLE t_class ( cno INT ( 4 ) PRIMARY KEY auto_increment, cname VARCHAR ( 10 ) NOT NULL, room CHAR ( 4 ) ); CREATE TABLE t_student ( sno INT ( 6 ) PRIMARY KEY auto_increment, sname VARCHAR ( 5 ) NOT NULL, classno INT ( 4 ), CONSTRAINT fk_stu_classno FOREIGN KEY ( classno ) REFERENCES t_class ( cno ) ); -- insert into t_class values (null,'1班',1) -- insert into t_student values(null,'柽柳',1),(null,'王五',1) 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 ; alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class (cno) on update cascade on delete cascade; -- 级联删除更新 alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class (cno) on update set null on delete set null; -- 置空删除更新

6|05.复制表或者复制表结构(快速添加)

-- 添加一张表:快速添加:结构和数据跟t_student 都是一致的 create table t_student2 as select * from t_student; -- 快速添加,结构跟t_student一致,数据没有: create table t_student3 as select * from t_student where 1=2; select * from t_student3; -- 快速添加:只要部分列,部分数据: create table t_student4 as select sno,sname,age from t_student where sno = 2; select * from t_student4; -- 删除数据操作 :清空数据 delete from t_student; truncate table t_student;

7|06.数据查询操作

-- 去重操作 select distinct job ,deptno from emp; -- 对后面的组合去重 -- 区分大小写 select * from emp where job = 'clerk'; -- 一般查询不区分,区分加binary select * from emp where binary job = 'clerk'

__EOF__

本文作者blanset
本文链接https://www.cnblogs.com/blanset/p/16907503.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   求道之愚者  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示