SQL语句的基本参数






create database benet
#创建数据库,名为benet
use benet
#打开benet数据库 
create table A1
#创建表为A1
(
 编号 
int identity(
1,
1) 
not null,
 #
identity(
1,
1)表示该列为标识列,种子和增量值都是
1
 学号 
int primary 
key not null,
 #primary 
key 表示该列为主键列
 姓名 
nvarchar(
20) 
not null,
 #
not null 表示不允许为空
 身 份 证号 
nvarchar(
18) 
not null,
 年龄 tinyint 
not null,
 班级 
int  not null,
 备注 
nvarchar(
1000) 
null,
)
alter table A1
add 出生日期 datetime 
not null
#表示往A1表中添加一个“出生日期”列
alter table A1
alter column 备注 
nvarchar(
2000) 
null
#修改A1表中备注的参数
alter table A1
drop column 备注
#删除A1表中的“备注”列
drop table A1
#删除A1表
insert into B1 (学号,姓名,身 份 证号,年龄,班级,备注)
values (
2,
'柳岩',
'110258198308282882',
27,
2,
'英语科代表')
#往B1表中插入柳岩的信息
update B1 
set 备注=
'数学课代表' where 姓名=
'柳岩'
#把B1表中柳岩的备注改为数学课代表
delete from B1 
where 学号=
2
#删除表中学号为
2的记录
关于删除的语句

查询时所需要用到的运算符、通配符、逻辑运算符



select * 
from B1
#查看B1表中的所有列
select 姓名,班级 
from B1
#查看表中的姓名和班级列
select 姓名 
from B1  
where 备注=
'英语科代表'
#查看B1表中的所有英语科代表的姓名
select * 
from B1 
where 基本工资 
between 8000 and 10000
#查看B1表中基本工资为
8000~
10000的员工的所有信息
select * 
from B1 
where 基本工资<
1000 or 基本工资>
2000
#查看B1表中基本低于
1000高于
2000的员工的所有信息
select * 
from B1  
where 基本工资 
in (
8000,
9000,
10000)
#查看表中基本工资是
8000、
9000、
10000的员工所有信息
select * 
from B1  
where 姓名 
like '王%' and 职务=
'运维工程师'
#查看B1表中姓王的运维工程师的信息
select * 
from B1  
where 备注 
is not null
#查看B1表中备注不为空的员工信息
select top 
3 * 
from B1
#查看B1表中前
3行的数据信息
select 姓名 
as name,身 份 证号 
as idcard 
from B1
#查询B1表中“姓名”和“身 份 证号”两列的数据,姓名改为
name,×××号改为idcard
select * 
from B1 
order by 基本工资 
desc
#查看B1表中的所有员工的信息,按基本工资从高到低显示查询结果
select * 
from B1 
order by 基本工资 
asc
#查看B1表中的所有员工的信息,按基本工资从低到高显示查询结果
select distinct 职务 
from  B1
#查看B1表中有哪些职务
select 姓名 
as name,身 份 证号,职务,基本工资 
from B1
where 身 份 证号 
like '_0%' and 职务 !=
'cto'
order by 基本工资 
desc
#在B1表中列出满足身 份 证号的左起第三位是
0的。除了cto以外的,
所有员工的姓名、身 份 证号、职务和基本工资,
其中姓名显示为
name,查询结果按照基本工资由高到底排序
select 姓名,身 份 证号,职务 
into new01 
from B1 
#将B1表中的姓名、身 份 证号、职务生成一个新表new01
(新表不用事先创建)
insert into new02 (姓名,职务,出生日期) 
select 姓名,职务,出生日期 
from B1 
where 基本工资>=
15000 
#将B1表中所有基本工资大于等于
15000的员工的姓名,职务,和出生日期保存到 new02表中
(注意,这里的 Table_1表中需要提前建立)
insert into new03 (姓名,职务,出生日期) 
select '张三',
'运维',
'1995-01-01' union 
select '李四',
'运维',
'1996-01-01' union 
select 姓名,职务,出生日期 
from B1 
#将B1表中所有员工的姓名、职务和出生日期,
以及新输入的
2名员工相关信息,一起保存到新表new03
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx