创建数据库

 create table student
(
    stuId int primary key,
    stuName varchar(20),
    stuAge int,
    birthDay datetime,
    stuAddress varchar(100)
)

 

新增数据库信息:

方式一:insert into 表名(列名1,列名2...) values(值1,值2...)

方式二:insert into 表名 values(值1,值2....)  要求必须值要与列对应

insert into board(boardName) values('C语言')

insert into student(stuId,stuName,stuAge,birthDay,stuAddress) values(1,'张三',30,'1985-01-01','河南')

insert into student values(2,'李四',20,'1995-01-01','河南')

insert into student(stuName,stuAge,birthDay,stuAddress,stuId) values('张三2',30,'1985-01-01',河南',3)

insert into student(stuId,stuName,stuAge) values(4,'张三3',30) --错误

insert into student  values(5,'张三5',30)  --错误

复制数据

方法一:insert into <目标表名>(列名)  select <列名> from <源表名> (要求目标表必须存在)

--将student表中stuId,stuName,stuAge复制到studentinfo

insert into studentinfo(sid,sname,sage) select stuId,stuName,stuAge from student

方法二:select <列名> into <自动创建的目标表名> from <源表名>(目标表可以自动创建)

select stuId,stuName,stuAge into studentinfo from student

删除数据库信息

删除:delete from 表名 [where 条件]

select * from product2(*代表选中所有列)--查询表中所有数据
delete from product2 --删除表中的所有记录
select * from product3
delete from product3 where pid=2

删除数据:truncate table 表名 == delete from 表名(删除表中所有的数据truncate 语句执行速度快,并不写入日志文件)

truncate table product3--删除所有数据

修改数据库信息

修改:update 表名 set 列名1=值1,列名2=值2... [where 条件]

select * from product
update product set price=price-200
update product set price=price-100 where pid=1
update product set price=price+500,createDate='2015-2-1' where pid=2
eg:将学员一号课程分数加5为了避免分数加5后超过100分所以
    update student set score=score+5 where cid=1 and score<95  and 表示 同时成立时才能加5
    将所有分数不小于100的设置为100
    update Student set score=100 where not score<100

查询数据库数据

查询数据

查询所有列:( * :代表所有列) select * from 表名

select * from emp

查询部分列:select 列名1,列名2..from 表名

select empId,empName,empSex from emp

为列起别名: select 列名1 as '别名'... from 表名或者去as用空格代替

select empId as '员工编号',empName as '员工姓名', empSex as '员工性别' from emp(用as表示的)
select empId '员工编号',empName '员工姓名', empSex '员工性别' from emp(用空格表示的)

条件查询(where子语句) select 列名1,列名2.... from 表名 where 条件

--查询地址为"杭州"的员工信息
select * from emp where empAddress='杭州'

--查询性别为"男"的员工信息
select * from emp where empSex='男'

--地址为"郑州"或"北京"的员工信息
select * from emp where empAddress='郑州' or empAddress='北京'


--查询地址是"杭州"并且性别为"女"的员工信息
select * from emp where empAddress='杭州' and  empSex='女'

--查询部门人数>0的部门信息
select * from dept where deptNum>0

--查询部门人数>=2并且<=5的部门名称和部门人数
select deptName as'部门名称',deptNum as'部门人数'where deptnum between 2 and 5

聚合函数

特别注意 :聚合函数中不可以使用 where条件语句  可以使用having 语句限制条件

select COUNT(*) as '记录数' from product--查询有多少条记录
select MAX(price) from product--查询最大值
select MIN(price) from product--查询最小值
select SUM(price) from product--查询总和
select AVG(price) from product--查询平均值
select * from sales

语法 : select 聚合函数名 (所求的对象)

--计算"联想集团"采购的总金额 
select SUM(productNumber*salePrice) from sales where ClientName='联想集团'

--计算"联想集团"总采购数量和平均价格
select SUM(productNumber) as '总数量', SUM(productNumber*salePrice)/SUM(productNumbe

---排序:order by 列名 asc(升序 默认是升序)| desc(降序)

分组函数:group by 列名 

select distinct clientName from sales
select clientName from sales group by clientName

查询其采购额>100000的客户名称和采购额

--1.查询客户名称和采购额
select clientName '客户名称',SUM(productNumber*salePrice)'采购额' from sales group by clientName
--2.为分组函数添加条件使用having 添加聚合函数条件(where子语句中不能出现聚合函数)
--select clientName '客户名称',SUM(productNumber*salePrice)'采购额' from sales 
-- group by clientName having  SUM(productNumber*salePrice)>100000
select ClientName,SUM(productNumber*salePrice)  from sales group by ClientName having SUM(productNumber*salePrice)>100000        

子查询和连接查询

select * from product
select * from sales

连接查询 

--1.查询采购数量>20的商品编号
select productId from sales where productNumber>20  --1,2,3
--2.根据商品编号查询商品名称
select productName from product where productId in(1,2,3)

子查询

select productName from product where productId in(select productId from sales where productNumber>20)

----查询采购总数量>50的商品名称
----1.查询采购总数量>50的商品编
--select productId from sales group by productId having SUM(productNumber)>50
----2.根据商品编号查询商品名称
--select productName from product where productId in(select productId from sales group by productId having SUM(productNumber)>50)

模糊查询和排序及取前N条记录

--排序:order by 列名 asc(升序)|desc(降序)   语法 :select *from 表名 order by 列名 asc或者desc
    select * from dept order by deptNum  --默认升序
    select * from dept order by deptNum asc --升序
    select * from dept order by deptNum desc --升序

--查询部门编号为1的员工信息,并按照员工编号降序排列
    select * from emp where deptId=1 order by empId desc

--top关键字:获取前N条记录  select top N 列 from 表名 .意思为:查询出表中的前N条记录
    select top 3 * from dept --查询部门表中的前3条记录

--查询部门人数最多的两个部门
    select top 2 * from dept order by deptNum desc
    select top 2 deptName,deptNum from dept order by deptNum desc

--模糊查询:like  %:代表任意长度任意字符  _:代表任意一个字符
--查询姓名中出现"小"的员工信息
    select * from emp where empName like '小%' --以"小"开头
    select * from emp where empName like '%小' --以"小"结尾
    select * from emp where empName like '%小%' --带"小"
    select * from emp where empName like '小_' --以"小"开头,并且后边只能有一个字符

--distinct:去掉重复项
    select empaddress from emp
    select distinct empaddress from emp
--is null 或 not null

--查询员工中没有电话的员工信息
    select * from emp where empTel IS NULL
    select * from emp where empTel IS NOT NULL
--between...and...
    select deptName as'部门名称',deptNum as'部门人数' from dept where deptNum>=2 and deptNum<=5
    select deptName as'部门名称',deptNum as'部门人数' from dept where deptNum between 2 and 5

-- in关键字
--地址为"郑州"或"北京"的员工信息
    select * from emp where empAddress='郑州' or empAddress='北京'
    select * from emp where empAddress in('郑州','北京')

数据查询例

create database empDB --创建数据库名empDB
on primary
(
	name='empDB',
	fileName='F:\empDB.mdf',
	size=5MB,
	maxsize=unlimited,
	filegrowth=10%
)
log on
(
	name='empDB_log',
	filename='F:\empDB_log.ldf',
	size=3MB,
	maxsize=20MB,
	filegrowth=2MB
)
go
use empDB
go
create table dept
(
	deptId int primary key identity(1,1),
	deptName varchar(20),
	deptNum int,
	deptDesc varchar(100)
)
 
create table emp
(
	empId int primary key,
	empName varchar(20),
	empSex varchar(2),
	empAddress varchar(100),
	empTel varchar(11),
	deptId int foreign key references dept(deptId)
)
go
--插入:insert into 表名(列名1,列名2....) values(值1,值2...)

insert into dept(deptName,deptNum,deptDesc) values('市场部',0,'市场开拓')
insert into dept(deptName,deptNum,deptDesc) values('研发部',0,'产品研发')
insert into dept(deptName,deptNum,deptDesc) values('行政部',3,'行政相关')
insert into dept(deptName,deptNum,deptDesc) values('人事部',2,'人事相关')
insert into dept(deptName,deptNum,deptDesc) values('生产部',10,'生产相关')


select * from dept
insert into emp(empId,empName,empSex,empAddress,empTel,deptId) values(1,'小强','男','杭州','13111111111',1)
insert into emp(empId,empName,empSex,empAddress,empTel,deptId) values(2,'小花','女','杭州','13111111112',1)
insert into emp(empId,empName,empSex,empAddress,empTel,deptId) values(3,'小明','男','杭州','13111111113',2)
insert into emp(empId,empName,empSex,empAddress,empTel,deptId) values(4,'小红','女','郑州','13111111114',2)
insert into emp(empId,empName,empSex,empAddress,empTel,deptId) values(5,'张三','男','北京','13111111113',1)
insert into emp(empId,empName,empSex,empAddress,empTel,deptId) values(6,'李四','女','上海','13111111114',2)

select * from emp

--修改:update 表名 set 列名1=值1, 列名2=值2 where 条件
	update  dept set deptNum=2 where deptName='市场部'
	select * from dept

--删除:delete from 表名 where 条件==truncate table 表名 where
	delete from emp where deptId=2
	select * from emp

--查询所有列:(*:代表所有列) select * from 表名
	select * from emp
--查询部分列:select 列名1,列名2..from 表名
	select empId,empName,empSex from emp
--为列起别名: select 列名1 as '别名'... from 表名
	select empId as '员工编号',empName as '员工姓名', empSex as '员工性别	' from emp
	select empId '员工编号',empName '员工姓名', empSex '员工性别' from 	emp

--条件查询(where子语句) select 列名1,列名2.... from 表名 where 条件
--查询地址为"杭州"的员工信息
	select * from emp where empAddress='杭州'
--查询性别为"男"的员工信息
	select * from emp where empSex='男'
--地址为"郑州"或"北京"的员工信息
	select * from emp where empAddress='郑州' or empAddress='北京'
--查询地址是"杭州"并且性别为"女"的员工信息
	select * from emp where empAddress='杭州' and  empSex='女'
--查询部门人数>0的部门信息
	select * from dept where deptNum>0
--查询部门人数>=2并且<=5的部门名称和部门人数

	select deptName as'部门名称',deptNum as'部门人数' from dept where 	deptNum>=2 and deptNum<=5

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐