数据库中表的简单查询、排序和连接

      该篇博文关于数据库中对表的简单查询、排序和连接均是以下面三张表为基础进行的操作。在每一条操作语句下对应有相关的操作说明。

  

查询分析器下代码附上:

create database YGGL;
use YGGL;
create table Employees
(
   EmployeeID char(6) primary key,
   Name char(10) not null,
   Birthday datetime not null,
   Sex bit  default 1,
   Address  varchar(40),
   Zip  char(6),
   PhonNumber   char(12),
   Email varchar(30),
   DepartmentID char(3) not null foreign  key references Departments(DepartmentID)
);

create table Departments
(
   DepartmentID char(3) not null  primary key,
   DepartmentName char(20) not null unique,
   Note text

);

create table  Salary
(

   EmployeeID  char(6) foreign key references Employees(EmployeeID)  primary key,     
   Income float not null,
   Outcome float not null

);

select * from Employees;
update Employees set Sex=0 where EmployeeID in('101','104','106','109')
insert into Employees ( EmployeeID ,Name , Birthday , Address ,Zip,PhonNumber ,Email ,DepartmentID )
values ('101','张三','1991-1-1','安徽','1001','1515594','[email protected]','110') 
,('102','李四','1992-1-2','六安','1002','1515595','[email protected]','111') 
,('103','王麻子','1993-1-4','马鞍山','1003','1515596','[email protected]','112')
, ('104','阿斗','1994-1-5','巢湖','1004','1515597','[email protected]','113')
,('105','阿豆','1995-1-7','霍邱','1005','1515598','[email protected]','114')
,('106','王五','1996-2-1','金安','1006','1515599','[email protected]','115')
,('107','豆豆','1997-3-1','裕安','1007','1515591','[email protected]','116')
, ('108','嘟嘟','1997-4-1','大别山','1008','1515592','[email protected]','117')

select *from Departments;
insert into Departments
values('110','人事部','人事相关'),
('111','消防部','消防相关')
,('112','纪检部','纪检相关')
,('113','纪检部','纪检相关')
,('114','信工部','信息相关')
,('115','文艺部','文艺相关')
,('116','党团部','党团相关')
,('117','思政部','思政相关')


select *from  Salary;
insert into Salary
values('101',2000,500)
,('102',1000,600)
,('103',1500,1000)
,('104',1600,1000)
,('105',6000,1500)
,('106',6000,1000)
,('107',7000,1000)
,('108',8000,1500)


update Salary set Income = 2890 where EmployeeID='101'              --修改职工号是101的工资为2890
update Salary  set Income=Income+200 where Income<2000              --修改工资低于2000元的并加上200
---delete from Salary where Income<2000                                --删除工资低于2000的员工信息
delete from Employees where EmployeeID ='13'                        --删除多余的第十行,莫名其妙多出来的              
select *from Employees                                                 --查询Employees中的所有员工信息
select *from Departments                                                --查询Departments中所有记录
select *from Salary                                                     --查询Salary 中的所有记录
select EmployeeID , Address,PhonNumber from Employees                              --查询每个员工的地址和联系方式
select DepartmentID  , DepartmentName from Departments                              --查询每个部门的部门号和部门名
select Name as 姓名,Address as 地址,DepartmentID as 部门号 from Employees where Sex='1'                     --	查询所有女员工的姓名和地址及部门号,并用as子句将结果中各列的标题分别指定为姓名和地址及部门号。
select EmployeeID ,实际收入 =(Income-Outcome) from  Salary                                      --计算每个员工的实际收入
select EmployeeID,Income from  Salary  where Income between 2000 and 3000                      --	找出所有收入在2000~3000之间的员工编号。
select *from Employees order by   Birthday                                                     ---查询员工的基本信息并出生时间先后排序。
select DepartmentID ,Name from Employees where DepartmentID in('110','111')                   --找出所有在部门‘1’或部门‘2’工作的员工的部门号及姓名。
select Name ,DepartmentID from Employees where Name like '张%'                      ---找出所有姓王的员工的姓名及部门号。
select Name,Address from Employees where  Address  like '%六安%'                     --找出所在其地址中含有“中山”的员工的姓名及地址
select s.*,Income,Outcome, DepartmentName,Note from Employees  s join Salary on s.EmployeeID=Salary.EmployeeID join  Departments on s.DepartmentID=Departments.DepartmentID     --- 查询每个员工的基本信息及其薪水情况。
select s.*,DepartmentName, Note from Employees as  s join  Departments on s.DepartmentID=Departments.DepartmentID    --、查询每个员工的情况及其工作部门的情况。
select Name,Income,Outcome from Employees S join Salary on S.EmployeeID=Salary.EmployeeID join Departments on S.DepartmentID=Departments.DepartmentID
where Income>1000 and  DepartmentName='纪检部'                                             --   查找财务部收入在2200以上的员工的姓名及其薪水详情
select Name,Income,Outcome, birthday  from Employees S  join Salary on S.EmployeeID=Salary.EmployeeID join Departments on S.DepartmentID=Departments.DepartmentID
where birthday<'1996' and DepartmentName='纪检部'                                             ----查找研发部在1966年以前出生的员工的姓名及其薪水情况。
select s.* ,departmentname, note ,income ,outcome from  Employees S join Salary on S.EmployeeID=Salary.EmployeeID join Departments on S.DepartmentID=Departments.DepartmentID
order by income                                                                                      ---将各员工的情况按收入由低到高排序。

SQL server数据库的功能:



SQL server中常见的几种命令动词的语法:

(1)drop语法:

      1、DROP TABLE 语句用于删除表(表的结构、属性以及索引也会被删除)

                  语法格式:         

DROP TABLE 表名称

  说明:只能删除自己创建的表,不能删除其他用户创建的表。

    2、DROP DATABASE 语句用于删除数据库   

                语法格式:

DROP DATABASE 数据库名称

说明:一个drop语句可以一次删除 多个数据库。   


(2)update用于修改表中的数据

语法格式: 

UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值

(3)delete用于删除表中的行

语法格式:

DELETE FROM 表名称 WHERE 列名称 = 值


(4)insert  into用于向表格中插入新的行

INSERT INTO 表名称 VALUES (值1, 值2,....)

(5)alter table可用于在已有的表中添加、修改或者删除列

   语法格式:

如需在表中添加列,请使用下列语法:

ALTER TABLE table_name
ADD column_name datatype

要删除表中的列,请使用下列语法:

ALTER TABLE table_name 
DROP COLUMN column_name

注释:某些数据库系统不允许这种在数据库表中删除列的方式 (DROP COLUMN column_name)。

要改变表中列的数据类型,请使用下列语法:

ALTER TABLE table_name
ALTER COLUMN column_name datatype




猜你喜欢

转载自blog.csdn.net/sunchanglan151/article/details/80157097