前言 SQL語句,即結(jié)構(gòu)化查詢語言( Query ),是一種特殊目的的編程語言,是一種數(shù)據(jù)庫查詢和程序設(shè)計(jì)語言,用于存取數(shù)據(jù)以及查詢、更新和管理關(guān)系數(shù)據(jù)庫系統(tǒng),同時(shí)也是數(shù)據(jù)庫腳本文件的擴(kuò)展名。 SQL標(biāo)準(zhǔn)規(guī)定的SQL語句分為:DDL(Data 數(shù)據(jù)定義語言)、 DML(Data 數(shù)據(jù)操作語言)、DQL(Data Query 數(shù)據(jù)查詢語言)、DCL(Data 數(shù)據(jù)控制語言)。本文將詳細(xì)介紹它們。 首先了解一下關(guān)于SQL語法的一些注意事項(xiàng):
1. SQL 語句可以單行或多行書寫,以分號(hào)結(jié)尾。
2. 可使用空格和縮進(jìn)來增強(qiáng)語句的可讀性。
3. MySQL 數(shù)據(jù)庫的 SQL 語句不區(qū)分大小寫,關(guān)鍵字建議使用大寫。
4. 3 種注釋
① 單行注釋: -- 注釋內(nèi)容 或 # 注釋內(nèi)容(mysql 特有)
② 多行注釋: /* 注釋 */
一、DDL(數(shù)據(jù)定義語言)
DDL語言:全面數(shù)據(jù)定義語言(Data ),是用來定義和管理數(shù)據(jù)對(duì)象,如數(shù)據(jù)庫,數(shù)據(jù)表等。DDL命令有(創(chuàng)建)、DROP(刪除)、ALTER(修改)。
下面用代碼給大家舉例子:
-- SQL語法不區(qū)分大小寫
-- 每一句結(jié)束的時(shí)候都要用一個(gè)分號(hào);
# 庫的操作
-- 顯示所有的庫
show databases;
-- 創(chuàng)建一個(gè)庫
-- create database 庫名;
create database ku;
-- 刪除一個(gè)庫
-- 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;
-- 查看表結(jié)構(gòu)
desc tab_teacher;
show create table tab_teacher;
-- ` 反引號(hào) 讓關(guān)鍵詞失效
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(數(shù)據(jù)操作語言)
DML語言:數(shù)據(jù)操作語言(Data ),是用于操作數(shù)據(jù)庫對(duì)象中所包含的數(shù)據(jù)。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);
-- 一次性插入多條數(shù)據(jù)
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;
-- 新建表的時(shí)候插入數(shù)據(jù)
-- 新表不能存在
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 小數(shù)據(jù) and 大數(shù)據(jù)
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(數(shù)據(jù)查詢語言)
DQL語言:數(shù)據(jù)查詢語言(Data Query ),是用于查詢數(shù)據(jù)庫數(shù)據(jù)。DQL命令有(查詢)。
下面用代碼給大家舉例子:
# DQL
-- 查詢
-- 查詢表所有行和列的數(shù)據(jù)(得到的是一張?zhí)摂M表)
-- 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;
-- 去除重復(fù) distinct
-- select distinct 字段名... from 表名;
select distinct ssex,classid,sid from student;
-- 帶條件的查詢 WHERE 子句
select * from student where ssex = '男' and classid = 1;

-- 生日 大于 1990-1-1 的學(xué)生
select * from student where birthday < '1990-1-1';
-- 模糊查詢 like
insert into student(sname)
values('張三豐'),('張三'),('張三三');
-- 張字有關(guān)的數(shù)據(jù)
-- 模糊符號(hào) % 任意多的任意字符
select * from student where sname like '%張%';
-- 姓張的人
select * from student where sname like '張%';
-- 模糊符號(hào)_ 一個(gè)任意字符
select * from student where sname like '張__';
-- 學(xué)生編號(hào)是 2,5,6,8,9,20,300,4000
-- in 在特定的范圍內(nèi)查找
select * from student where sid in (2,5,6,8,9,20,300,4000);
-- 沒有生日的學(xué)生 is 是對(duì)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;
-- 每個(gè)班有多少學(xué)生
select classid,count(sid) from student group by classid;

-- sc 每個(gè)學(xué)生的平均分
select sid,avg(score) 平均分, sum(score) 總成績,max(score) 最高分, min(score) 最低分, count(*) 次數(shù) from sc group by sid;
四、聚合函數(shù)
語法:之前我們做的查詢都是橫向查詢,它們都是根據(jù)條件一行一行的進(jìn)行判斷,而使用聚合函數(shù)查詢是縱向查詢數(shù)據(jù)庫增刪查改關(guān)鍵詞,它是對(duì)一列的值進(jìn)行計(jì)算數(shù)據(jù)庫增刪查改關(guān)鍵詞,然后返回一個(gè)結(jié)果值。聚合函數(shù)會(huì)忽略空值 NULL。
-- 統(tǒng)計(jì)個(gè)數(shù) count(字段)/字段可以寫*、常量、任意字段名/count不統(tǒng)計(jì)數(shù)據(jù)為null的個(gè)數(shù)。
-- 統(tǒng)計(jì)平均值 avg(字段)
-- 統(tǒng)計(jì)最大值 max(字段)
-- 統(tǒng)計(jì)最小值 min(字段)
-- 統(tǒng)計(jì)總和 sum(字段)
eg: count(*) 總個(gè)數(shù), sum(score) 總成績, avg(score) 平均分, max(score) 最高分, min(score) 最低分 from sc;
# 聚合函數(shù)
count(字段) -- 統(tǒng)計(jì)個(gè)數(shù)
-- 數(shù)字
avg(字段) -- 平均值
sum(字段) -- 總和
max(字段) -- 最大值
min(字段) -- 最小值
-- count 統(tǒng)計(jì)個(gè)數(shù)
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() 不統(tǒng)計(jì)null
select count(birthday) from student;
-- avg 平均值
-- 所有學(xué)生的平均分
select avg(score) from sc;
-- sum 總成績
select sum(score) from sc;
select count(*) 次數(shù), sum(score) 總成績, avg(score) 平均分, max(score) 最高分,min(score)最低分 from sc;