当前位置:网站首页>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: MyBingSchoolemployee_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;
原网站

版权声明
本文为[Mixed with bean curd and]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252353285277.html