【Database】--The most commonly used SQL statements and keywords

Table of contents

Data Definition Language (DDL)

1. Operations on the database

2. Operation on the data table

Data Manipulation Language (DML)

1. Add insert into

2. Delete delete

3. Modify update

Data Query Language (DQL)

1. Query select

2. Keywords

1.between ... and ... (between ....)

2.in, exists

3.check

4.pirmary key (primary key)

5.foreign key (foreign key)

6.null ,not null

7.distinct

8.having

​9.as

3. Sorting query order by

Four. Group query group by

5. Aggregation functions

6. Fuzzy query like


Through a semester of learning about the principles and applications of professional databases, today I would like to summarize the SQL statements. The focus is to provide a variety of different statements for the sentences to be implemented with SQL statements. I hope it will be helpful to you! ! Especially for beginners, after reading it, your database language programming will definitely be greatly improved! ! !

Data Definition Language (DDL)

1. Operations on the database

--1.1创建数据库
create database MyDatabase;

--1.2判断数据库是否已经存在,若存在则不创建,不存在创建,语句更为严谨
create database if not exists MyDatabase ;

--2.查看所有创建的数据库
show databases;

--3.查看指定数据库的·定义信息
show create database MyDatabase;

--4删除数据库
drop database MyDatabase;

2. Operation on the data table

--1.创建数据表
create table Student(
	SNO char(10)primary key,     --[primary key] 为设置主键
	[Name] char(20)not null,     -- [not null] 为属性列的值不能为空
	Sex char(2)  check(Sex = '男' or Sex = '女'),--[check] 起限制,约束作用,例如性别男和女,分数0到100分
	birthday char(20),
	Major char(20),
	Dept char(20),
	Number char(10))

--2.查看表的结构
desc 表名;

--3.修改表名
alter table 表名 rename to 新的表名;

--4.添加一列
alter table 表名 add 列名 数据的类型; --(注意,此处所添加的咧属性是默认可以为空值的null)

--5.添加多列
alter table 表名 add 列名 数据的类型,
                     列名 数据的类型,
					 列名 数据的类型;

--6.删除一列
alter table 表名 drop 列名;

--7.删除多列
alter table 表名 drop 列名,
                      列名,
					  列名,
					  列名;

----8.删除表
drop table 表名;

--9.判断表是否已经存在,若存在才删除,语句更为严谨
drop table  if exists 表名;

Data Manipulation Language (DML)

1. Add insert into

--1.规范书写
insert into 表名(列名1,列名2,...) values(值1,值2,,...);


--2.省略列名时,默认所有列都添加,下面两个句子表达意思相同
insert into 表名 values(值1,值2,,...,值n);--省略列名
insert into 表名(列名1,列名2,...,列名n) values(值1,值2,,...,值n);--此为写全了所有列名


--3.插入部分数据时
insert into 表名(列名2,列名3,,列明5) values(值2,值3,值5);


--4.插入多行数据时 两种写法 注意标点符号 明显第二种较为简单
insert into 表名()values();
                  values();
				  values();

insert into 表名()values(),
                        (),
						(),
						();

2. Delete delete

--1.规范书写
--delete 作用仅是删除表中的数据,对表体,列属性名无任何影响

delete from 表名 where 列名 = 值;

--2.删除表中所有数据 这里表体仍存在,是空表,表仍能查询到
delete from 表名 ;--如果不是想删除所有数据,切记加where,不然表中数据全删除完了,小编就吃过亏,慎用 

--3.先删除表,再创建一张一样的表
truncate table 表名;

3. Modify update

--规范书写
update 表名 set 列名 = 值 where 列名 = 值;
--代码中第一个【列名 = 值】 为想要修改的所在列的值
--代码中第二个【列名 = 值】 的列名为具有标识的作用如主键,其值唯一。非主键也行,看具体修改的目的

Data Query Language (DQL)

1. Query select

--查询表中所有数据 (行列无限制,全部输出)
select * from 表名;

--查询表中某几列数据(行全部输出)
select 列名1,列名3 from 表名;

--查询特定列名的几行数据 (列全部输出)
select * from 表名 where 列名 = 值;

--查询特定列名的几行数据 (行列都有限制)
select 列名2,列名4 from 表名 where 列名 = 值;

2. Keywords

1. between ... and ... (between ....)

between value and value: Find values ​​between.
not between value and value: Look for values ​​that are not between. These values ​​can be numbers, text, or dates.

--查询年龄大于等于18,小于等于20的学生(三种写法,结果一样)
SELECT * FROM student WHERE Age>=20 AND Age<=30;
SELECT * FROM student WHERE Age<=20 && Age<=30;
SELECT * FROM student WHERE BETWEEN 20 AND 30;

--查询年龄不在18到20的学生(三种写法)
SELECT * FROM student WHERE Age<20 AND Age>30;
SELECT * FROM student WHERE Age<20 && Age>30;
SELECT * FROM student WHERE NOT BETWEEN 20 AND 30;

2.in, exists

1. When in is querying, first query the table of the subquery, then make a Cartesian product of the inner table and the outer table ,
and then filter according to the conditions.

2. When exists is queried, the outer loop will be executed first, and each row of the result returned by the outer loop will be carried to the inner loop for execution ( at this time, note that the inner layer is also cyclically queried ).

-查询选修了课程的学生全部信息  S(学生表)  SC(学生选课表)
SELECT *
FROM S
WHERE SNO IN(SELECT SNO
             FROM SC)

SELECT * 
FROM S
WHERE EXISTS(SELECT SNO  
              FROM SC
			  WHERE S.SNO = SC.SNO)

--查询没有选修任何课程的学生全部信息  S(学生表)  SC(学生选课表)
SELECT * 
FROM S
WHERE SNO NOT IN(SELECT SNO
                 FROM SC
				 WHERE S.SNO = SC.SNO)

SELECT *
FROM S
WHERE NOT EXISTS(SELECT SNO
                 FROM SC
				 WHERE S.SNO = SC.SNO)

3.check

CHECK constraints: Used to limit the range of values ​​in a column.

In some cases, we need the input of the field in the specified range,
for example: gender can only be entered as 'male' or 'female', the balance can only be greater than 0, and the score is between 0 and 100.
At this time, you can use CHECK constraints to standardize the data.

Next, let's see where it can be used. 

1. When creating a table

8762f730abd54bcfa16adf0c05958c11.png

2. When the table already exists, you want to modify it.

f9f543d3a6de4c8c897cb1b9229950e6.png

4. pirmary key (primary key)

pirmary key (primary key),

Used to uniquely identify each piece of data in the table.

Cannot be repeated, cannot be empty.

A table can have only one primary key.

Next, let's see where it can be used.

1. When creating a table

The first

b384d4e22c634b718a051a32b278afbb.png

the second

0ae6fd0ed0f342f395240e2453cad8e6.png

2. The table already exists and has no primary key. When you want to add a primary key

1af97cdf66804a1b992001ec0ba1d229.png

3. The table already exists and has a primary key. When you want to delete the primary key

42073616508e4e408694d67c95f7e9cc.png

The constraint name in the above code is here, right click to copy

3e8dbf6229a843949b5ba4d576d4c873.png

5. foreign key (foreign key)

Foreign key: External key, a field (non-primary key) of a table points to the primary key of another table, then the field is called a foreign key.

The table where the foreign key is located is called the child table (attached table); the table where the primary key pointed to by the foreign key is called the parent table (main table).

1. When creating a table

 a57a5dd79aaa41cd8f70559ea0c364c9.png

2. The table already exists, when you want to add a foreign key

Note: The fields in the master-slave table must have the same data type, character length and constraints! ! !

e2ce10f6776b4aee817c8e4f6c89f5ba.png

3. The table already exists, when you want to delete the foreign key

Deleting foreign keys is the same as deleting primary keys

11257bac3a6848f590a30f3ec0cc5ee2.png

6.null ,not null

  Null and not null  can be called the attributes of the data type, which can modify the restricted column attributes. The null value is not equal to the null value. The null value does not occupy storage space, and null occupies storage space.

is null  and is not null  are operators, (note that it cannot be written as =null or !=null) when querying a field is empty, use is null,

Use is not null when it is not empty.

Next, let's see where it can be used.

7.distinct

distinct deduplication, the main function of the distinct keyword is to filter the duplicate data in one or more fields in the database table, and only return one of the data to the user. distinct can only be used in select.

8.having

The HAVING statement is usually used in conjunction with the GROUP BY clause and the aggregate functions COUNT, AVG, SUM, MAX, and MIN statements to filter the recordset returned by the GROUP BY statement. Usually, it is equivalent to WHERE after GROUP BY.

​9.as

alias

select 字段 as 别名 from 表名
select SNO as 学号 from Student

3. Sorting query order by

order by is used for sorting, and the default is to sort in ascending order.

order by 列名 desc --[desc] 表示降序
order by 列名 asc  --[asc] 表示升序


--查找所有学生的全部信息,按年龄降序排列
select *
from S
order by Age desc

Four. Group query group by

Usage: used for grouping, generally used for grouping and aggregation with aggregation functions (summation, statistics, etc.).

--按照年龄分组并计算每个年龄段的平均成绩
SELECT Age, AVG(Score) 
FROM students
GROUP BY Age;


--按照性别和年龄分组并计算每个年龄段不同性别的平均成绩
SELECT Gender, Age, AVG(Score)
FROM students
GROUP BY Gender, Age;

5. Aggregation functions

Calculate a column of data as a whole, usually combined with the group query function group by .

1.count() 计算列数
2.max()  计算列最大值
3.min()  计算列最小值
4.sum()  对列进行求和
5.avg()  对列计算平均数

6. Fuzzy query like

like fuzzy query, supports % and underscore matching, % matches multiple characters, _underscore: any character.

Example:

--1、查询名字中含有李的学生信息
select * from student where sname like ‘%李%’;

--2、查询名字以刘开头的学生信息
select * from student where sname like ‘刘%’;

--3、查询名字以玮结尾的学生信息
select * from student where sname like ‘%玮’;

--4、查询名字中第二个字为雨的学生信息
select * from student where sname like ‘_雨%’;

--5、查询名字中第三个字为思的学生信息
select * from student where sname like ‘__思%’;

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/131064951