前言 SQL語句,即結構化查詢語言( Query ),是一種特殊目的的編程語言,是一種數據庫查詢和程序設計語言,用于存取數據以及查詢、更新和管理關系數據庫系統,同時也是數據庫腳本文件的擴展名。 SQL標準規定的SQL語句分為:DDL(Data 數據定義語言)、 DML(Data 數據操作語言)、DQL(Data Query 數據查詢語言)、DCL(Data 數據控制語言)。本文將詳細介紹它們。 首先了解一下關于SQL語法的一些注意事項:
1. SQL 語句可以單行或多行書寫,以分號結尾。
2. 可使用空格和縮進來增強語句的可讀性。
3. MySQL 數據庫的 SQL 語句不區分大小寫,關鍵字建議使用大寫。
4. 3 種注釋
① 單行注釋: -- 注釋內容 或 # 注釋內容(mysql 特有)
② 多行注釋: /* 注釋 */
一、DDL(數據定義語言)
DDL語言:全面數據定義語言(Data ),是用來定義和管理數據對象,如數據庫,數據表等。DDL命令有(創建)、DROP(刪除)、ALTER(修改)。
下面用代碼給大家舉例子:
-- SQL語法不區分大小寫
-- 每一句結束的時候都要用一個分號;
# 庫的操作
-- 顯示所有的庫
show databases;
-- 創建一個庫
-- create database 庫名;
create database ku;
-- 刪除一個庫
-- drop database 庫名;
drop database ku;
-- 使用庫
-- use 庫名;
use ku;
# 表的操作
-- 查看庫中所有的表
show tables;
-- 建表
create table 表名(
字段名 類型 屬性,
字段名 類型 屬性,
....

字段名 類型 屬性
);
create table tab_teacher(
tea_name varchar(10),
tea_sex char(1),
tea_birthday datetime,
tea_money decimal(20,1)
);
show tables;
-- 查看表結構
desc tab_teacher;
show create table tab_teacher;
-- ` 反引號 讓關鍵詞失效
CREATE TABLE `tab_teacher` (
`tea_name` varchar(10) DEFAULT NULL,
`tea_sex` char(1) DEFAULT NULL,
`tea_birthday` datetime DEFAULT NULL,
`tea_money` decimal(20,1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
二、DML(數據操作語言)
DML語言:數據操作語言(Data ),是用于操作數據庫對象中所包含的數據。DML命令有(增加)、(刪除)、(修改)。
下面用代碼給大家舉例子:
# DML 語句
-- 新增
-- 語法
-- insert into 表名(字段名,字段名...字段名) values(值,值...值);
-- 日期用字符串的形式表示
insert into student(sid,sname,birthday,ssex,classid) values(9,'張三','1999-1-1','男',3);

insert into student(sid,ssex,classid) values(11,'男',2);
-- 讓主鍵自增
insert into student(sname) values("王桑");
insert into student values(default,'老王','1970-6-1','男',2);
insert into student values(null,'老王','1970-6-1','男',2);
-- 一次性插入多條數據
insert into student(sname,ssex) values('王帥帥','男'),('王靚靚','男'),('王妹妹','女');
-- 不常用的新增方式
-- 表都要存在
create table stu1(
xingming varchar(10),
ssex varchar(2)
)
-- insert into select
insert into stu1 select sname,ssex from student;
-- 新建表的時候插入數據
-- 新表不能存在
create table newstu select sname,birthday,ssex from student;
-- 修改
-- 語法
-- update 表名 set 字段名=值,字段名=值... where 子句
update stu1 set xingming = '趙雷雷';
update newstu set ssex= '女' where sname='老王';
-- 范圍
update student set ssex = '女',classid = 10 where sid >= 10 and sid <= 15;
-- between 小數據 and 大數據
update student set ssex='呵呵',classid = 20 where sid between 10 and 15;

-- 刪除
-- delete from 表名 where 子句
delete from stu1;
delete from student where sname = '老王';
-- 清空表
truncate 表名
truncate student;
三、DQL(數據查詢語言)
DQL語言:數據查詢語言(Data Query ),是用于查詢數據庫數據。DQL命令有(查詢)。
下面用代碼給大家舉例子:
# DQL
-- 查詢
-- 查詢表所有行和列的數據(得到的是一張虛擬表)
-- select * from 表名;
select * from student;
-- 查詢指定字段
-- select 字段名1,字段名2... from 表名;
select sid,sname,birthday,ssex,classid from student;
-- 字段起別名
-- select 舊字段名 as '新字段名';
select sname as '姓名', birthday '生日',ssex 性別 from student;
-- 去除重復 distinct
-- select distinct 字段名... from 表名;
select distinct ssex,classid,sid from student;
-- 帶條件的查詢 WHERE 子句
select * from student where ssex = '男' and classid = 1;

-- 生日 大于 1990-1-1 的學生
select * from student where birthday < '1990-1-1';
-- 模糊查詢 like
insert into student(sname)
values('張三豐'),('張三'),('張三三');
-- 張字有關的數據
-- 模糊符號 % 任意多的任意字符
select * from student where sname like '%張%';
-- 姓張的人
select * from student where sname like '張%';
-- 模糊符號_ 一個任意字符
select * from student where sname like '張__';
-- 學生編號是 2,5,6,8,9,20,300,4000
-- in 在特定的范圍內查找
select * from student where sid in (2,5,6,8,9,20,300,4000);
-- 沒有生日的學生 is 是對null的判斷
select * from student where birthday is null;
select * from student where birthday is not null;
# 分組
-- group by 字段
select count(1) from student where ssex = '男';
select count(1) from student where ssex = '女';
select ssex,count(sid) from student group by ssex;
-- 每個班有多少學生
select classid,count(sid) from student group by classid;

-- sc 每個學生的平均分
select sid,avg(score) 平均分, sum(score) 總成績,max(score) 最高分, min(score) 最低分, count(*) 次數 from sc group by sid;
四、聚合函數
語法:之前我們做的查詢都是橫向查詢,它們都是根據條件一行一行的進行判斷,而使用聚合函數查詢是縱向查詢數據庫增刪查改關鍵詞,它是對一列的值進行計算數據庫增刪查改關鍵詞,然后返回一個結果值。聚合函數會忽略空值 NULL。
-- 統計個數 count(字段)/字段可以寫*、常量、任意字段名/count不統計數據為null的個數。
-- 統計平均值 avg(字段)
-- 統計最大值 max(字段)
-- 統計最小值 min(字段)
-- 統計總和 sum(字段)
eg: count(*) 總個數, sum(score) 總成績, avg(score) 平均分, max(score) 最高分, min(score) 最低分 from sc;
# 聚合函數
count(字段) -- 統計個數
-- 數字
avg(字段) -- 平均值
sum(字段) -- 總和
max(字段) -- 最大值
min(字段) -- 最小值
-- count 統計個數
select count(*) from student where ssex = '男';
select count(sname) from student where ssex = '男';
select count(1) from student where ssex = '男';
select count('a') from student where ssex = '男';
-- count() 不統計null
select count(birthday) from student;
-- avg 平均值
-- 所有學生的平均分
select avg(score) from sc;
-- sum 總成績
select sum(score) from sc;
select count(*) 次數, sum(score) 總成績, avg(score) 平均分, max(score) 最高分,min(score)最低分 from sc;