SQL基本知识-2

本文介绍了SQL中的关键概念,包括TOP用于限制返回的记录数,LIKE用于模式匹配,IN和BETWEEN操作符,以及JOIN、UNION等联接查询。还讲解了数据库中的别名、约束(如NOT NULL、UNIQUE、PRIMARY KEY、FOREIGN KEY和CHECK)以及索引的重要性和使用。最后提到了视图和日期函数的应用,以及SQL数据类型和数据库管理系统的基本知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Top:用于规定要返回的记录数目
    *并非所有数据库都支持Top子句
select top number/percent column_names from table_name
eg:select top 5 * from table_1

在MySQL中:

select column_name(s) from table_name limit number

在oracle中:

select column_name(s) from table_name where rownum <= number
  1. LIKE:在where语句中搜索指定列中的指定模式
    “%” 可用于定义通配符(模式中缺少的字母)
select column_name(s) from table_name where column_name like pattern
eg:
select * from Persons where city like 'N%'
select * from persons where city like '%g'
select * from persons where city like '%lon%'
select * from persons where city not like '%lon%'
  1. 通配符:
    必须与like运算符一起使用;
    %代替0or多个字符在这里插入图片描述
从上面的 "Persons" 表中选取居住的城市以 "A""L""N" 开头的人:
select * from persons where city like '[ALN]%'
从上面的 "Persons" 表中选取居住的城市
不以 "A""L""N" 开头的人:
select * from persons where city like '[!ALN]%'
  1. IN操作符:在where句子中规定多个值
select column_name(s) from table_name where column_name in (value1,value2,...)
  1. between 操作符:在where语句中使用,选取介于两者之间的数据范围
    *取值是否包括value1和value2由不同的数据库决定,存在差异性
select column_name(s) from table_name where column_name between value1 and value2
select column_name(s) from table_name where column_name not between value1 and value2
  1. alias:为列名称和表名称指定别名
表的SQL ALIAS用法:
select column_name(s) 
from table_name as alias_name
列的SQL ALIAS用法:
select column_name(s) as alias_name 
from table_name 
  1. join:根据两个或多个表中的列之间的关系,从这些表中查询数据

数据库中的表可通过键将彼此联系起来。主键(Primary Key)是一个列,在这个列中的每一行的值都是唯一的。在表中,每个主键的值都是唯一的。这样做的目的是在不重复每个表中的所有数据的情况下,把表间的数据交叉捆绑在一起。

inner join:在表中存在至少一个匹配时,INNER JOIN 关键字返回行。
*inner join 和 join相同

select column_name(s) 
from table_name1
inner join table_name2
on table_name1.column_name=table_name2.column_name

left join:从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行
*在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN

select column_name(s) 
from table_name1
left join table_name2
on  table_name1.column_name = table_name2.column_name

right join:从右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行
*在某些数据库中, right JOIN 称为 right OUTER JOIN

select column_name(s) 
from table_name1
right join table_name2
on  table_name1.column_name = table_name2.column_name

full join:只要其中某个表存在匹配,FULL JOIN 关键字就会返回行
*在某些数据库中, full JOIN 称为full OUTER JOIN

select column_name(s) 
from table_name1
full join table_name2
on table_name1.column_name=table_name2.column_name
  1. union:合并两个or多个select 语句的结果集;简而言之,合并表结果

UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同
UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
#默认选取不同的值;若允许重复,则使用union all
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
  1. select into:主要指从一个表中选取数据插入另一个表中,创建表的备份复件或存档
select *
into new_table_name[in externaldatabase]
from old_tablename
eg:
备份复件:
select *
into new_tablename
from old_tablename
in子句可用于从另一个database中拷贝:
select *
into persons in 'backup.mdb'
from persons
仅拷贝某些域:
select lastname,firstname
into persons_backup
from persons

也可以从多个表中选取数据:

select lastname,firstname
into persons_backup
from persons
inner join orders
on persons.id_p=orders.id_p 
  1. create database:创建数据库
create database database_name
  1. create table:创建表
create table table_name
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
)

在这里插入图片描述

  1. SQL约束:限制加入表的数据类型
    not full:约束强制列不接受 NULL 值;强制字段始终包含值。这意味着,如果不向字段添加值,就无法插入新记录或者更新记录?
    unique:约束唯一标识数据库表中的每条记录;

PRIMARY KEY 拥有自动定义的 UNIQUE 约束。
每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束
在MySQL中:

create table persons
(
Id_P int not null,
city varchar(250),
unique(Id_P)
)

在oracle/MS Access/SQL Server中:

create table Persons
(
Id_P int not null unique,
city varchar(250)
)

若需要命名unique约束,且为多个列定义unique:

create table persons
(
Id_P int not null,
city varchar(250),
constraint uc_personid unique(Id_P,city)
)

若表已被创建,需定义某一列unique:

alter table persons
add unique(Id_P)

若表已被创建,需命名约束,定义多列unique:

alter table persons
add constraint uc_personid unique(Id_P,city)

若需撤销unique约束:
在MySQL中:

alter table persons
drop index uc_personid

在oracle/MS Access/SQL Server中:

alter table persons
drop constraint uc_personid
  1. primary key:主键,约束唯一标识数据库表中的每条记录;包含唯一值;不包含null值;每个表都有且只有一个主键
    在MySQL中:
create table persons
(
Id_P int not null
primary key(Id_P)
)

在oracle/MS Access/SQL Server中:

create table persons
(
Id_P int not null primary key
)

若需命名primary key,且为多个列定义primary key约束:

create table persons
(
Id_P int not null,
city varchar(250),
constraint pk_personid primary key (Id_P,city)
)

若表已被创建,需为某一列创建primary key约束:

alter table persons
add primary key(Id_P)

若需命名primary key,且为多个列定义:

alter table persons
add constraint pk_personid primary key(Id_P,city)

若需撤销primary key约束:
在MySQL中:

alter table person
drop primary key

在oracle/MS Access/SQL Server中:

alter table person
drop constraint pk_personid
  1. foreign key:外键,指向另一个表中的primary key;用于预防破坏表之间连接的动作,防止非法数据插入外键列
    在MySQL中:
create table orders
(
Id_O int not null,
Id_P int,
primary key (Id_O),
foreign key(Id_P) references persons(Id_P)
)

在oracle/MS Access/SQL Server中:

create table orders
(
Id_O int not null primary key,
Id_P int foreign key references persons(Id_P)
)

若需命名foreign key约束,且为多个列定义foreign key:

create table orders
(
Id_O int not null,
Id_P int,
primary key (Id_O),
constraint fk_perorders foreign key (Id_P)
references persons(Id_P)
)

若在表已存在的情况下,创建foreign key约束:

alter table orders
add foreign key (Id_P)
references persons(Id_P)

若在表存在的情况下,需命名foreign key约束,且为多个列定义foreign key:

alter table orders
add constraint fk_perorders
foreign key (Id_P)
references persons(Id_P)

若需撤销foreign key约束:
在MySQL中:

alter table orders
drop foreign key fk_perorders

在oracle/MS Access/SQL Server中:

alter table orders
drop constraint fk_perorders
  1. check约束:用于限制列中值的范围;
    在MySQL中:
create table persons
(
Id_P int not null,
check(Id_P>0)
)

在oracle/MS Access/SQL Server中:

create table persons
(
Id_P int not null check(Id_P>0)
)

若需要命名 CHECK 约束,以及为多个列定义 CHECK 约束:
create table persons

create table persons
(
Id_P int not null,
city varchar(250),
constraint chk_person check (Id_P>0 and city='Sandnes')
)

若在表存在的情况下,需为某一列创建check约束:

alter table persons
add check(Id_P>0

若在表存在的情况下,需为多列创建check约束且命名:

alter table persons
add constraint chk_person check(Id_P>0 and city='Sandnes')

若需撤销check约束:
在MySQL中:

alter table persons
drop check chk_person

在oracle/MS Access/SQL Server中:

alter table persons
drop constraint chk_person
  1. default约束:向列中插入默认值;
    在创建表时插入值:
create table persons
(
city varchar(250) default 'Sandnes'
)
#利用函数getdate()可插入系统值
create table orders
(
city varchar(250) default **getdate()**
)

若表已存在,向某列插入值:
在MySQL中:

alter table persons
alter city set default 'Sandnes'

在oracle/MS Access/SQL Server中:

alter table persons
alter column city set default 'Sandnes'

若需要撤销Default约束:
在MySQL中:

alter table persons
alter city drop default 

在oracle/MS Access/SQL Server中:

alter table persons
alter column city drop default 
  1. create index:在表中创建索引;方便应用程序在不读取整个表的情况下更快查找数据

用户无法看到索引,它们只能被用来加速搜索/查询。
更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。

create index index_name
on table_name(column_name1,column_name2)

在表中创建一个唯一的索引给,意味着两个行不能拥有相同索引值:

create unique index index_name
on table_name(column_name)
  1. drop:删除索引跟、表、数据库
    drop index: 删除索引
    在MySQL中:
ALTER TABLE table_name DROP INDEX index_name

在Microsoft SQLJet (以及 Microsoft Access) 中:

drop index index_name on table_name

在MS SQL Server 中:

DROP INDEX table_name.index_name

在IBM DB2 和 Oracle中:

DROP INDEX index_name

drop table:删除表drop table 表名称
drop database:删除数据库drop database 数据库名称
truncate table:删除表数据但不删除表truncate table 表名称

  1. alter table:在已有表中增、删、改列
    添加列:
alter table table_name
add column_name datatype

删除列:

alter table table_name
drop column column_name
#或者:
alter table table_name
alter column column_name datatype
  1. auto-increment:在新记录插入表中时生成一个唯一的数字
    在MySQL中:
create table persons
(
P_Id int not null auto_increment,
PRIMARY KEY (P_Id)
)

默认开始值为1,每条递增1;若需要从其他值开始:

alter table persons AUTO_INCREMENT=100

在MS SQL中,使用identify来执行auto-increment:

create table persons
(
P_Id int PRIMARY KEY identity
)

默认开始值为1,每条递增1;若需要从20开始且递增10:

create table persons
(
P_Id int PRIMARY KEY identity(20,10)
)

在Access中,使用auto-increment:

create table persons
(
P_Id int PRIMARY KEY autoincrement(20,10)
)

在oracle中,需先创建auto-increment字段:
通过对sequence创建auto-increment字段:

CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10
  1. view:视图
    create view:创建视图
create view view_name as
select column_name(s)
from table_name
where condition

查询视图:select * from [ view_name ]
更新视图:

sql create or replace view Syntax
create or replace view view_name as
select column_name(s)
from table_name
where condition

撤销视图:

drop view view_name
  1. date函数:如果不含时间部分可以更好处理和查询~
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  2. null
    is null操作符:查找null值

select column_name(s) from table_name
where column_name is null

is not null:

select column_name(s) from table_name
where column_name is null
  1. null函数:如何处理null值
    在MySQL中:ifnull(column_name,值)coalesce(column_name,值)
    在SQL Server / MS Access中:isnull(column_name,值)
    在oracle中:nvl(column_name,值)

  2. SQL数据类型
    在MySQL中,主要有三种类型:文本、数字和日期/时间

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

其余更多,请参考:添加链接描述

  1. RDBMS:SQL服务器

DBMS - 数据库管理系统(Database Management System)
数据库管理系统是一种可以访问数据库中数据的计算机程序。
DBMS 使我们有能力在数据库中提取、修改或者存贮信息。
不同的 DBMS 提供不同的函数供查询、提交以及修改数据。

RDBMS - 关系数据库管理系统(Relational Database Management System)
关系数据库管理系统 (RDBMS) 也是一种数据库管理系统,其数据库是根据数据间的关系来组织和访问数据的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值