1、创建名为gongsi的数据库:
初始大小1MB,上不封顶,每次扩展64MB,同时有辅助文件
create database gongsi
on primary(
name='gongsi_dbf',
filename='E:\gongsi\gongsi.mdf',
size=1MB,
maxsize=unlimited,
filegrowth=64MB
)
log on
(
name='gongsi_ldf',
filename='E:\gongsi\gongsi.ldf',
size=1MB,
maxsize=unlimited,
filegrowth=64MB
);
2、连接至gongsi数据库:
use gongsi
3、创建表dangan至gongsi数据库:
create table dangan 创建表,表名为dangan
(
ID int primary key, 设置ID为主键
name char (12) not null, 设置name为非空
sex char (2) default '男' not null , 设置sex默认值为男,并且非空
age int not null, 设置age为非空
csny date,
jiguanid int,
zhiweiid int,
gongling int
);
select * from dangan 查看dangan表项是否正确
4、将数据写入dangan表中
insert into dangan
select 1,'李晨','男',35,'1982-5-30',1,1,15
union select 2,'陈赫','男',34,'1983-4-22',1,2,14
union select 3,'张敏','女',33,'1984-1-20',2,3,13
union select 4,'林青霞','男',28,'1989-6-26',1,2,8
union select 5,'陆毅','男',40,'1977-4-14',2,5,20
union select 6,'王菲','女',32,'1985-3-21',2,3,12
union select 7,'刘亦菲','女',30,'1987-9-30',3,3,10
union select 8,'王祖贤','女',50,'1967-8-30',4,4,30
union select 9,'高晓松','男',21,'1996-5-30',6,6,1
union select 10,'邱淑贞','女',20,'1997-5-30',8,6,0
5、创建zhiwei表
create table zhiwei
(
zw_id int primary key,
zw char (10) not null,
);
6、将数据写入zhiwei表中:
insert into zhiwei
select 1,'技术支持'
union select 2,'运维'
union select 3,'销售'
union select 4,'财务'
union select 5,'保安'
union select 6,'开发'
7、创建jiguan表:
create table jiguan
(
jg_id int primary key,
jg char (10) not null,
)
8、将数据写入jiguan表中:
insert into jiguan
select 1,'北京'
union select 2,'河北'
union select 3,'天津'
union select 4,'上海'
union select 5,'哈尔滨'
union select 6,'广东'
union select 7,'福建'
union select 8,'云南'
union select 9,'甘肃'
9、将dangan表的zhiweiid外键约束设置为zhiwei表的zw_id
alter table dangan
add constraint zw foreign key (zhiweiid) references zhiwei(zw_id)
10、将dangan表的jiguanid外键约束设置为jiguan表的jg_id
alter table dangan
add constraint jg foreign key (jiguanid) references jiguan(jg_id)
11、创建yeji表并设置ID的外键约束为dangan表的ID
create table yeji
(
ID int not null,
nian int not null,
yue int not null,
yeji int not null
constraint yj foreign key (ID) references dangan(ID)
)
12、将数据写入至yeji表中:
insert into yeji
select 1,2017,1,89067
union select 1,2017,2,77800
union select 1,2017,3,80000
union select 1,2017,4,76900
union select 1,2017,5,88097
union select 1,2017,6,98078
union select 1,2017,7,89067
union select 3,2017,1,47694
union select 3,2017,2,98576
union select 3,2017,3,65967
union select 3,2017,4,69857
union select 3,2017,5,58776
union select 3,2017,6,97877
union select 3,2017,7,58756
union select 6,2017,1,56897
union select 6,2017,2,57836
union select 6,2017,3,54966
union select 6,2017,4,56807
union select 6,2017,5,60987
union select 6,2017,6,67987
union select 6,2017,7,67989
union select 7,2017,1,56976
union select 7,2017,2,56860
union select 7,2017,3,56657
union select 7,2017,4,88980
union select 7,2017,5,80808
union select 7,2017,6,78796
union select 7,2017,7,80796
13、创建kaohe表并设置ID的外键约束为dangan表的ID
create table kaohe
(
ID int not null,
nian int not null,
yue int not null,
kaohe int not null
constraint kh foreign key (ID) references dangan(ID)
)
14、将数据添加至kaohe表中:
insert into kaohe
select 1,2017,1,89
union select 1,2017,2,88
union select 1,2017,3,78
union select 1,2017,4,78
union select 1,2017,5,87
union select 1,2017,6,67
union select 1,2017,7,67
union select 2,2017,1,77
union select 2,2017,2,89
union select 2,2017,3,98
union select 2,2017,4,88
union select 2,2017,5,89
union select 2,2017,6,87
union select 2,2017,7,78
union select 3,2017,1,87
union select 3,2017,2,89
union select 3,2017,3,87
union select 3,2017,4,89
union select 3,2017,5,87
union select 3,2017,6,89
union select 3,2017,7,76
union select 4,2017,1,65
union select 4,2017,2,67
union select 4,2017,3,86
union select 4,2017,4,78
union select 4,2017,5,86
union select 4,2017,6,78
union select 4,2017,7,98
union select 9,2017,1,89
union select 9,2017,2,88
union select 9,2017,3,78
union select 9,2017,4,78
union select 9,2017,5,87
union select 9,2017,6,67
union select 9,2017,7,67
union select 6,2017,1,77
union select 6,2017,2,89
union select 6,2017,3,98
union select 6,2017,4,88
union select 6,2017,5,89
union select 6,2017,6,87
union select 6,2017,7,78
union select 7,2017,1,87
union select 7,2017,2,89
union select 7,2017,3,87
union select 7,2017,4,89
union select 7,2017,5,87
union select 7,2017,6,89
union select 7,2017,7,76
union select 10,2017,1,65
union select 10,2017,2,67
union select 10,2017,3,86
union select 10,2017,4,78
union select 10,2017,5,86
union select 10,2017,6,78
union select 10,2017,7,98
15、创建gongzi表并设置ID的外键约束为dangan表的ID
create table gongzi
(
ID int not null,
nian int not null,
yue int not null,
gongzi int not null
constraint gz foreign key (ID) references dangan(ID)
)
16、将数据添加至gongzi表:
insert into gongzi
select 1,2017,1,8000
union select 1,2017,2,7900
union select 1,2017,3,7900
union select 1,2017,4,8500
union select 1,2017,5,7500
union select 1,2017,6,8100
union select 1,2017,7,7900
union select 2,2017,1,6000
union select 2,2017,2,6000
union select 2,2017,3,6000
union select 2,2017,4,6000
union select 2,2017,5,6000
union select 2,2017,6,6000
union select 2,2017,7,6000
union select 3,2017,1,6500
union select 3,2017,2,6500
union select 3,2017,3,6500
union select 3,2017,4,6500
union select 3,2017,5,6500
union select 3,2017,6,6500
union select 3,2017,7,6500
union select 4,2017,1,5800
union select 4,2017,2,5700
union select 4,2017,3,5800
union select 4,2017,4,5700
union select 4,2017,5,5900
union select 4,2017,6,5900
union select 4,2017,7,6000
union select 9,2017,1,8000
union select 9,2017,2,7900
union select 9,2017,3,7900
union select 9,2017,4,8500
union select 9,2017,5,7500
union select 9,2017,6,8100
union select 9,2017,7,7900
union select 6,2017,1,6000
union select 6,2017,2,6000
union select 6,2017,3,6000
union select 6,2017,4,6000
union select 6,2017,5,6000
union select 6,2017,6,6000
union select 6,2017,7,6000
union select 7,2017,1,6500
union select 7,2017,2,6500
union select 7,2017,3,6500
union select 7,2017,4,6500
union select 7,2017,5,6500
union select 7,2017,6,6500
union select 7,2017,7,6500
union select 10,2017,1,5800
union select 10,2017,2,5700
union select 10,2017,3,5800
union select 10,2017,4,5700
union select 10,2017,5,5900
union select 10,2017,6,5900
union select 10,2017,7,6000
union select 5,2017,1,8000
union select 5,2017,2,7900
union select 5,2017,3,7900
union select 5,2017,4,8500
union select 5,2017,5,7500
union select 5,2017,6,8100
union select 5,2017,7,7900
union select 8,2017,1,6000
union select 8,2017,2,6000
union select 8,2017,3,6000
union select 8,2017,4,6000
union select 8,2017,5,6000
union select 8,2017,6,6000
union select 8,2017,7,6000
17、给档案表中添加TEL字段,用来输入员工电话号码:
alter table dangan
add TEL char (15) default '1388686186' not null 添加默认值
18、给dangan表中前5名员工修改电话号码:
update dangan set TEL=13000000000 where ID=1
update dangan set TEL=13111111111 where ID=2
update dangan set TEL=13222222222 where ID=3
update dangan set TEL=13333333333 where ID=4
update dangan set TEL=13444444444 where ID=5
19、删除工资表中ID为9的所有数据。
delete from gongzi where ID=9
20、显示一个字段的数据记录
select name from dangan
21、显示多个字段的数据:
select name,id,age,sex from dangan;
22、显示所有字段的数据:
select * from dangan;
23、显示前3行的数据:
select top 3 * from dangan
24、条件查询
条件表达式:
where 字段名 运算符 值
运算符:比较运算符
= 等于
!= 不等于
<> 不等于
> 大于
< 小于
>= 大于等于
<= 小于等于
如果条件表达式中的字段名和值是数值型,则所有比较运算符都能够使用,如果是字符串型,则只能使用= 和!=
查询性别为女的员工信息。
select * from dangan where sex='女';
查询年龄大于25岁的员工信息。
select * from dangan where age>25 ;
查询职位为销售的员工信息。
方法一:
先查询销售的职位id号:
select * from zhiwei where zw='销售';
再查询dangan表中,zhiweiid为3的数据记录。
select * from dangan where zhiweiid=3;
方法二:
使用多表查询的语句,进行连接查询。涉及到多张表时,语法有了新的规则,建议将所有的字段前都加上表名,肯定不会出错。
select dangan.* from dangan,zhiwei
where dangan.zhiweiid=zhiwei.zw_id and zhiwei.zw='销售'
查询年龄20或21岁,并且工龄大于等于1年的员工信息。
select * from dangan where (age=20 or age=21) and gongling>=1;
查询年龄在30到50之间的员工信息。(包括30和50)
select * from dangan where age>=30 and age<=50;
或
select * from dangan where age between 30 and 50; between为在某个范围之间
查询职位为销售或运维的员工信息
select dangan.* from dangan,zhiwei
where dangan.zhiweiid=zhiwei.zw_id and (zhiwei.zw='销售' or zhiwei.zw='运维')
或者
select dangan.* from dangan,zhiwei
where dangan.zhiweiid=zhiwei.zw_id and zhiwei.zw in ('销售','运维')
25、模糊查询
又称为部分匹配查询,使用通配符代替查询条件中常量的部分字符,便于查询到跟多的结果或不能精确定位的结果。
通配符:
linux系统中的通配符:
? 可以代替任意一个字符,不能为空,范围是0-9 a-z A-Z 符号等。
如:abcd.txt ?bcd.txt ?a?
* 可以代替任意一组字符,可以为空,字符数量0个到最大
如:abc*.txt *a*
数据库中,由于?号和*号有特殊的作用所以使用其他符号做通配符:
_ 可以代替任意一个字符,可以为空,可以连续使用。显示多个_数量以内的字符。范围是0-9 a-z A-Z 符号等。
如:’刘_’ ’王__’最大2个最少1个字符 ‘_易_’
% 可以代替任意一组字符,可以为空,字符数量0个到最大
如:’刘%’
注意:因为查询条件中的常量使用了通配符后,不能代表一个精确的值,所以条件表达式中就不能使用=号进行连接了。使用like引用添加了通配符的常量。
查询姓张的员工信息:
select * from dangan where name like '张%';
查询姓刘的员工信息:
select * from dangan where name like '刘__'
不知道具体数值的查询:
select * from dangan where name like '王[飞非费肥菲妃]'
select * from dangan where name like '刘[以一亿已意亦][飞非费肥菲妃]'
26、空值查询
select * from dangan where gongling is null;
null空值,不是一个具体的数值,只是一个标记。is只用于查询空值
27、多表查询
当查询的字段或者条件中涉及到多张表时,需要在条件表达式中建立表与表之间的关系,如果没有建立关系,多张表之间的数据记录会被重复引用,出现错误的数据记录,如果关系建立错误,则可能导致查询出错误的数据或无数据。
查询李晨的工资信息
select dangan.name,gongzi.yue,gongzi.工资 from dangan,gongzi
where dangan.id=gongzi.id and dangan.name='李晨'
语句说明:多表查询时,在每个字段的前面添加表名,在语句的调用过程中肯定不会出错。如果引用的字段在所有涉及的表中只有一个,则可以不加表名。
查询李晨2月份工资
select gongzi.工资 from dangan,gongzi where
dangan.id=gongzi.id and dangan.name='李晨' and gongzi.yue=2
查询籍贯为北京的员工姓名。
select dangan.name from dangan,jiguan
where dangan.jiguanid=jiguan.jg_id and jiguan.jg='北京'
查询籍贯为北京,职位为运维的员工姓名。
分析:涉及到jiguan表,zhiwei表,dang_an表,在超过两张表建立关系时,不是所有的表之间都相互建立关系,至少与其中一张表建立关系。在建立关系时,注意需要重复引用的字段。
select dangan.name from dangan,jiguan,zhiwei
where dangan.jiguanid=jiguan.jg_id and
dangan.zhiweiid=zhiwei.zw_id and jiguan.jg='北京' and zhiwei.zw='运维'
查询李晨7月份的业绩,考核和工资。
分析:dangan,yeji,kaohe,gongzi ,id字段,
select dangan.name,yeji.yeji,kaohe.kaohe,gongzi.gongzi from dangan,yeji,kaohe,gongzi
where dangan.id=yeji.id and dangan.id=kaohe.id and dangan.id=gongzi.id and
yeji.nian=kaohe.nian and yeji.nian=gongzi.nian and
yeji.yue=kaohe.yue and yeji.yue=gongzi.yue and
dangan.name='李晨' and yeji.yue=7
语句说明:在编写多表查询的语句时,先做好三件事,就能降低编写难度,和提高编写思路。
1.分析查询需求中都涉及到了那些表?有些表是显示字段数据时调用,有些表是条件中调用。
2.分析这些表之间建立关系,都需要使用那些字段。包括这些表之间有没有描述同一件事务的字段,如果有,也需要建立等值链接。
3.分析查询需求中都有哪些条件?有些是明确的条件描述,有些是不明确的条件描述。
查询籍贯不是‘北京’的员工姓名
分析:查询条件是籍贯!=’北京’,需要显示的字段是 姓名name字段,涉及到dangan,jiguan表。
select dangan.name from dangan,jiguan
where dangan.jiguanid=jiguan.jg_id and jiguan.jg!='北京'
查询职位不是运维的员工姓名和所属职位
分析:条件--职位不是运维,显示字段 姓名 职位,涉及到dangan表和zhiwei表。
select dangan.name,zhiwei.zw from dangan,zhiwei
where dangan.zhiweiid=zhiwei.zw_id and zhiwei.zw!='运维'
查询2017年7月,既有yeji,又有考核的员工ID、姓名;
分析:条件2017年7月,yeji中的id等于kaohe中的id,显示字段 id name 涉及表 dangan,yeji,kaohe
select dangan.id,dangan.name from dangan,yeji,kaohe
where dangan.id=yeji.id and dangan.id=kaohe.id
and yeji.nian=kaohe.nian and yeji.yue=kaohe.yue
and yeji.nian=2017 and yeji.yue=7
查询职位为‘运维’,籍贯为‘北京’的员工ID、姓名
分析:条件 职位=运维,籍贯=北京,显示字段id name 涉及表 dang_an zhiwei jiguan
select dangan.id,dangan.name from dangan,jiguan,zhiwei
where dangan.jiguanid=jiguan.jg_id and dangan.zhiweiid=zhiwei.zw_id
and jiguan.jg='北京' and zhiwei.zw='运维'
查询有考核分数小于70的ID、姓名、年、月
分析:条件是kaohe<70,显示字段是id name nian yue ,涉及表dang_an kaohe
select dangan.id,dangan.name,kaohe.nian,kaohe.yue from dangan,kaohe
where dangan.id=kaohe.id and kaohe.kaohe<70
查询7月份考核分数小于80的员工姓名,按分数降序排列
分析:条件,yue=7 kaohe<80 order by kaohe desc,显示字段name,涉及表dangan kaohe
select dangan.name from dangan,kaohe
where dangan.id=kaohe.id and kaohe.yue=7
and kaohe.kaohe<80 order by kaohe desc desc:降序
查询职位为‘销售’并且工资6000以上的员工ID和姓名
分析:条件,zw=’销售’ gongzi>6000 显示字段 id name 表 dangan gongzi zhiwei
select dangan.id,dangan.name from dangan,gongzi,zhiwei
where dangan.id=gongzi.id and dangan.zhiweiid=zhiwei.zw_id
and zhiwei.zw='销售' and gongzi.gongzi>6000
查询职位为“销售”,且考核分数低于90的员工姓名、考核分数、月
分析:条件,zw=’销售’ kaohe<90 显示字段 name kaohe yue 表 dangan zhiwei kaohe
select dangan.name,kaohe.kaohe,kaohe.yue from dangan,kaohe,zhiwei
where dangan.id=kaohe.id and dangan.zhiweiid=zhiwei.zw_id
and zhiwei.zw='销售' and kaohe.kaohe<90
28、查询排序
order by 子句,根据指定的字段按升序asc 或降序desc进行排序,默认主键字段升序排序。如果指定字段排序但没有指定是升序还是降序,默认也是升序。如果指定排序的字段不是主键或有唯一性约束,重复信息的记录按照主键或第一字段升序排序。
select * from dangan order by age desc
select * from dangan order by csny asc
注意:在sqlserver中,能够对汉字进行排序,是按照汉字发音的声母排序,升序排序的优先级是,数字--英文字母--汉字。
在mysql和oracle中,不能对汉字的声母进行排序,只能按照汉字的16进制编码进行排序。
order by子句一般使用在T-SQL语句的最后面。能够对字段或统计后的数据进行排序。
29、将查询结果放入一张新表
查询员工姓名和工资大于6000的信息,放入新的gg表
select dangan.name,gongzi.nian,gongzi.yue,gongzi.gongzi into gg
from dangan,gongzi
where dangan.id=gongzi.id and gongzi.gongzi>6000
select * from gg 查看
注意:into 表名 只能是新表,如果是已存在的表,则会报错。
30、将查询数据添加到指定表。
insert into gg
select name,gongzi.nian,gongzi.yue,gongzi
from dangan,kaohe,gongzi
select * from gg
注意:添加时要匹配原表的结构和查询显示字段的位置。