码迷,mamicode.com
首页 > 数据库 > 详细

基础数据库脚本12年编写

时间:2016-08-26 12:14:28      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

--新建数据库
create database admin
--新建表同时建字段
create table student
(
ID int,
namee nvarchar(5),
age nchar(1),
sex int
)

--删除表
drop table student
--查询
select * from student
--查询指定的字段
select ID,age from student
--小名或者别名
select namee 姓名,age 年龄from student
--添加记录
insert into student values(1,王建国,,61)
insert into student values(2,李刚,,55)
insert into student values(4,张家辉,,64)
--添加部分字段()里的是需要添加的
insert into student (ID,namee,sex) values(3,刘能,69)
--删除全部记录
delete from student
--删除一条记录
delete from student where ID=1
delete from student where sex<60
delete from student where namee=刘能
--按条件查询
select ID,namee,sex from student where sex<60
--修改记录
update student set sex=56 where sex=40
update student set sex=sex+1 where sex<60

-------------------------------------------------------------------------
--数据类型
char给多少就有多少字符串varchar可变长字符串,指定长度
nchar和nvarchar是在前者的基础上采用了Unicode编码
char  varchar   含英文的
nchar nvarchar  含中文的
Unicode编码的特点是字节和汉字占用的空间一样 (要用到中午就在前面加一个n)
int        bigint        numeric(6.2)-(更加精准,好控制)
datetime    timestamp  --时间
getdate()  得到当前时间
--测试时间
create table timee(timee datetime)
insert into timee values(getdate())
select *from timee
----------------------------------------------------------------------------
--主键 (唯一标示)不能重复不为空
create table teacher
(
 ID int primary key,--primary key表示主键
namee nvarchar(5),
age int
)
delete from teacher where ID=4
insert into teacher values(1,王建国,61)
insert into teacher values(2,李刚,55)
insert into teacher (ID,namee) values(3,刘能)--插入部分字段主键必须插入
insert into teacher values(4,李刚,55)
select * from teacher
update teacher set age=69 where age is null
delete from teacher where age=61 and namee=李刚
delete from teacher where age=61 or namee=李刚
select ID,namee,age from teacher where namee=李刚or age=61

---------------------- 外键---------------------
--外键只能指向主键并且类型要一致
create database Supermarket
drop table bumen
create table bumen
(
ID int primary key,--主键
cname nvarchar(10),
address nvarchar(10)
)
create table person
(
number int primary key,
pname nvarchar(10),
work nvarchar(10),--职务
shangji int ,
ptime datetime,--入职时间
Wage numeric(6,1),--工资
buzhu numeric(5,1),
ID int foreign key references bumen(ID)--外键
)
select * from bumen
select * from person
--修改员工的上级
update person set ptime=2014-05-14 where pname=王翔
insert into bumen values(1,永辉喷水池店,喷水池)
insert into bumen values(2,永辉观山湖店,观山湖)
insert into bumen values(3,永辉世纪城店,世纪城)
insert into bumen values(4,永辉花果园店,花果园)
insert into bumen values(5,永辉火车站店,火车站)
select number 编号,pname 姓名,work 职务,shangji 上级,ptime 入职时间,Wage 工资,buzhu 补助,ID ID from person
delete from person where work=员工
insert into person (number,pname,work,ptime,Wage,buzhu,ID) values(101,张轩松,董事长,1998-01-02,50000,8000,1)
insert into person values(102,李明星,总经理,101,2010-05-23,20000,5000,1)
insert into person values(103,旺小涵,区域经理,102,2010-05-23,15000,5500,1)
insert into person values(104,陈明尔,区域经理,102,2010-05-23,15000,5500,2)
insert into person values(105,王星宇,部门经理,104,2010-05-23,10000,1000,3)
insert into person(number,pname,work,shangji,ptime,Wage,ID)values(106,张雨生,员工,104,2011-08-15,2800,3)
insert into person(number,pname,work,shangji,ptime,Wage,ID)values(107,王秘书,员工,104,2012-01-13,2500,3)
insert into person(number,pname,work,shangji,ptime,Wage,ID)values(108,石阿姨,员工,104,2013-12-02,2000,3)
insert into person(number,pname,work,shangji,ptime,Wage,ID)values(109,蒋敏平,员工,104,2013-12-02,2200,3)
insert into person(number,pname,work,shangji,ptime,Wage,ID)values(110,李安静,员工,104,2013-12-02,2000,4)
insert into person(number,pname,work,shangji,ptime,Wage,ID)values(111,王翔,员工,104,2013-12-02,2200,5)
--查询王星宇的编号,姓名,职务,工资
select number,pname,work,Wage from person where pname=王星宇
--查询一共多少部门 distinct  只显示重复记录的一个
select distinct ID from person
--查询区域经理的姓名和年收入
select pname 姓名,Wage*12 年薪from person where work=区域经理
--计算年总收入 和空值做四则运算
select pname 姓名,wage*12+isnull(buzhu,0)*12 年薪from  person
--查询-01-01以后入职的员工
select pname,ptime from person where ptime>2012-1-1and ptime<2014-1-1
--查询工资在到的
select pname,wage from person where wage>2000 and wage<=10000
--模糊查询
select pname from person where pname like王%
select pname from person where pname like__宇%
--批量查询
select pname,number from person where number in(101,105,109)
select pname,work from person where shangji is null
--排序默认升序 desc降序
select number,pname,wage from person order by wage desc
select number,pname,wage from person order by pname
select ID,pname,wage from person order by ID desc,wage desc
select pname姓名,wage*12+isnull(buzhu,0)*12年薪 from  person order by 年薪 desc --别名方法显示排序
--显示总工资和平均工资
select sum(wage)总工资,avg(wage)平均工资 from person
select pname,wage from person where wage=(select max(wage) from person)
select pname,wage from person where wage=(select min(wage) from person)
select pname,wage,(select avg(wage)from person) from person where wage>(select avg(wage) from person)
--统计有多少记录
select count(*) from person
--统计每个部门的平均工资和总工资group by后面的字段不需显示字段中
select ID,avg(wage)平均工资,sum(wage)总工资 from person group by ID
--显示每个部门的每个职务的平均工资和最低工资
select ID,avg(wage)平均工资,min(wage)最低工资,work from person group by ID,work order by ID
--平均工资低于的部门编号和平均工资(having)再次查询
select id,avg(wage) from person group by id having avg(wage)>10000

-------------多表查询--------------------------
select * from bumen,person
select * from bumen,person where bumen.address=喷水池
select * from bumen,person where bumen.address=喷水池 and person.ID=bumen.id
select * from bumen,person where person.ID=bumen.id
--显示姓名和所在部门以及部门编号
select pname,cname,person.ID from bumen,person where person.ID=bumen.ID
select pname,cname,person.ID from bumen,person where (person.ID=bumen.ID)and(bumen.id=3)
select pname,cname,person.ID from bumen,person where person.ID=bumen.ID order by bumen.ID
--查询张玉生的上级
select shangji from person where pname=张雨生
select pname from person where number=(select shangji from person where pname=张雨生)
--查询所有员工及其上级的姓名
select a.pname,b.pname from person a,person b where a.shangji=b.number
--查询高于部门平均工资的员工和工资,部门编号以及部门的平均工资首先得到每个部门的平均工资
select avg(wage),ID from person group by id
select pname,wage,avgwage,person.ID from person,(select avg(wage) avgwage,ID from person group by id) lsb
where (person.ID=lsb.ID) and (person.wage>lsb.avgwage) 
--分页查询
--查询第个到第个入职的职工
select top 5 pname,work,ptime from person order by ptime
----查询第个到第个入职的职工
select top 7 number,pname,work,ptime from person where number not in (select top 3 number from person order by ptime) order by ptime


----------------删除重复的记录----------------------
select * from student
select distinct * from student
select distinct *into lsStudent from student
delete from student
insert into student select * from lsStudent
drop table lsStudent

---------外连接---
select a.pname,b.pname 上级from person a left join person b on a.shangji=b.number---张轩松的上级为空

 

基础数据库脚本12年编写

标签:

原文地址:http://www.cnblogs.com/hllive/p/5809717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!