SQL 数据库
温馨提示:本篇文章较长,请注意身体健康,不要爆肝
引言
本教程系列是根据菜鸟教程所改编
本教程适合已经有基础,仅需要速过的技术人员浏览
如果需要更细教程,请打开下方菜鸟教程网址
菜鸟教程网址:https://www.runoob/sql/sql-tutorial.html
目录
- SQL select top
- SQL like
- SQL 通配符
- SQL in
- SQL between
- SQL 别名
- SQL 连接(join)
- SQL inner join
- SQL left join
- SQL right join
- SQL full outer join
- SQL union
- SQL select into
- SQL insert into select
- SQL create database
- SQL create table
- SQL 约束
- SQL not null
- SQL unique
- SQL primary key
- SQL foreign key
- SQL check
- SQL default
- SQL create index
- SQL drop
- SQL alter
- SQL Auto Increment
- SQL 视图
- SQL 日期
- SQL null 值
- SQL null 函数
- SQL 通用数据类型
- SQL DB 数据类型
正文
1. SQL select top
SQL select top 规定要返回的记录的数目,适用于处理数千条记录的大型表
MySQL 数据库使用 limit ,Oracle 数据库使用 rownum
SQL Server / MS Access 语法
select top 5 * from tables;
select top 5 * from tables order by id desc; 通过降序来展示后5行
MySQL 语法
select * from tables limit 5;
Oracle 语法
select * from tables where rownum <=5;
2. SQL like
like 操作符用于在 where 子句中搜索列中的指定模式。
语法:
查询 name 以字母 “G” 开头的所有客户:
select * form tables where name like ‘G%’;
查询 name 不以字母 “G” 开头的所有客户:
select * form tables where name not like ‘G%’;
3. SQL 通配符
like 通配符:
通配符 | 描述 |
---|---|
% | 替代 0 个或多个字符 |
_ | 替代一个字符 |
[ ] | 字符列中的任何单一字符 |
[^] | 不在字符列中的任何单一字符 |
regexp 正则表达式:
通配符 | 描述 |
---|---|
^ | 匹配字符串的开始位置 |
$ | 匹配文本的结束字符 |
. | 英文下的点,它匹配任何单个字符,包括回车、换行等 |
* | 匹配零个或多个在他前面的字符 |
[^] | 匹配 0 个或多个字符 |
+ | 匹配 前面的 1 次或多次字符 |
? | 匹配 0 次或 1 次 |
竖杠 | 或,匹配前者或后者 |
(abc)* | 匹配任意多个abc |
{n} | 前面的元素的n个实例 |
{n,m} | n到m个实例前面的元素 |
[…] | 匹配字符集合中的任何一个字符 |
[^…] | 匹配不在括号中的任何字符 |
字符串 | 匹配包含指定的字符串 |
4. SQL in
in 操作符允许您在 where 子句中规定多个值。
语法:
select * from tables where name in (‘张三’,‘李四’);
5. SQL between
between 操作符用于选取介于两个值之间的数据范围内的值。
between 可以传入的数据类型有 字符 文本 日期
语法:
select * from tables where id between 1 and 10;
not between 取反,不在两者之间
语法:
select * from tables where id not between 1 and 10;
带有 in 的 between,选取介于两者之间并且在in里面的数据
select * from tables where (id between 1 and 20) and age in (18,19,20);
6. SQL 别名
通过使用 SQL,可以为表名称或列名称指定别名。
目的:为了让列名称的可读性更强。
列别名 语法:
select name as n from tables;
表别名 语法:
select * from table_name as t_n;
在下面的情况下,使用别名很有用:
-
List item
-
在查询中涉及超过一个表
-
在查询中使用了函数
-
列名称很长或者可读性差
-
需要把两个列或者多个列结合在一起
7. SQL join (连接)
SQL join 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
最常见的 join 类型:SQL inner join(简单的 join)。 SQL inner join 从多个表中返回满足 join 条件的所有行。
8. SQL inner join (内连接)
inner join 关键字在表中至少一个匹配时返回行。
语法:
select * from 左表 inner join 右表 on 左表.ID=右表.ID;
或:
select * from 左表 join 右表 on 左表.ID=右表.ID;
inner join 和 join 是相同的
9. SQL left join (左连接)
left join 关键字从左表返回所有的行,右表中没有匹配到的用 null 显示。
语法:
select * from 左表 left join 右表 on 左表.ID=右表.ID;
或
select * from 左表 left outer join 右表 on 左表.ID=右表.ID;
10. SQL right join (右连接)
right join 关键字从右表返回所有的行,左表中没有匹配到的用 null 显示。
语法:
select * from 左表 right join 右表 on 左表.ID=右表.ID;
或
select * from 左表 right outer join 右表 on 左表.ID=右表.ID;
11. SQL full outer join
full outer join 关键字只要左表和右表其中一个表中存在匹配,则返回行
full outer join 关键字结合了 left join 和 right join 的结果
语法:
select * from 左表 full outer join 右表 on 左表.ID=右表.ID;
12. SQL union
union 操作符合并两个或多个 select 语句的结果。
union 内部的每个 select 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 select 语句中的列的顺序必须相同。
语法:
select * from 表1 union select * from 表2;
默认地,union 操作符选取不同的值。如果允许重复的值,请使用 union all。
select * from 表1 union all select * from 表2;
union 可以用 where 进行条件筛选
13. SQL select into
select into 语句从一个表复制数据到另一个新表中
语法:
MYSQL: create table 新表 as select * from 旧表;
SQL: select * into 新表 from 旧表;
14. SQL insert into select
insert into select 语句从一个表复制数据插入到另一个已存在的表中
语法:
insert into 表2 select * from 表1;
15. SQL create database
create database 创建数据库
语法:
create database 数据库名;
16. SQL create table
create table 创建数据库中的表
语法:
create table 表名;
create table 表名 (列1 列1数据类型,列2 列2数据类型,…);
17. SQL 约束
SQL 约束用于规定表中的数据规则
语法:
create table 表名(列名 列数据类型 列约束类型,…);
18. SQL not null (约束-不为空)
默认情况下,表的列允许为 null
当设置了not null 时,表的列不允许为 null
语法:
建表时: create table 表名(列名 列数据类型 not null,…);
建表后: alter table 表名 modify 列名 列数据类型 not null;
删除约束:alter table 表名 modify 列名 列数据类型 null;
19. SQL unique (约束-唯一)
unique 约束规定了列的唯一性
语法:
建表时:
MYSQL:
create table people(ID int not null,…, unique (ID));
SQL Server / Oracle / MS Access:
create table people(ID int not null unique,…);
如果要定义多个 unique 约束:
MySQL / SQL Server / Oracle / MS Access:create table people(ID int not null,name varchar(255) null,…,constratnt uc_personID unique (ID,name));
建表后:
MySQL / SQL Server / Oracle / MS Access:
单列约束:alter table people add unique (ID);
多列约束:alter table people add constratnt uc_personID unique (ID,name);
删除约束:
MySQL:
alter table people drop index uc_PersonID;
SQL Server / Oracle / MS Access;
alter table people drop constratnt uc_PersonID;
20. SQL primary key (约束-主键)
- primary key 约束用于标识数据库中的每条记录
- 主键非空且唯一
- 每个表都应该有且仅有一个主键
语法:
建表时:
MYSQL:
create table people(ID int ,…,primary key (ID));
SQL Server / Oracle / MS Access:
create table people(ID int primary key,…);
组合主键
SQL Server / Oracle / MS Access:
create table people(ID int ,name varchar(255),…,constratnt pk_personID primary key (ID,name));
注释:由于主键只能有一个,所以 ID 和 name 组合成了一个主键 pk_personID
建表后:
MySQL / SQL Server / Oracle / MS Access:
单列主键:alter table people add primary key (ID);
组合主键:alter table people add constratnt pk_personID primary key (ID,name);
删除主键:
MYSQL
alter table people drop primary key;
SQL Server / Oracle / MS Access:
alter table people drop constratnt pk_personID;
21. SQL foreign key (约束-外键)
foreign key 外键约束 用于从表与主表的关联约束
语法:
建表时:
MYSQL:
create table people(ID int not null,name varchar(255),primary key(ID),foreign key(ID) references 主表(ID));
SQL Server / Oracle / MS Access:
create table people(ID int not null primary key foreign key references 主表(ID),name varchar(255));
如需命名外键约束,并定义多个列的外键约束:
MySQL / SQL Server / Oracle / MS Access:create table people(ID int,primary key (ID),constratnt fk_PerOrders foreign key (ID) references 主表(ID),name varchar(255));
建表后:
MySQL / SQL Server / Oracle / MS Access:
alter table people add foreign key (ID) references 主表(ID);
如需命名外键约束,并定义多个列的外键约束:
alter table people add constratnt fk_perders foreign key (ID) references 主表(ID);
删除外键:
MySQL:
alter table people drop foreign key fk_PerOrders;
SQL Server / Oracle / MS Access:
alter table people drop constratnt fk_PerOrders;
22. SQL check (约束-限制列值)
check 约束用于限制列中值的范围
语法:
建表时:
MYSQL:
create table people(ID int not null,…,check (ID>0));
SQL Server / Oracle / MS Access:
create table people(ID int not null check (ID>0),…);
如需命名 checek 约束,并定义多个列的 check 约束:
MySQL / SQL Server / Oracle / MS Access:create table people(ID int not null,name varchar(255),…,constratnt chk_person check (ID>0 and name=‘sucheng’));
建表后:
MySQL / SQL Server / Oracle / MS Access:
alter table people add check (ID>0);
如需命名 checek 约束,并定义多个列的 check 约束:
MySQL / SQL Server / Oracle / MS Access:alter table people add constratnt chk_person check (ID>0 and name=‘sucheng’));
删除外键:
MySQL:
alter table poeple drop check chk_person;
SQL Server / Oracle / MS Access:
alter table poeple drop constratnt chk_person;
23. SQL default (约束-默认值)
default 约束用于向列中插入默认值。
语法:
建表时:
My SQL / SQL Server / Oracle / MS Access:
create table people(ID int not null,sex varchar(2) default ‘man’);
通过使用类似 getdate() 这样的函数,default 约束也可以用于插入系统值:
create table people(ID int not null,sex varchar(2) data default getdate());
建表后:
MYSQL:
alter table people alter sex set default ‘man’;
SQL Server / MS Access:
alter table people add constratnt default_name default ‘man’ for sex;
default_name 为约束名称Oracle:
alter table people modify sex set default ‘man’;
删除默认值:
MYSQL:
alter table people alter sex drop default;
SQL Server / Oracle / MS Access:
alter table people alter column sex drop default;
24. SQL create index
create index 创建索引
索引:
- 更加快速高效地查询数据
- 用户无法看到索引,它们只能被用来加速搜索/查询
- 索引应该在常常被搜索的列(以及表)上面被创建
create index 在表上创建一个简单的索引,允许使用重复值。
语法:
create index 索引名称 on 表名 (列名);
create unique index 在表上创建一个唯一的索引,不允许使用重复的值:唯一索引意味着两个行不能拥有相同的索引值。
语法:
create unique index 索引名称 on 表名 (列名);
注释:用于创建索引的语法在不同的数据库中不一样。因此,检查您的数据库中创建索引的语法
25. SQL drop
drop index 删除索引
MS Access 语法:
drop index 索引名 on 表名;
MS SQL Server 语法:
drop index 表名.索引名
DB2/Oracle 语法:
drop index 索引名;
MYSQL 语法:
alter table 表名 drop index 索引名;
drop table 删除表
语法:
drop table 表名;
drop database 删除数据库
语法:
drop database 数据库名;
truncate table 删除表数据
语法:
truncate table 表名;
26. SQL alter
alter table 语句:添加 删除 修改列
添加列 语法:
alter table 表名 add 列名 数据类型;
删除列 语法:
alter table 表名 drop column 列名;
修改表中列的数据类型
SQL Server / MS Access 语法:
alter table 表名 alter column 列名 数据类型;
My SQL / Oracle 语法:
alter table 表名 modify 列名 数据类型;
27. SQL Auto Increment
Auto Increment 自增
MySQL 语法:
create table people(ID int not null auto_increment,primary key (ID));
SQL Server 语法:
create table people(ID int identity(1,1) primary key);
Access 语法:
create table people(ID integer primary key autoincrement(1,1));
Oracle 语法:
create sequence seq_person minvalue 1 start with 1 increment by 1 cache 10;
28. SQL 视图
可视化的表
创建视图:
create view 语法:
create view 视图名称 as select 列名 from 表名 where condition;
更新视图:
create or replace view 语法:
create or replace view 视图名称 as select 列名 from 表名 where condition;
删除视图
drop view 语法:
drop view 视图名称;
29. SQL 日期
MySQL date 函数:
函数 | 描述 |
---|---|
now() | 返回当前的日期和时间 |
curdate() | 返回当前的日期 |
curtime() | 返回当前的时间 |
date() | 提取日期或日期/时间表达式的日期部分 |
extract() | 返回日期/时间的单独部分 |
date_add() | 向日期添加指定的时间间隔 |
date_sub() | 从日期减去指定的时间间隔 |
datediff() | 返回两个日期之间的天数 |
date_format() | 用不同的格式显示日期/时间 |
SQL Server date 函数
函数 | 描述 |
---|---|
getdate() | 返回当前的日期和时间 |
datepart() | 返回日期/时间的单独部分 |
dateadd() | 在日期中添加或减去指定的时间间隔 |
datediff() | 返回两个日期之间的时间 |
convert() | 用不同的格式显示日期/时间 |
30. SQL null 值
null 空值
is null 查找空值
select * form people where name is null;
is not null 查找非空值
select * form people where name is not null;
31. SQL null 函数
此函数定义了在列进行统计或者计算时,列值为 null 时自动转换成 0 来参与计算
语法:
SQL Server / MS Access;
select score * (score+isnull(score,0)) from students;
Oracle:
select score * (score + nvl(score,0)) form students;
MySQL:
select score * (score + ifnull(score,0)) form students;
或
select score * (score + coalesce(score,0)) form students;
32. SQL 通用数据类型
通用数据类型:
数据类型 | 描述 |
---|---|
CHARACTER(n) | 字符/字符串。固定长度 n。 |
VARCHAR(n) 或CHARACTER VARYING(n) | 字符/字符串。可变长度。最大长度 n。 |
BINARY(n) | 二进制串。固定长度 n。 |
BOOLEAN | 存储 TRUE 或 FALSE 值 |
VARBINARY(n) 或BINARY VARYING(n) | 二进制串。可变长度。最大长度 n。 |
INTEGER§ | 整数值(没有小数点)。精度 p。 |
SMALLINT | 整数值(没有小数点)。精度 5。 |
INTEGER | 整数值(没有小数点)。精度 10。 |
BIGINT | 整数值(没有小数点)。精度 19。 |
DECIMAL(p,s) | 精确数值,精度 p,小数点后位数 s。例如:decimal(5,2) |
NUMERIC(p,s) | 精确数值,精度 p,小数点后位数 s。(与 DECIMAL 相同) |
FLOAT§ | 近似数值,尾数精度 p。一个采用以 10 为基数的指数计数法的浮点数。该类型的 size 参数由一个指定最小精度的单一数字组成。 |
REAL | 近似数值,尾数精度 7。 |
FLOAT | 近似数值,尾数精度 16。 |
DOUBLE PRECISION | 近似数值,尾数精度 16。 |
DATE | 存储年、月、日的值。 |
TIME | 存储小时、分、秒的值。 |
TIMESTAMP | 存储年、月、日、小时、分、秒的值。 |
INTERVAL | 由一些整数字段组成,代表一段时间,取决于区间的类型。 |
ARRAY | 元素的固定长度的有序集合 |
MULTISET | 元素的可变长度的无序集合 |
XML | 存储 XML 数据 |
SQL 数据类型快速参考手册
数据类型 | Access | SQLServer | Oracle | MySQL | PostgreSQL |
---|---|---|---|---|---|
boolean | Yes/No | Bit | Byte | N/A | Boolean |
integer | Number (integer) | Int | Number | Int Integer | Int Integer |
float | Number (single) | Float Real | Number | Float | Numeric |
currency | Currency | Money | N/A | N/A | Money |
string (fixed) | N/A | Char | Char | Char | Char |
string (variable) | Text (<256) Memo (65k+) | Varchar | Varchar Varchar2 | Varchar | Varchar |
binary object | OLE Object Memo | Binary (fixed up to 8K);Varbinary (<8K);Image(<2GB); | Long;Raw | Blob;Text | Binary;Varbinary |
33. SQL DB 数据类型
累了 自己找这个链接看吧
https://www.runoob/sql/sql-datatypes.html
更多推荐
SQL 高级
发布评论