<MySQL——L2>
文章目录
- <MySQL——L2>
- 1.DDL(Data Define Language )数据定义语言
- 1.1库的管理
- 1.2数据类型
- 1.3表的管理
- 1.4完整性约束
- 2.DML(Data Manipulation Language)数据操作语言
1.DDL(Data Define Language )数据定义语言
1.1库的管理
show databases;
use hero;
select * from employees;
select database();
create database if not EXISTS test;
create database if not EXISTS test2509;
drop database if EXISTS test2500;
drop database if EXISTS test2509;
1.2数据类型
drop table if EXISTS test;
create table if not EXISTS test(c1 int(11) UNSIGNED, c2 double(5,2), c3 float(6,2), c4 decimal(10,1)
);insert into test values(-111,999.99,9999.99,8888.8);
insert into test values(111,999.99,9999.99,8888.8);
insert into test values(111,999.995,9999.99,8888.8);
insert into test values(111,999.994,9999.99,8888.8);
insert into test values(111111111112,999.99,9999.99,8888.8);select * from test;
drop table if EXISTS test;
create table if not EXISTS test(c1 char(9),c2 varchar(9),c3 text
);insert into test values('abc','abc','aaabbbccc');insert into test values(' abc ',' abc ','aaabbbccc');
insert into test values('abc ','abc ','aaabbbccc');select * from test;
select concat('###',c1,'###') , concat('###',c2,'###') from test;
select length(c1) , length(c2) from test;
drop table if EXISTS test;
create table if not EXISTS test(c1 date,c2 time,c3 datetime,c4 timestamp DEFAULT CURRENT_TIMESTAMP
);desc test;insert into test values(now() , now() , now() , now());
insert into test(c1) values('2025-12-12');
insert into test(c2) values('12:12:12');
insert into test(c3) values('2025-12-12 12:12:12');
insert into test(c3) values(20251212121212);
insert into test(c1,c2,c3) values(now() , now() , now());select * from test;
1.3表的管理
drop table if EXISTS stus;
create table if not EXISTS stus(id int,name varchar(255),major varchar(255),gender char(1),grade double(3,1)
);
desc stus;
SHOW TABLES;
SHOW CREATE TABLE stus;
CREATE TABLE `stus` (`id` int DEFAULT NULL,`name` varchar(255) DEFAULT NULL,`major` varchar(255) DEFAULT NULL,`gender` char(1) DEFAULT NULL,`grade` double(3,1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
alter table stus rename student;
alter table student add classname varchar(25);
alter table student change gender sex char(1);
alter table student change name name varchar(100);
alter table student modify name varchar(300);
alter table student drop classname;desc student;
create table emp like employees;
desc emp;
select * from emp;
create table emps select * from employees;
create table new_emp select last_name,department_id,salary from employees where 1=2;
desc new_emp;
select * from new_emp;
create index idx_name on employees(first_name);
create view v_emp as select first_name , salary , department_id from employees;
select * from v_emp where salary BETWEEN 10000 and 15000;
drop table if exists empinfo;
create table if not exists empinfo(num int PRIMARY KEY, employee_id VARCHAR(10) UNIQUE, employee_name VARCHAR(255) NOT NULL,gender CHAR(1) CHECK(gender IN('男','女')),age int UNSIGNED,personId CHAR(18) UNIQUE, hiredate date
);
desc empinfo;select * from empinfo;
drop table if exists dept1;
create table if not exists dept1(id int(7),name VARCHAR(25));
desc dept1;
drop table if exists dept2;
create table if not exists dept2 as select * from departments;
desc dept2;
select * from dept2;
drop table if exists emp5;
create table if not exists emp5(id int(7),First_name VARCHAR(25),Last_name VARCHAR(25),Dept_id int(7)
);
desc emp5;
ALTER TABLE emp5 MODIFY Last_name VARCHAR(50);
drop table if exists employees2;
create table if not exists employees2 as select * from employees;
desc employees2;
select * from employees2;
drop table if exists emp5;
alter table employees2 RENAME to emp5;
desc employees2;
desc dept1;
ALTER TABLE dept1 ADD test_column VARCHAR(50);
desc emp5;
ALTER TABLE emp5 ADD test_column VARCHAR(50);
desc emp5;
ALTER TABLE emp5 drop department_id;
1.4完整性约束
drop table if EXISTS major;
create table if not EXISTS major(id int PRIMARY KEY,name varchar(25) not null
);insert into major values(10,'计算机'),(20,'大数据'),(30,'网络安全');
select * from major;
drop table if EXISTS stu;
create table if not EXISTS stu(id int PRIMARY KEY, name varchar(255) UNIQUE not null, gender char(1) DEFAULT '男', email varchar(20) not null, age int check(age between 0 and 100), majorId int
);
desc stu;
insert into stu values(1,'小宫',DEFAULT,'123@qq.com',18,10);
insert into stu values(null,'小宫',DEFAULT,'123@qq.com',18,10);
insert into stu values(2,null,DEFAULT,'123@qq.com',18,10);
insert into stu values(2,'小小','女','123@qq.com',18,10);
insert into stu values(3,'小红花','女','123@qq.com',100,10);
select * from stu;
drop table if EXISTS stu;
create table if not EXISTS stu(id int, name varchar(255) not null, gender char(1) DEFAULT '女', email varchar(20) , age int, majorId int,PRIMARY KEY(id),constraint un_name UNIQUE(name),constraint ck_gender check(gender in('男','女')),constraint fk_majorId FOREIGN KEY(majorId) REFERENCES major(id)
);
desc stu;
insert into stu values(1,'小宫',DEFAULT,'123@qq.com',18,10);
insert into stu values(1,'小宫',DEFAULT,'123@qq.com',18,10);
insert into stu values(2,null,DEFAULT,'123@qq.com',18,10);
insert into stu values(2,'小小','女','123@qq.com',18,40);
insert into stu values(3,'小红花','非','123@qq.com',100,10);
select * from stu;
drop table if EXISTS stu;
create table if not EXISTS stu(id int,name varchar(20),gender char(1),email varchar(20),age int,majorId int
);
desc stu;
alter table stu modify id int primary key;
alter table stu add primary key(id);
alter table stu drop primary key;
alter table stu add constraint fk_majorId FOREIGN KEY(majorId) REFERENCES major(id);
alter table stu drop FOREIGN KEY fk_majorId;
alter table stu add FOREIGN KEY(majorId) REFERENCES major(id);
alter table stu drop FOREIGN KEY stu_ibfk_1;
alter table stu modify name varchar(255) unique;
alter table stu add constraint un_name UNIQUE(name);
alter table stu drop index name;
desc stu;
alter table stu modify email varchar(25) not null;
alter table stu modify email varchar(25);
desc stu;
alter table stu modify gender varchar(1) DEFAULT '女';
alter table stu modify gender varchar(1);
desc stu;
alter table stu modify gender varchar(1) check(gender in('男','女'));
alter table stu add constraint ck_gender check(gender in('男','女'));
alter table stu drop check ck_gender;
insert into stu value(2,'小草','非','123@qq.com',19,20);
select * from stu;
drop table if EXISTS stu;
create table if not EXISTS stu(id int PRIMARY KEY, name varchar(20) UNIQUE not null, gender char(1) DEFAULT '男', email varchar(20) not null, age int check(age BETWEEN 1 and 120), majorId int,FOREIGN KEY(majorId) REFERENCES major(id)
);
desc stu;
drop table if EXISTS stu;
create table if not EXISTS stu(id int, name varchar(20) , email varchar(25)
);
desc stu;
insert into stu values(1,'小张' , '123@qq.com');
insert into stu values(1,'小张' , '123@qq.com');
insert into stu values(null,'小张' , '123@qq.com');
insert into stu values(2,'小张' , '123@qq.com');
insert into stu values(2,null , '123@qq.com');
insert into stu values(3,null , '123@qq.com');
alter table stu add primary key(id,name);
insert into stu values(1,'小张' , '123@qq.com');
insert into stu values(1,'小小' , '123@qq.com');
insert into stu values(2,'小小' , '123@qq.com');
insert into stu values(2,'小小' , '123@qq.com');
alter table stu add constraint un_key UNIQUE(name , email);
insert into stu values(1,'小张' , '123@qq.com');
insert into stu values(1,'小小' , '123@qq.com');
insert into stu values(2,'小小' , '123@qq.com');select * from stu;
drop table if exists qqinfo;
create table if not exists qqinfo(qqid int PRIMARY KEY,nickname VARCHAR(25) UNIQUE,email VARCHAR(20) not null,gender char(1)
);
desc qqinfo;
desc emp5;
alter table emp5 add constraint my_emp_id_pk PRIMARY KEY (employee_id);
desc dept2;
alter table dept2 add constraint my_dept_id_pk PRIMARY KEY (department_id);
desc emp5;
alter table emp5 add dept_id int;alter table emp5 add constraint fk_emp5_dept2 foreign key (dept_id) references dept2(department_id);
drop table if exists hero_equipment;
drop table if exists hero;
drop table if exists role;
drop table if exists equipment;
create table if not exists role(role_id int PRIMARY KEY,role_name varchar(20) not null UNIQUE,role_type varchar(10) not null
);
create table if not exists equipment(equip_id int PRIMARY KEY,equip_name varchar(30) not null UNIQUE,equip_type varchar(10) not null,price int not null
);
create table if not exists hero(hero_id int PRIMARY KEY,hero_name varchar(20) not null UNIQUE, role_id int not null,position varchar(20),foreign key (role_id) references role(role_id)
);
create table if not exists hero_equipment(hero_id int,equip_id int,PRIMARY KEY(hero_id,equip_id),foreign key (hero_id) references hero(hero_id),foreign key (equip_id) references equipment(equip_id)
);
INSERT INTO role (role_id, role_name, role_type) VALUES
(1, '战士', '近战'),
(2, '法师', '远程'),
(3, '射手', '远程');INSERT INTO equipment (equip_id, equip_name, equip_type, price) VALUES
(101, '无尽战刃', '攻击', 2140),
(102, '博学者之怒', '法术', 2300),
(103, '不祥征兆', '防御', 2180);INSERT INTO hero (hero_id, hero_name, role_id, position) VALUES
(1001, '亚瑟', 1, '上路'),
(1002, '安琪拉', 2, '中路'),
(1003, '后羿', 3, '下路');INSERT INTO hero_equipment (hero_id, equip_id) VALUES
(1001, 103),
(1002, 102),
(1003, 101);
SELECT h.hero_name as '英雄',r.role_name as '职业',h.position as '位置'
FROM hero h
JOIN role r ON h.role_id = r.role_id;
SELECT h.hero_name as '英雄',e.equip_name as '装备',e.equip_type as '类型'
FROM hero h
JOIN hero_equipment he ON h.hero_id = he.hero_id
JOIN equipment e ON he.equip_id = e.equip_id;
SELECT role_name as '职业',COUNT(*) as '英雄数量'
FROM role r
JOIN hero h ON r.role_id = h.role_id
GROUP BY r.role_id;drop table if EXISTS stu;
create table if not EXISTS stu(id int, name varchar(20) , grade CHAR(3));
insert into stu values(1,"的","以发热");
desc stu;use mysql;
select * from user;
2.DML(Data Manipulation Language)数据操作语言
drop table if EXISTS stu;
create table if not EXISTS stu(id int PRIMARY key auto_increment, name varchar(255) UNIQUE,grade char(3) DEFAULT "一年级"
);
insert into stu values(NULL , "小张同学" , "一年级");
insert into stu values(NULL , "小王同学" , "一年级");
insert into stu values(NULL , "小陈同学" , "二年级");
insert into stu values(NULL , "小孙同学" , "三年级");
insert into stu values(NULL , "小李同学" , "一年级");
select * from stu;
insert into stu(name) values("小张同学");
insert into stu(name) values("小王同学");
insert into stu(name) values("小孙同学");
select * from stu;
insert into stu values(NULL,"小黄同学",DEFAULT),(NULL,"小李同学",DEFAULT),(NULL,"小何同学",DEFAULT);
drop table if EXISTS stu;
create table if not EXISTS stu(id int, name varchar(255) UNIQUE,grade char(3) DEFAULT "一年级"
);
alter table stu modify id int PRIMARY KEY auto_increment;
insert into stu(name) values("小张同学");
insert into stu(name) values("小王同学");
insert into stu(name) values("小孙同学");
select * from stu;
show variables like '%auto_increment%';
set auto_increment_offset = 10;
set auto_increment_increment = 10;
drop table if EXISTS stu;
create table if not EXISTS stu(id varchar(36) PRIMARY KEY, name varchar(255) UNIQUE,grade char(3) DEFAULT "一年级"
);
insert into stu values(UUID() , "小张同学" , DEFAULT);
insert into stu values(UUID() , "小孙同学" , DEFAULT);
insert into stu values(UUID() , "小黄同学" , DEFAULT);
select * from stu;
drop table if EXISTS stu;
create table if not EXISTS stu(id BIGINT PRIMARY KEY, name varchar(255) UNIQUE,grade char(3) DEFAULT "一年级"
);
insert into stu values(UUID_SHORT() , "小张同学" , DEFAULT);
insert into stu values(UUID_SHORT() , "小孙同学" , DEFAULT);
insert into stu values(UUID_SHORT() , "小黄同学" , DEFAULT);
select * from stu;
update stu set grade = '二年级';
update stu set grade = '三年级' where name like '%孙%';
select * from stu;
delete from stu;
delete from stu where id = 6;insert into stu(name) values("小张同学");
insert into stu(name) values("小王同学");
insert into stu(name) values("小孙同学");
select * from stu;
truncate table stu;insert into stu(name) values("小张同学");
insert into stu(name) values("小王同学");
insert into stu(name) values("小孙同学");select * from stu;
drop table stu;
select * from stu;
start TRANSACTION;
select * from girl where id = 12;
update girl set phone = '18209876579' where id =12;
ROLLBACK;
select * from girl where boyfriend_id is null;
insert into girl values(default,'灭绝师太', '女', '1990-05-15 00:00:00', '18209876666', null,null);
select * from girl where boyfriend_id is null;
delete from girl where boyfriend_id is null;
insert into girl (name, sex, borndate, phone, boyfriend_id)
values ('黄蓉', '女', '1990-05-15 00:00:00', '18209876666', 1);
select * from girl where borndate > '1988-12-31';
desc my_employees;
drop table if exists my_employees;
create table my_employees(
Id int(10),
First_name varchar(10),
Last_name varchar(10),
Userid varchar(10),
Salary double(10,2)
);
desc my_employees;
desc users;
create table users(
id int,
userid varchar(10),
department_id int
);
desc users;
desc my_employees;
INSERT INTO my_employees VALUES
(1, 'patel', 'Ralph', 'Rpatel', 895),
(2, 'Dancs', 'Betty', 'Bdancs', 860),
(3, 'Biri', 'Ben', 'Bhiri', 1100),
(4, 'Newman', 'Chad', 'Chewman', 750),
(5, 'Ropeburn', 'Audrey', 'Aropebur', 1550);
select * from my_employees;
INSERT INTO users VALUES
(1, 'Rpatel', 10),
(2, 'Bdancs', 10),
(3, 'Bhiri', 20),
(4, 'Chewman', 30),
(5, 'Aropebur', 40);select * from users;
select * from my_employees;
update my_employees set Last_name = 'drelxer' where Id = 3;
update my_employees set salary = 1000 where salary < 900;
select * from users;
delete from users where userid = 'Bhiri';select * from my_employees;
delete from my_employees where Userid = 'Bhiri';delete from users;
select * from users;delete from my_employees;
select * from my_employees;truncate table my_employees;
select * from my_employees;