当前位置: 移动技术网 > IT编程>数据库>MSSQL > 关于SQL语句的面试题分享

关于SQL语句的面试题分享

2018年12月26日  | 移动技术网IT编程  | 我要评论

尹载镐,谷开来图片,比克曼的世界

有段时间没写过sql语句了,面试就...所以总结了一下

sql对大小写并不敏感

sql分为两部分:数据操作语言(dml)和 数据定义语言(ddl)。

dml:

select ,update ,delete,insert into

ddl:

create database --创建

alter database --修改数据库

create table --创建新表

alter table --变更数据表

1.alter table 表名 add 列名 数据类型

2.alter table 表名 drop column 列名

3.alter table 表名 alter column 列名 数据类型

drop table --删除表

create index --创建索引

1.create index index_名称 on 表名(索引列名 asc|desc)

2.create unique index index_名称 on 表名(索引列名1,索引列名2)

drop index --删除索引

microsoftsqljet,microsoftaccess:

drop index index_名称 on 表名

mssqlserver:

drop index 表名.index_名称

ibmdb2oracle:

drop index index_名称

mysql:

alter table 表名 drop index index_名称

数据库中常用关键字:-------------------------------------------------------------------------

distinct 返回唯一值即:去除重复的元组

order by 根据指定列对结果集进行排序

asc --升序排序 desc --降序排序

top 用于规定返回的记录的数目

like 用于在where子句中搜索列中的指定模式

% --代替一个或多个字符

_ --仅代替一个字符

[charlist] --字符列表中任何单一字符

[^charlist] | [!charlist] --不再字符列表中的任何单一字符

in 用于在 where 子句中规定多个值。

between between ... and 用于选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。

alias 用于为表设置别名

select 列名 as 列的别名 from 表名 as 表的别名

inner join | join 如何表中有至少一个匹配,则返回行

left join ... on ... 即使右表没有匹配,也返回左表中的悬浮元组

right join ... on ... 即使左表没有匹配,也返回右表中的悬浮元组

full join ... on ... 只要其中一个表存在匹配,就返回悬浮元组

union 用于合并两个或多个select语句的结果集

union 默认选取不同值 union all 选取所有值

select distinct 列名 from 表名 where 列名 运算符 值

select distinct 列名 from 表名 order by 列名 asc, 列名 desc

sqlserver---select top number|precent 列名 from 表名

mysql---select 列名 from 表名 limit number|百分数

oracle--select 列名 from 表名 where rownum <= number

select distinct 列名 from 表名 where 列名 in (值1,值2,.....,值n)

select distinct 列名 from 表名 where 列名 between value1 and value2

select distinct 列名 from 左表名 inner join 右表名 on 左表列 运算符 右表列

select distinct 列名 from 左表名 left join 右表名 on 左表列 运算符 右表列

select distinct 列名 from 左表名 right join 右表名 on 左表列 运算符 右表列

select distinct 列名 from 左表名 full join 右表名 on 左表列 运算符 右表列

select 列名 from 表名 where 条件

union | union all

select 列名 from 表名 where 条件

select into 用于从一个表中选取数据,然后插入另一表中,常用于数据备份

select 列名 into 新表名 from 旧表名

select 列名 into 表名 in 数据库 from 旧表名

数据库中约束:-------------------------------------------------------------------------------

not null --约束强制列不接受空值

unique --约束唯一标识

1.constraint uc_约束名 unique (列名1,..,列名n)

2.alter table 表名 add unique (列名)

3.alter table 表名 add constraint uc_约束名 union (列名1,...,列名2)

-------------------------------------------------------------------

mysql--alter table 表名 drop index uc_约束名

其他 --alter table 表名 drop constraint uc_约束名

primary key --主键约束,唯一,不为空

1.mysql--primary key (列名)

2.constraint pk_约束名 primary key (列名1,列名2,...,列名n)

3.alter table 表名 add primary key(列名)

4.alter table 表名 add constraint pk_约束名 (列名1,列名2,...,列名n)

-------------------------------------------------------------------

mysql--alter table 表名 drop primary key

其他 --alter table 表名 drop pk_约束名

foregin key --外键约束

1.foregin key references 外表名(列名)

2.mysql--foregin key (要约束的列列名) references 外表名(列名)

3.constraint fk_约束名 foregin key (要约束的列列名) references 外表名(列名)

4.alter table 表名 add foregin key (列名) references 外表名(列名)

5.alter table 表名 add constraint fk_约束名 foregin key (列名) references 外表名(列名)

-------------------------------------------------------------------

mysql--alter table 表名 drop foregin key fk_约束名

其他 --alter table 表名 drop constraint fk_约束名

check 约束用于限制列中值的范围

1.check(id>0) 创建表时约束

2.constraint chl_约束名 check(id>0 and city='nanchang')

3.alter table 表名 add check (id>0)

4.alter table 表名 add constraint chk_约束名 check (id>0 and city='nanchang')

--------------------------------------------------------------------

mysql--alter table 表名 drop check chk_约束名

其他--alter table 表名 drop constraint chk_约束名

default 约束用于向列中插入默认值

1.default '默认值'

2.mysql--alter table 表名 alter 列名 set default '默认值'

3.其他 --alter table 表名 add column 列名 set default '默认值'

-------------------------------------------------------------------

mysql--alter table 表名 alter 列名 drop default

其他 --alter table 表名 alter column 列名 drop default

auto increment 自增主键

mysql:

id int not null auto_increment, 默认开始值为1

primary key (id)

alter table 表名 auto_increment=100 设置以100开始

sqlserver:

id int primary key identity, 默认1开始

oracle:

create sequence seq_名称

minvalue 1

start with 1

increment by 1

cache 10

-------------------------------------------

insert into 表名(p_id,firstname,lastname)

values (seq_person.nextval,'lars','monsen')

-------------------------------------------

access:

id int primary key autoincrement,

--------------------------------------------------------------------------------------------

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网