当前位置:网站首页>Set set!! Review quickly -- MySQL addition, deletion, modification and query, internal, left and right connection review notes
Set set!! Review quickly -- MySQL addition, deletion, modification and query, internal, left and right connection review notes
2022-06-26 01:25:00 【Mixed with bean curd and】
Two case databases were created: MyBingSchool、employee_tbl
The logic is clear ,SQL Easy to use . Review more
– establish MyBingSchool Set the character set to utf8( unicode )
create database MyBingSchool CHARSET=utf8;
– Access to database
use MyBingSchool;
– Delete table ( Structure and data )
drop table Student;
– Student information sheet
create table Student(
-- Primary key Student number varchar Out of commission default
s_id varchar(20) COMMENT ' Student number ',
s_name varchar(20) UNIQUE NOT NULL COMMENT ' The student's name ' ,
s_sex varchar(10) NOT NULL COMMENT ' Student gender ' ,
s_brith varchar(20) NOT NULL COMMENT ' The date of birth of the student ' ,
primary key(s_id)
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
– Query table data
SELECT * FROM Student;
select * from Cuosre;
select * from Sorce;
select * from Teacher;
– towards Student Table insert data
insert into Student VALUES('01',' Zhang San ',' male ','1996-01-24');
insert into Student VALUES('02',' The king 2 ',' male ','1998-09-01');
insert into Student VALUES('03',' Li Si ',' male ','1978-03-13');
insert into Student VALUES('04',' Zhao Lei ',' male ','1996-04-24');
insert into Student VALUES('06',' Leizi ',' Woman ','2000-04-24');
insert into Student VALUES(DEFAULT,' floret ',' Woman ','1996-05-24');
– VARCHAR You can't use... When inserting default
– Modify insert data
UPDATE Student set s_id='05' where s_name=' floret ';
– Delete No 06 The data of
DELETE from Student WHERE s_id='06';
– The curriculum
drop table Cuosre;
create table Cuosre(
c_id VARCHAR(20) COMMENT ' Course number ',
c_name varchar(20) UNIQUE NOT NULL COMMENT ' Course name ',
t_id varchar(20) NOT NULL COMMENT ' Teachers list id Foreign keys ',
PRIMARY key (c_id),
CONSTRAINT fk_Cuosre_Teacher foreign key (t_id) references Teacher(t_id)
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
select * from Cuosre;
select * from Sorce;
select * from Teacher;
insert into Cuosre VALUES('01',' Senior one ','01');
insert into Cuosre VALUES('02',' English ','02');
insert into Cuosre VALUES('03',' Politics ','03');
– Teachers list
drop table Teacher;
create table Teacher(
t_id VARCHAR(20) primary key comment ' Teacher number ',
t_name VARCHAR(20) UNIQUE not null comment ' Teacher's name '
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
select * from Sorce;
select * from Teacher;
insert into Teacher VALUES('01',' Sun Feng ');
insert into Teacher VALUES('02',' Li Yun ');
insert into Teacher VALUES('03',' Zhou Mei ');
insert into Teacher VALUES('04',' Wu Lan ');
– League tables
drop table Sorce;
create table Sorce(
s_id VARCHAR(20) comment ' Grade sheet No ',
c_id VARCHAR(20) comment ' Foreign keys 、 Schedule number ',
-- Combined primary key
s_sroce int(3),
-- Set the combined primary key
primary key (s_id,c_id),
-- Single foreign key Note that the specified table corresponds to
CONSTRAINT fk_Sorce_Cuosre foreign key (c_id) references Cuosre(c_id)
)ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
select * from Sorce;
insert into Sorce VALUES('01','01','99');
insert into Sorce VALUES('01','02','89');
insert into Sorce VALUES('01','03','99');
insert into Sorce VALUES('02','01','89');
insert into Sorce VALUES('02','02','60');
insert into Sorce VALUES('02','03','50');
insert into Sorce VALUES('03','01','100');
insert into Sorce VALUES('03','02','90');
insert into Sorce VALUES('03','03','70');
insert into Sorce VALUES('04','01','79');
insert into Sorce VALUES('04','02','80');
insert into Sorce VALUES('04','03','50');
– GROUP BY Learning table data
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `employee_tbl`
-- employee_tbl The structure of the table
-- ----------------------------
DROP TABLE IF EXISTS `employee_tbl`;
CREATE TABLE `employee_tbl` (
`id` int(11) NOT NULL,
`name` char(10) NOT NULL DEFAULT '',
`date` datetime NOT NULL,
`singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT ' Login times ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `employee_tbl`
-- employee_tbl Data record of table
-- ----------------------------
BEGIN;
INSERT INTO `employee_tbl` VALUES ('1', ' Xiao Ming ', '2016-04-22 15:25:33', '1'), ('2', ' Xiao Wang ', '2016-04-20 15:25:47', '3'), ('3', ' Xiao Li ', '2016-04-19 15:26:02', '2'), ('4', ' Xiao Wang ', '2016-04-07 15:26:14', '4'), ('5', ' Xiao Ming ', '2016-04-11 15:26:40', '4'), ('6', ' Xiao Ming ', '2016-04-04 15:26:54', '2');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
set names utf8;
SELECT * FROM employee_tbl;
– List aliases for fields fild1 as ‘’
SELECT name,COUNT(*) as ' total ' from employee_tbl GROUP BY name DESC;
– WITH ROLLUP It can realize the same statistics on the basis of grouping statistics (SUM,AVG,COUNT…).
SELECT name ,sum(singin) as ' song ' FROM employee_tbl GROUP BY name with ROLLUP;
– coalesce To set a replacement for NUll The name of
SELECT coalesce(name,' total ') ,sum(singin) as singin_count FROM employee_tbl GROUP BY name with ROLLUP;
SELECT * FROM Student;
select * from Cuosre;
select * from Sorce;
select * from Teacher;
– INNER JOIN( Internal connection , Or equivalent connection ): Get the records of field matching relationship in two tables . Equivalent to where
SELECT c_id as ' Number ' ,c_name as ' Course name ' , t_name as ' teacher ' from Cuosre INNER join Teacher ON Cuosre.t_id=Teacher.t_id;
– INNER JOIN Equivalent to where
SELECT c_id as ' Number ' ,c_name as ' Course name ' , t_name as ' teacher ' from Cuosre , Teacher WHERE Cuosre.t_id=Teacher.t_id;
– LEFT JOIN( Left connection ): Get all the records in the left table , Even if the right table does not have a matching record .
SELECT c_id as ' Number ' ,c_name as ' Course name ' , t_name as ' teacher ' from Cuosre LEFT join Teacher ON Cuosre.t_id=Teacher.t_id ;
– RIGHT JOIN( The right connection ): And LEFT JOIN contrary , Used to get all records in the right table , Even if the left table does not have a matching record .
SELECT c_id as ' Number ' ,c_name as ' Course name ' , t_name as ' teacher ' from Cuosre RIGHT join Teacher ON Cuosre.t_id=Teacher.t_id;
边栏推荐
- The kth largest element in the array
- halcon之区域:多种区域(Region)生成(4)
- 生信周刊第33期
- Sqlserver is case sensitive
- Computer network knowledge summary (interview)
- Digital circuit - adder
- Template engine - FreeMarker first experience
- Endnote IEEE TRANSACTIONS ON INDUSTRIAL ELECTRONICS/TIE/TPEL 参考文献格式模板
- Daily question: the difference between threads and processes
- containerd客户端比较
猜你喜欢
Online gadget sharing (updated from time to time, current quantity: 2)
Handling of @charset UTF-8 warning problems during vite packaging and construction;
Download and install flume
SPI protocol
Case: drawing Matplotlib dynamic graph
ETCD数据库源码分析——集群通信初始化
STM32 uses SPI mode to drive TFT-LCD optimization code of hx8347 scheme
CityJSON
Modelsim simulation FFT core cannot be simulated solution (qsys)
Remote incremental synchronization artifact Rsync
随机推荐
15 `bs object Node name Node name String` get nested node content
2022年育婴员(五级)考试试题及答案
Remote incremental synchronization artifact Rsync
剑指 Offer II 096. 字符串交织
When you run the demo using the gin framework, there is an error "listen TCP: 8080: bind: an attempt was made to access a socket in a way forbidden"
JSON简介
2022资料员-通用基础(资料员)考试模拟100题及在线模拟考试
Simple deepclone
A sharp tool for information collection, Google hacker syntax
C#使用MySql进行操作
Download and install flume
远程增量同步神器rsync
Technical introduction - detailed explanation of chip manufacturing process
Technical foreword - metauniverse
Inheritance -- holy grail mode
从查询数据库性能优化谈到redis缓存-谈一谈缓存的穿透、雪崩、击穿
Comment promouvoir efficacement les produits
接口的幂等性——详细谈谈接口的幂等即解决方案
原生DOM与虚拟DOM
Return value is object type method call equals()