mysql8.0
1.创建数据库表
##这是一个单行注释
/*
多行注释
多行注释
多行注释
*/
/*
建立一张用来存储学生信息的表
字段包含学号、姓名、性别,年龄、入学日期、班级,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
*/
2.修改删除数据
-- 修改表中数据
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;
3.修改删除数据库中的表
-- 查看数据:
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;
4.外键、非外键约束
1.非外键约束
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);
2.外键约束
外键策略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; -- 置空删除更新
5.复制表或者复制表结构(快速添加)
-- 添加一张表:快速添加:结构和数据跟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;
6.数据查询操作
-- 去重操作
select distinct job ,deptno from emp; -- 对后面的组合去重
-- 区分大小写
select * from emp where job = 'clerk'; -- 一般查询不区分,区分加binary
select * from emp where binary job = 'clerk'