MySQL data manipulation statements
1. Master
DDL - Data Definition Language ** create / drop / alter **
create: Create a
drop: Delete
alter: ModifyDML - Data Manipulation Statements INSERT ** / the Delete / Update / the SELECT **
INSERT: insert data
delete: delete data
update: update the data
select: Find dataDCL - Data Control Statements Grant ** / ** REVOKE
grent: Authorization
revoke: deauthorize
2. DDL - Data Definition Language
2.1 create: create
- Create a database school
create database school default charset utf8mb4;
- Create a table
create table tb_student
(
stuid int not NULL,
stuname VARCHAR(20) not NULL,
stusex bit DEFAULT 1,
stubirth date,
PRIMARY KEY (stuid)
);
2.2 drop: Delete
- Delete Database
drop database school
drop database if exists school
- Delete table
drop table if exists tb_student;
2.3 alter: Modify
- Modify the table - add / modify / remove columns
-- 增加
alter table tb_student add column colid int;
alter table tb_student add column stuaddr varchar(255);
-- 修改
alter table tb_student change column stuaddr stuaddr varchar(511);
-- 删除
alter table tb_student drop column stuaddr;
3. DML - Data Manipulation Language
3.1 insert: inserting data
- Inserting data - full input / selection input / batch input
insert into tb_student values (1001,'阳光检',1,'1999-2-2','太阳');
insert into tb_student (stuid, stuname) values (1003,'天残');
insert into tb_student (stuid ,stuname, stusex) values
(1005, '一号', default),
(1006, '二号', 0),
(1007, '三号', 1);
3.2 delete: delete data
-- 截断表 ---危险操作 >>>慎重,慎重,慎重
-- truncate table tb_student;
-- 删除学号为1002 的学生
delete from tb_student where stuid=1002;
-- 删除所有女生
delete from tb_student where stsex=0;
3.3 update: update data
-- 更新操作
update tb_student set stuaddr='四川成都' where stuid = 1003 or stuid =1004;
update tb_student set stuaddr='四川成都' where stuid in (1006,1007);
update tb_student set stubirth = '2000-2-29',stuaddr='太阳以南'
where stuid=1005;
3.4 select: Find data
4. DCL - data control statement
4.1 grent: Authorization
-- 给远程访问权限
create user 'root'@'%' identified by '123456';
-- 更新 flush 使设置生效
flush privileges;
-- 给远程root所有文件全局最大权限
grant all privileges on *.* to 'root'@'%' with grant option;
4.2 revoke: deauthorize
5. Other
5.1 show display
-- 显示数据库
show databases;
-- 显示数据库
show databases;
5.2 between in = Filters
-- between 两者直接 闭区间(包含1001,1006)
-- in 多个
-- = 单个
update tb_student set colid =1 where stuid between 1001 and 1006;
update tb_student set colid =2 where stuid in (1008,1009);
update tb_student set colid =3 where stuid=1007;
5.3 Relations - add key constraint
- Primary key
- Foreign key
- The primary key constraint
-- 创建老师表
create table tb_teacher
(
teaid int not null comment '工号',
teaname varchar(20) not null comment '姓名',
teasex bit default 1 comment '性别',
teabirth date comment '生日',
teatitle varchar(10) default '助教' comment '职称',
colid int not null comment '所在学院'
-- 创建时添加 约束
-- 添加主键约束
-- primary key (teaid),
-- 添加外键约束
-- foreign key (colid) references tb_college (colid)
);
-- 添加主键约束
alter table tb_teacher add constraint pk_teacher_teaid
primary key (teaid);
-- 添加外键约束
alter table tb_teacher add constraint fk_teacher_colid
foreign key (colid) references tb_college (colid);
Adding a unique constraint
select
data types