Skip to content
久久日记本
曾经年少爱追梦,一心只想往前飞
  • 首页
  • 博客
    • 博客历史
    • 主题
    • 个人文集
  • 关于
    • 正在读的书
    • 作品归档
    • 2018作品归档
    • 联系我
  • 友情链接
  • 留言板
❄
❅
❆
❄
❅
❆
❄
❅
❆
❄
Code/DB

SQL基础复习(1)

Posted on 2012年8月6日 by 九九 / 946 Views

1、表上的对象

--字段:数据库表网格中的一列,又称column
--记录:数据库表网格中的一行,又称row
--主键:一个表中能唯一区分每一条记录的字段,它可以做为其他表的外键
--外键:一个表中的某个字段的取值是另一个表的主键
--唯一键:一个表中能唯一区分每一条记录的字段,不能做外键

--索引:根据表的某些字段将数据进行分组,目的是提高查询效率,
--但是会导致新增等操作的效率降低,创建索引的字段取值不能很多
--索引适用于一些频繁进行查询的表,对于频繁进行新增等操作的表不适宜创建索引

2、新建表

create table Person1
(
 ID int not null identity(1,1) primary key,
 Name varchar(20),
 Age int,
 Sex char(2)
)

drop table person1

alter table person add birthday datetime
alter table person1 drop column Birthday

--删除数据用delete,删除数据库对象用drop

3、查询数据

select ID,Name,Birthday,Salary from Person
select ID,Name,year(Birthday),Salary+10000 from Person
--select 后面的字段可以是表的字段,也可以是计算公式,还可以是方法等

select * from person
select * from person order by salary asc--desc降序,asc升序
select * from person order by classID asc,salary desc
select distinct salary from person--消除重复行
select distinct sex,salary from person

--条件查询
select * from person where ID = 5
select * from person where salary >= 3000
select * from person where sex != '女'
select * from person where sex <>'女'
select * from person where not ID=5--not表示取反

select * from person where salary between 1000 and 3000--包含两个边界值
select * from person where id in (2,4,6,8,10,100)

select * from person where name like 'hu%'--通配符%表示任意个任意的字符, _表示一个任意的字符
select * from person where name like '%hu%'

select * from person where  sex is null--查询 null值的数据用is

--多条件查询
select * from person where classID=1 and sex='女' and birthday >'1985-1-1'
select * from person where classID=1 or sex='女' 
select * from person where  sex='女' and (birthday>'1985-1-1' or classID=1 )--and和or一起使用的时候,用()限定执行先后顺序


--聚合函数
select max(salary) from person--最大值
select min(salary) from person--最小值
select avg(salary) from person--平均值
select sum(salary) from person--总和
select count(*) from person--取记录的数量


--分组查询:根据字段的值将数据进行分组,得到的结果是每个分组的计算结果
--select查询的字段只能是分组的条件或者聚合函数
select count(*),classID
from person
group by classID

select classID
from person
where sex='女'--where用于限定所有的数据的条件
group by classID
having count(*)>=3--having用于限定分组内的条件,一般都只是聚合函数

4、更新数据

--修改
update Person set
 sex='女',
 salary = 3000.0
where id = 10

--注意两表关联更新的情况

5、删除数据

--删除
delete from Person
where id=9

--删除一个表中重复的数据???
注:2018-08-30 删除Person表中id等于9的数据,不论重复问题

6、数据类型

--char:固定长度字符串,如果输入字符不足,后面添空格
--varchar:不固定长度的字符串

--int:整数
--numeric:数值类型,可以小数,可以整数,表达方式numeric(20,2)

--datetime:表示时间 yyyy-mm-dd HH:mi:ss(24h)
insert into Person(ID,Name,Sex,Birthday,Salary)
values(1,'huqitao','m','2000-1-1',4000.0)

--数值类型的值不需要加单引号,时间类型和字符串需要加上单引号

7、新增数据

--新增
insert into Person(Name,Sex,Birthday,Salary)
values('Hello','f','1990-2-2',4000.0)

insert into Person(Name,sex,birthday,salary)
select name,sex,birthday,salary from person where sex='女'


--复制表
select * into Person2 from person
MSSQL, 网新实训笔记
九九
过去的我们,现在的自己,往事,终会随风而逝。 View all posts by 九九 →

Post navigation

Older post
SQL基础
Newer post
SQL基础复习(2)

标签云

2019ncov Android ASP.NET Baby C# C/C++ CSS Div DX11 flask front-end GAE Git Java JJProject JS Life MSSQL MVC OpenSource Oracle Python React React-Native Software Tools Vue Webpack Website Window WP7 乱记 十年旧梦 天气 宝宝成长日记 小说 工作 情感 故障 散文 日记 网新实训笔记 花落梧桐 诗间集 转载

时光机

  • 2023年3月
  • 2023年2月
  • 2022年12月
  • 2022年4月
  • 2022年3月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年5月
  • 2019年12月
  • 2019年10月
  • 2019年9月
  • 2019年6月
  • 2019年5月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年3月
  • 2018年2月
  • 2018年1月
  • 2017年11月
  • 2017年10月
  • 2017年9月
  • 2017年7月
  • 2017年3月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年10月
  • 2016年7月
  • 2016年3月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年11月
  • 2015年10月
  • 2015年9月
  • 2015年8月
  • 2015年7月
  • 2015年4月
  • 2015年3月
  • 2015年2月
  • 2015年1月
  • 2014年12月
  • 2014年11月
  • 2014年10月
  • 2014年9月
  • 2014年8月
  • 2014年7月
  • 2014年6月
  • 2014年5月
  • 2014年4月
  • 2014年3月
  • 2014年2月
  • 2014年1月
  • 2013年12月
  • 2013年11月
  • 2013年10月
  • 2013年9月
  • 2013年8月
  • 2013年7月
  • 2013年6月
  • 2013年5月
  • 2013年4月
  • 2013年3月
  • 2013年1月
  • 2012年11月
  • 2012年10月
  • 2012年9月
  • 2012年8月
  • 2012年7月
  • 2012年6月
  • 2012年5月
  • 2012年4月
  • 2012年3月
  • 2012年2月
  • 2012年1月
  • 2011年12月
  • 2011年11月
  • 2011年10月
  • 2011年9月
  • 2011年8月
  • 2011年6月
  • 2011年5月
  • 2011年4月
  • 2011年3月
  • 2011年2月
  • 2010年12月
  • 2010年11月
  • 2010年10月
  • 2010年9月
  • 2010年8月
  • 2010年6月
  • 2010年5月
  • 2010年2月
  • 2010年1月
  • 2009年12月
  • 2009年11月
  • 2009年10月
  • 2009年9月
  • 2009年8月
  • 2009年7月
  • 2009年6月
  • 2009年5月
  • 2009年4月
  • 2009年3月
  • 2009年2月
  • 2009年1月
  • 2008年8月
  • 2008年6月
  • 2008年5月
  • 2008年4月
  • 2008年2月
  • 2007年11月
  • 2007年8月
  • 2007年6月
  • 2007年5月
  • 2007年4月
  • 2007年3月
  • 2007年2月
  • 2007年1月
  • 2006年10月
  • 2006年8月
© 2006 - 2023 久久日记本
Powered by WordPress | Theme: Graphy for 99diary