当前位置: 数据库>sqlserver
MSSQL 基本语法及实例操作语句
来源: 互联网 发布时间:2014-10-07
本文导语: MS SQL基本语法及实例操作 一:建表并初始化 ============================ create database mf2011 --创建数据库 use mf2011 --使用数据库 create table dept --创建“部门”表 ( www. deptno int primary key, --部门编号(主键) dname nvarchar(30), --部门名称 loc ...
MS SQL基本语法及实例操作
一:建表并初始化
============================
create database mf2011 --创建数据库
use mf2011 --使用数据库
create table dept --创建“部门”表
( www.
deptno int primary key, --部门编号(主键)
dname nvarchar(30), --部门名称
loc nvarchar(30) --部门所在的地点
)
-----
create table emp --创建“员工”表
(
empno int primary key, --员工编号(主键)
ename nvarchar(30), --员工的姓名
job nvarchar(30), --员工的工作类型
mgr int, --员工的上级
hiredate datetime, --员工的入职时间(受聘时间)
sal numeric(10,2), --员工的每月工资
comm numeric(10,2), --员工的年终奖金
deptno int foreign key references dept(deptno) --创建一个外键指向部门表(描述该员工属于哪个部门)
)
--------
insert into dept values (10,'accounting','new york')
insert into dept values (20,'reasarch','dallas')
insert into dept values (30,'sales','chicago')
insert into dept values (40,'operations','boston')
-------
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7369,'michael','clerk',7902,'2010-1-12',675.23,300,20)
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7499,'allen','salesman',7698,'2009-1-23',1675.23,322.50,30)
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7521,'ward','salesman',7698,'2008-1-3',12675.99,399.50,30)
insert into emp (empno,ename,job,mgr,hiredate,comm,deptno) values
(7566,'jones','manager',7839,'2000-1-1',8675.99,20)
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7654,'martin','salesman',7698,'2007-12-31',1275.99,999.00,30)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7782,'blake','manager',7839,'2007-12-20',1275.99,30)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7788,'sccot','analyst',7566,'2003-1-22',1275.99,10)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7781,'miller','opreator',7566,'2005-10-12',1275.99,40)
www.
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7744,'adamc','opreator',7566,'2006-10-30',1244.0,40)
insert into emp (empno,ename,job,hiredate,sal,deptno) values
(7839,'king','president','2000-1-1',100244.0,10)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(1999,'lxliog','opreator',7566,'2006-10-30',1244.0,40)
-----
select * from dept
select * from emp
--主键:唯一,非空
--外键:只能指向主键,与指向的主键数据类型必须一致
二:练习
============================
1,查询emp表所有列
select * from emp --刘君正
2,查询指定列(例如:姓名,工资两列)
select ename,sal from emp --郑朝阳
3,取消重复行(从emp表中查询部门号)
select distinct(deptno) from emp --徐绍峰,郝艳芳,刘辉,马东勤
4,从emp表中查询名字为lxliog员工的薪水,工作,所作部门
select sal,job,deptno from emp where 'lxliog'=ename --侯耀文,谭雪玲,黎小龙
5,统计总共有都少个部门(两种方式:从emp或从dept)
select count(deptno) from dept --诺布才仁
select count(distinct(deptno)) as 部门总数 from emp --诺布才仁
use mf2011
select * from dept;
select * from emp;
--6,显示每个雇员的年工资
select isnull(sal,0)*12+isnull(comm,0) as 年薪 from emp --扎西多杰
7,如何显示工资高于3000的员工信息
select * from emp where sal>3000 --邓文文
www.
8,如何查找1982.1.1后入职的员工
select * from emp where hiredate>'1982-1-1'
9,如何显示工资在2000到2500之间的员工情况
select * from emp where sal between 1244 and 100244
select * from emp where sal>=2000 and sal0 and nums
一:建表并初始化
============================
create database mf2011 --创建数据库
use mf2011 --使用数据库
create table dept --创建“部门”表
( www.
deptno int primary key, --部门编号(主键)
dname nvarchar(30), --部门名称
loc nvarchar(30) --部门所在的地点
)
-----
create table emp --创建“员工”表
(
empno int primary key, --员工编号(主键)
ename nvarchar(30), --员工的姓名
job nvarchar(30), --员工的工作类型
mgr int, --员工的上级
hiredate datetime, --员工的入职时间(受聘时间)
sal numeric(10,2), --员工的每月工资
comm numeric(10,2), --员工的年终奖金
deptno int foreign key references dept(deptno) --创建一个外键指向部门表(描述该员工属于哪个部门)
)
--------
insert into dept values (10,'accounting','new york')
insert into dept values (20,'reasarch','dallas')
insert into dept values (30,'sales','chicago')
insert into dept values (40,'operations','boston')
-------
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7369,'michael','clerk',7902,'2010-1-12',675.23,300,20)
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7499,'allen','salesman',7698,'2009-1-23',1675.23,322.50,30)
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7521,'ward','salesman',7698,'2008-1-3',12675.99,399.50,30)
insert into emp (empno,ename,job,mgr,hiredate,comm,deptno) values
(7566,'jones','manager',7839,'2000-1-1',8675.99,20)
insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7654,'martin','salesman',7698,'2007-12-31',1275.99,999.00,30)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7782,'blake','manager',7839,'2007-12-20',1275.99,30)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7788,'sccot','analyst',7566,'2003-1-22',1275.99,10)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7781,'miller','opreator',7566,'2005-10-12',1275.99,40)
www.
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(7744,'adamc','opreator',7566,'2006-10-30',1244.0,40)
insert into emp (empno,ename,job,hiredate,sal,deptno) values
(7839,'king','president','2000-1-1',100244.0,10)
insert into emp (empno,ename,job,mgr,hiredate,sal,deptno) values
(1999,'lxliog','opreator',7566,'2006-10-30',1244.0,40)
-----
select * from dept
select * from emp
--主键:唯一,非空
--外键:只能指向主键,与指向的主键数据类型必须一致
二:练习
============================
1,查询emp表所有列
select * from emp --刘君正
2,查询指定列(例如:姓名,工资两列)
select ename,sal from emp --郑朝阳
3,取消重复行(从emp表中查询部门号)
select distinct(deptno) from emp --徐绍峰,郝艳芳,刘辉,马东勤
4,从emp表中查询名字为lxliog员工的薪水,工作,所作部门
select sal,job,deptno from emp where 'lxliog'=ename --侯耀文,谭雪玲,黎小龙
5,统计总共有都少个部门(两种方式:从emp或从dept)
select count(deptno) from dept --诺布才仁
select count(distinct(deptno)) as 部门总数 from emp --诺布才仁
use mf2011
select * from dept;
select * from emp;
--6,显示每个雇员的年工资
select isnull(sal,0)*12+isnull(comm,0) as 年薪 from emp --扎西多杰
7,如何显示工资高于3000的员工信息
select * from emp where sal>3000 --邓文文
www.
8,如何查找1982.1.1后入职的员工
select * from emp where hiredate>'1982-1-1'
9,如何显示工资在2000到2500之间的员工情况
select * from emp where sal between 1244 and 100244
select * from emp where sal>=2000 and sal0 and nums