SQL零基础入门必知必会!
大数据DT
共 3554字,需浏览 8分钟
·
2021-10-17 09:59
导读:SQL语言有40多年的历史,从它被应用至今几乎无处不在。我们消费的每一笔支付记录,收集的每一条用户信息,发出去的每一条消息,都会使用数据库或与其相关的产品来存储,而操纵数据库的语言正是 SQL !
SQL 指结构化查询语言 SQL 使我们有能力访问数据库 SQL 是一种 ANSI 的标准计算机语言
数据查询语言(DQL: Data Query Language) 数据操纵语言(DML:Data Manipulation Language)
SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 SQL 可更新数据库中的数据 SQL 可从数据库删除记录 SQL 可创建新数据库 SQL 可在数据库中创建新表 SQL 可在数据库中创建存储过程 SQL 可在数据库中创建视图 SQL 可以设置表、存储过程和视图的权限
MS SQL Server IBM DB2 Oracle MySQL Microsoft Access
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | Oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
);
数据类型 | 描述 |
---|---|
integer(size),int(size),smallint(size),tinyint(size) | 仅容纳整数、在括号内规定数字的最大位数 |
decimal(size,d),numeric(size,d) | 容纳带有小数的数字、"size" 规定数字的最大位数、"d" 规定小数点右侧的最大位数 |
char(size) | 容纳固定长度的字符串(可容纳字母、数字以及特殊字符)、在括号中规定字符串的长度 |
varchar(size) | 容纳可变长度的字符串(可容纳字母、数字以及特殊的字符)、在括号中规定字符串的最大长度 |
date(yyyymmdd) | 容纳日期 |
CREATE TABLE Persons
(
Id_P int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
INSERT INTO 表名称 VALUES (值1, 值2,....);
我们也可以指定所要插入数据的列:
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....);
INSERT INTO Persons VALUES (1, 'Gates', 'Bill', 'Xuanwumen 10', 'Beijing');
INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');
3. SELECT – 查询数据
SELECT * FROM 表名称;
SELECT 列名称 FROM 表名称;
SELECT * FROM Persons;
SELECT LastName,FirstName FROM Persons;
4. DISTINCT – 去除重复值
SELECT DISTINCT 列名称 FROM 表名称;
SELECT LASTNAME FROM Persons;
SELECT DISTINCT LASTNAME FROM Persons;
5. WHERE – 条件过滤
SELECT 列名称 FROM 表名称 WHERE 列 运算符 值;
操作符 | 描述 |
---|---|
= | 等于 |
<> | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
BETWEEN | 在某个范围内 |
LIKE | 搜索某种模式 |
SELECT * FROM Persons WHERE City='Beijing';
6. AND & OR – 运算符
如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。 如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。
SELECT * FROM 表名称 WHERE 列 运算符 值 AND 列 运算符 值;
SELECT * FROM 表名称 WHERE 列 运算符 值 OR 列 运算符 值;
INSERT INTO Persons VALUES (2, 'Adams', 'John', 'Oxford Street', 'London');
INSERT INTO Persons VALUES (3, 'Bush', 'George', 'Fifth Avenue', 'New York');
INSERT INTO Persons VALUES (4, 'Carter', 'Thomas', 'Changan Street', 'Beijing');
INSERT INTO Persons VALUES (5, 'Carter', 'William', 'Xuanwumen 10', 'Beijing');
SELECT * FROM Persons;
SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter';
SELECT * FROM Persons WHERE firstname='Thomas' OR lastname='Carter';
SELECT * FROM Persons WHERE (FirstName='Thomas' OR FirstName='William') AND LastName='Carter';
7. ORDER BY – 排序
SELECT * FROM 表名称 ORDER BY 列1,列2 DESC;
SELECT * FROM Persons ORDER BY LASTNAME;
SELECT * FROM Persons ORDER BY ID_P,LASTNAME;
SELECT * FROM Persons ORDER BY ID_P DESC;
8. UPDATE – 更新数据
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值;
UPDATE Persons SET FirstName = 'Fred' WHERE LastName = 'Wilson';
UPDATE Persons SET ID_P = 6,city= 'London' WHERE LastName = 'Wilson';
9. DELETE – 删除数据
DELETE FROM 表名称 WHERE 列名称 = 值;
DELETE FROM Persons WHERE LastName = 'Wilson';
DELETE FROM table_name;
10. TRUNCATE TABLE – 清除表数据
TRUNCATE TABLE 表名称;
TRUNCATE TABLE persons;
11. DROP TABLE – 删除表
DROP TABLE 表名称;
drop table persons;
03 SQL 高级言语学习
1. LIKE – 查找类似值
SELECT 列名/(*) FROM 表名称 WHERE 列名称 LIKE 值;
INSERT INTO Persons VALUES (1, 'Gates', 'Bill', 'Xuanwumen 10', 'Beijing');
INSERT INTO Persons VALUES (2, 'Adams', 'John', 'Oxford Street', 'London');
INSERT INTO Persons VALUES (3, 'Bush', 'George', 'Fifth Avenue', 'New York');
INSERT INTO Persons VALUES (4, 'Carter', 'Thomas', 'Changan Street', 'Beijing');
INSERT INTO Persons VALUES (5, 'Carter', 'William', 'Xuanwumen 10', 'Beijing');
select * from persons;
SELECT * FROM Persons WHERE City LIKE 'N%';
SELECT * FROM Persons WHERE City LIKE '%g';
SELECT * FROM Persons WHERE City LIKE '%on%';
SELECT * FROM Persons WHERE City NOT LIKE '%on%';
2. IN – 锁定多个值
SELECT 列名/(*) FROM 表名称 WHERE 列名称 IN (值1,值2,值3);
SELECT * FROM Persons WHERE LastName IN ('Adams','Carter');
3. BETWEEN – 选取区间数据
SELECT 列名/(*) FROM 表名称 WHERE 列名称 BETWEEN 值1 AND 值2;
SELECT * FROM Persons WHERE LastName BETWEEN 'Adams' AND 'Carter';
SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Adams' AND 'Carter';
某些数据库会列出介于 "Adams" 和 "Carter" 之间的人,但不包括 "Adams" 和 "Carter" ;某些数据库会列出介于 "Adams" 和 "Carter" 之间并包括 "Adams" 和 "Carter" 的人;而另一些数据库会列出介于 "Adams" 和 "Carter" 之间的人,包括 "Adams" ,但不包括 "Carter" 。
4. AS – 别名
SELECT 列名称/(*) FROM 表名称 AS 别名;
SELECT 列名称 as 别名 FROM 表名称;
SELECT p.LastName, p.FirstName
FROM Persons p
WHERE p.LastName='Adams' AND p.FirstName='John';
SELECT LastName "Family", FirstName "Name" FROM Persons;
5. JOIN – 多表关联
create table orders (id_o number,orderno number,id_p number);
insert into orders values(1,11111,1);
insert into orders values(2,22222,2);
insert into orders values(3,33333,3);
insert into orders values(4,44444,4);
insert into orders values(6,66666,6);
select * from orders;
select * from persons p,orders o where p.id_p=o.id_p;
select 列名
from 表A
INNER|LEFT|RIGHT|FULL JOIN 表B
ON 表A主键列 = 表B外键列;
JOIN: 如果表中有至少一个匹配,则返回行 INNER JOIN: 内部连接,返回两表中匹配的行 LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行 RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行 FULL JOIN: 只要其中一个表中存在匹配,就返回行
SELECT p.LastName, p.FirstName, o.OrderNo
FROM Persons p
INNER JOIN Orders o
ON p.Id_P = o.Id_P
ORDER BY p.LastName DESC;
6. UNION – 合并结果集
SELECT 列名 FROM 表A
UNION
SELECT 列名 FROM 表B;
SELECT 列名 FROM 表A
UNION ALL
SELECT 列名 FROM 表B;
CREATE TABLE Persons_b
(
Id_P int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
INSERT INTO Persons_b VALUES (1, 'Bill', 'Gates', 'Xuanwumen 10', 'Londo');
INSERT INTO Persons_b VALUES (2, 'John', 'Adams', 'Oxford Street', 'nBeijing');
INSERT INTO Persons_b VALUES (3, 'George', 'Bush', 'Fifth Avenue', 'Beijing');
INSERT INTO Persons_b VALUES (4, 'Thomas', 'Carter', 'Changan Street', 'New York');
INSERT INTO Persons_b VALUES (5, 'William', 'Carter', 'Xuanwumen 10', 'Beijing');
select * from persons_b;
select * from persons
UNION
select * from persons_b;
7. NOT NULL – 非空
CREATE TABLE 表
(
列 int NOT NULL
);
create table lucifer (id number not null);
insert into lucifer values (NULL);
select * from persons where FirstName is not null;
select * from persons where FirstName is null;
8. VIEW – 视图
CREATE VIEW 视图名 AS
SELECT 列名
FROM 表名
WHERE 查询条件;
create view persons_beijing as
select * from persons where city='Beijing';
CREATE OR REPLACE VIEW 视图名 AS
SELECT 列名
FROM 表名
WHERE 查询条件;
create or replace view persons_beijing as
select * from persons where lastname='Gates';
drop view persons_beijing;
04 SQL 常用函数学习
SELECT function(列) FROM 表;
1. AVG – 平均值
SELECT AVG(列名) FROM 表名;
select avg(orderno) from orders;
select * from orders where orderno < (select avg(orderno) from orders);
2. COUNT – 汇总行数
COUNT(*) :返回表中的记录数。 COUNT(DISTINCT 列名) :返回指定列的不同值的数目。 COUNT(列名) :返回指定列的值的数目(NULL 不计入)。
SELECT COUNT(*) FROM 表名;
SELECT COUNT(DISTINCT 列名) FROM 表名;
SELECT COUNT(列名) FROM 表名;
select count(*) from persons;
select count(distinct city) from persons;
select count(city) from persons;
3. MAX – 最大值
SELECT MAX(列名) FROM 表名;
select max(orderno) from orders;
4. MIN – 最小值
SELECT MIN(列名) FROM 表名;
select min(orderno) from orders;
5. SUM – 求和
SELECT SUM(列名) FROM 表名;
select sum(orderno) from orders;
6. GROUP BY – 分组
SELECT 列名A, 统计函数(列名B)
FROM 表名
WHERE 查询条件
GROUP BY 列名A;
select lastname,count(city) from persons
where city='Beijing'
group by lastname;
7. HAVING – 句尾连接
SELECT 列名A, 统计函数(列名B)
FROM table_name
WHERE 查询条件
GROUP BY 列名A
HAVING 统计函数(列名B) 查询条件;
select lastname,count(city) from persons
where city='Beijing'
group by lastname
having count(city) > 1;
8. UCASE/UPPER – 大写
select upper(列名) from 表名;
select upper(lastname),firstname from persons;
9. LCASE/LOWER – 小写
select lower(列名) from 表名;
select lower(lastname),firstname from persons;
10. LEN/LENGTH – 获取长度
select length(列名) from 表名;
select length(lastname),lastname from persons;
11. ROUND – 数值取舍
select round(列名,精度) from 表名;
select round(1.1314,2) from dual;
select round(1.1351,2) from dual;
select round(1.1351,0) from dual;
select round(1.56,0) from dual;
12. NOW/SYSDATE – 当前时间
select sysdate from 表名;
select sysdate from dual;
延伸阅读👇
《DBA攻坚指南:左手Oracle,右手MySQL》
干货直达👇
评论