1. There is an order management database, which includes warehouse table, employee table, purchase order form, and supplier table as follows: Warehouse table (warehouse number, city, area) Employee table (warehouse number, employee number, salary) Purchase order form (employee number, supplier number, purchase order number, order date) Supplier Table (Supplier Number, Supplier Name, City) Please use SQL statements to complete the following operations (3 points for each question from 1-12, 4 points for 13 questions, a total of 40 points). : 1. Retrieve all wage values from the employee relationship, and require no duplicate values in the results. 2. Search for employee numbers with salaries of more than 1,230 yuan and their city. 3. Search for the employee number of the warehouse with an area greater than 400 and the city where these employees work. 4. Find out which cities have at least one warehouse with a salary of 1250 yuan 5. Search for information on warehouses where the wages of all employees are more than 1210 yuan. 6. Sort by warehouse number in ascending order, then sort by salary in descending order and output all employee information. 7. Obtain the sum of the wages of warehouse workers in Beijing and Shanghai. 8. Seek the maximum salary value of employees working in the WH2 warehouse. 9. Obtain the average salary of each warehouse with at least two employees. 10. Retrieve all supplier information not in Beijing. 11. List the purchase order information with the highest total amount handled by each employee. 12. Search for information on which warehouses do not have employees. 13. Search for the warehouse number where the salary of the employee is greater than or equal to the salary of any employee in the wh1 warehouse
2. The existing relational database is as follows: Database Name: Teacher Database Teacher Table (number char(6), name, gender, ethnicity, job title, ID number) Course schedule (course number char(6), name) Class schedule (ID, teacher number, class number, number of class hours) SQL statement code that implements the following functions in SQL language (5 points per question, 40 points in total): 1. Create the database and table creation code of the above three tables; Required use: primary key (teacher table. Number, Curriculum. Course number), foreign key (class schedule. Teacher number, class schedule. Class number), default (ethnicity), non-empty (ethnicity, name), unique (ID number), check (gender, number of class hours), automatic number (ID) 2. Add the following course information to the code of the course schedule (1) Course number Course name 100001 SQL Server database 100002 Data structures 100003 VB programming (2) Change the course name of the course number 100003: Visual Basic Programming (3) Delete the course information with the course number 100003 3. Write out the code to create [Class Schedule View] (teacher number, name, course number, course name, class hours); 4. Write out the embedded table value function and retrieval code to create [a course teacher]; Search: Names of all teachers for the course 'SQL Server Database'; 5. Write out and create [Count Class Hours]: output the maximum number of class hours, the minimum number of class hours, the storage procedure of the average class hours, and the execution code. 6. Write out and create: calculate the total class hours of a teacher's substitute teaching, and return the value to the stored procedure and execution code. Implementation: Calculate the total class hours of "Teacher Guo". 7. Retrieve the information of all teachers with one or more courses with more than 90 class hours, including numbers and names. 8. Create a rule and bind it to the title column of the teacher table, and stipulate that the value is one of ('Professor', 'Associate Professor', 'Lecturer', 'Teaching Assistant').
3. There is a talent information management database, which has three tables. Talent Basic Information Form RCDA (number, name, gender, date of birth, salary status) Talent Professional Specialty Table ZYTC (number, major, professional years, professional title, English proficiency) Talent Achievement Achievement Table CJDA (Number, Achievement Name, Achievement Category, Achievement Source) Please use SQL language to complete the following operations (4 points per question, 40 points in total): 1. Query all information in the RCDA table; 2. Query the number, name, gender, date of birth, and salary status, and arrange them in descending order according to the field values of the number. 3. Query number, name, gender, date of birth, and salary status, and require the search for records of salary status exceeding 5,000 yuan or less than 3,000 yuan, and the query results are arranged in ascending order according to the value of the salary status field. 4. Query the information of all fields in the RCDA table and ask to select the records of all gay men. 5. Check the names and salary status of all records in the RCDA form; 6. Check the name, gender and salary status of all men in the RCDA table, and sort them in descending order of salary status; 7. Check the name and date of birth of employees aged 20-30 in the RCDA form; 8. Find the sum of everyone's wages in the RCDA table, and change the column heading to "Sum of wages"; 9. Check the sum of wages of male and female employees in the RCDA table; 10. Check the average salary of all owners in the RCDA table;
- use master
- --删除数据库
- if exists (select * from sysdatabases where name='订货管理')
- DROP database 订货管理
- GO
- --创建数据库
- CREATE DATABASE 订货管理
- GO
- --切换数据库
- USE 订货管理
- Go
- --1/创建仓库表(仓库号,城市,面积)
- create table 仓库表
- (
- 仓库号 char(4) primary key,
- 城市 varchar(4) not null,
- 面积 int
- )
- go
- --2/创建供应商表(供应商号,供应商名,城市)
- create table 供应商表
- (
- 供应商号 char(2) primary key,
- 供应商名 char(2),
- 城市 varchar(10)
- )
- Go
- --3/创建职工表(仓库号,职工号,工资)
- create table 职工表
- (
- 仓库号 char(4) not null references 仓库表(仓库号),
- 职工号 char(2) primary key,
- 工资 int
- )
- Go
- --4/创建订购单表(职工号,供应商号,订购单号,订购日期)
- create table 订购单表
- (
- 职工号 char(2) not null references 职工表(职工号),
- 供应商号 char(2) references 供应商表(供应商号),
- 订购单号 char(2) primary key,
- 订购日期 datetime
- )
- Go
- --1、 从职工关系中检索所有工资值,要求结果中没有重复值。
- select 职工号,工资 from 职工表
- --2、 检索工资多于1230元的职工号和他们所在的城市。
- select 城市,职工号 from 仓库表
- inner join 职工表 on 仓库表.仓库号=职工表.仓库号
- where 工资>1230
- --3、 检索工作在面积大于400的仓库的职工号以及这些职工工作所在的城市。
- select 职工号,城市 from 职工表
- inner join 仓库表 on 职工表.仓库号=仓库表.仓库号
- where 面积>400
- --4、 检索出哪些城市至少有一个仓库的职工工资为1250元
- select 城市 from 仓库表 where 仓库号 in
- (select 仓库号 from 职工表 where 工资=1250)
- --5、 检索所有职工的工资都多于1210元的仓库的信息。
- select * from 仓库表 where 仓库号 in
- (select 仓库号 from 职工表
- except
- select 仓库号 from 职工表 where 工资<=1210)
- --6、 先按仓库号升序排序,再按工资降序排序并输出全部职工信息。
- select * from 职工表 order by 仓库号,工资
- --7、 求北京和上海的仓库职工的工资总和。
- select sum(工资) from 职工表 where 仓库号 in
- (select 仓库号 from 仓库表 where 城市='北京' or 城市='上海')
- --8、 求在wh2仓库工作的职工的最高工资值。
- select MAX(工资) from 职工表 where 仓库号='wh2'
- --9、 求至少有两个职工的每个仓库的平均工资。
- select 仓库号,AVG(工资) as 职工平均工资 from 职工表
- group by 仓库号
- having COUNT(职工号)>=2
- --10、 检索出不在北京的全部供应商信息。
- select * from 供应商表 where 城市<>'北京'
- --11、 列出每个职工经手的具有最高总金额的订购单信息。
- --12、 检索哪些仓库中还没有职工的仓库的信息。
- select * from 仓库表 where 仓库号 in
- (select 仓库号 from 仓库表
- Except
- select 仓库号 from 职工表)
- --13、 检索有职工的工资大于或等于wh1仓库中任何一名职工工资的仓库号
- select distinct 仓库号 from 职工表
- where 工资 >= (select min(工资) from 职工表 where 仓库号='wh1')
-
Copy code
- --1、
- create database 教师数据库
- go
- use 教师数据库
- go
- create table 教师表
- (
- 编号 char(6) primary key,
- 姓名 nchar(4) not null,
- 性别 nchar(1) check([性别] in ('男', '女')),
- 民族 nchar(8) default '汉族' not null,
- 职称 nchar(12),
- 身份证号 char(18) unique )
- go
- create table 课程表
- (课号 char(6) primary key,
- 名称 char(40) not null
- )
- go
-
- create table 任课表
- (
- ID int IDENTITY(1, 1),
- 教师编号 char(6) references 教师表(编号),
- 课号 char(6) references 课程表(课号),
- 课时数 integer check([课时数] between 0 and 200)
- )
- go
- --2、
- insert 课程表 values('100001', 'SQL Server数据库')
- insert 课程表 values('100002', '数据结构')
- insert 课程表 values('100003', 'VB程序设计')
- update 课程表 set 名称='Visual Basic程序设计' where 课号='100003'
- delete 课程表 where 课号='100003'
- --3、
- create view [任课表视图] as
- select 教师编号,姓名,任课表.课号,名称,课时数 from 教师表
- inner join 任课表 on 教师表.编号=任课表.教师编号
- inner join 课程表 on 课程表.课号=任课表.课号
- --4、
- create function [某门课任课教师](@课程名 varchar(15))
- returns table as
- return (select 名称,课时数,教师姓名=姓名 from 任课表视图 where 名称=@课程名)
- go
- select * from [某门课任课教师]('SQL Server数据库')
- --5、
- create procedure [统计课时数]
- as
- select 最大课时数=max(课时数) ,最小课时数=min(课时数),平均课时数=avg(课时数)
- from 任课表
- go
- execute [统计课时数]
- --6、
- create procedure [统计课时]
- @教师名 nchar(16)
- as
- declare @总课时 int
- select @总课时=sum(课时数) from 任课表视图
- where 姓名=@教师名
- go
- execute [统计课时] '郭老师'
- --7、
- select 编号, 姓名 from 教师表
- where 编号 in (select distinct 教师编号 from 任课表 where 课时数>=90)
- 8、
- create rule zhicheng_rule
- as @zhicheng in ('教授','副教授','讲师', '助教')
- go
- sp_bindrule zhicheng_rule, '教师表.职称'
Copy code
- use master
- --删除数据库
- if exists (select * from sysdatabases where name='人才信息管理')
- DROP database 人才信息管理
- GO
- --创建数据库
- CREATE DATABASE 人才信息管理
- GO
- --切换数据库
- USE 人才信息管理
- Go
- --人才基本情况表RCDA(编号,姓名,性别,出生日期,工资现状)
- create table RCDA
- (
- 编号 char(4) primary key,
- 姓名 varchar(10) not null,
- 性别 varchar(2) not null,
- 出生日期 datetime,
- 工资现状 int
- )
- go
- --人才专业特长表ZYTC(编号,专业,专业年限,职称,英语水平)
- create table ZYTC
- (
- 编号 char(4) primary key,
- 姓名 varchar(10) not null,
- 性别 varchar(2) not null,
- 出生日期 datetime,
- 工资现状 int
- )
- go
- --人才成就成果表CJDA(编号,成果名称,成果类别,成果出处)
- create table CJDA
- (
- 编号 char(4) primary key,
- 成果名称 varchar(10) not null,
- 成果类别 varchar(10) not null,
- 成果出处 varchar(10) not null
- )
- go
- --1.查询RCDA表中的所有信息;
- Select * from RCDA
- --2. 查询编号、姓名、性别、出生日期、工资现状,并按编号的字段值降序排列。
- Select 编号,姓名,性别,出生日期,工资现状 from RCDA
- order by 编号 desc
- --3.查询编号、姓名、性别、出生日期、工资现状,要求查询出工资现状超过5000,或少于3000元的记录,且查询结果按工资现状字段值升序排列。
- Select 编号,姓名,性别,出生日期,工资现状 from RCDA
- Where 工资现状>5000 or 工资现状<3000 order by 工资现状
- --4.查询RCDA表中所有字段的信息,要求选择所有男同志的记录。
- Select * from RCDA where 性别='男'
- --5.在RCDA表中查询所有记录的姓名和工资现状的情况;
- Select 姓名,工资现状 from RCDA
- --6.在RCDA表中查询所有男性的姓名、性别和工资现状的情况,并按工资现状降序排序;
- Select 姓名,性别,工资现状 from RCDA order by 工资现状 desc
- --7. 在RCDA表中查询年龄在20-30岁间的职工的姓名,出生日期;
- select 姓名,出生日期,datediff(yy,出生日期,getdate()) as 年龄 from RCDA
- where datediff(yy,出生日期,getdate()) between 20 and 30
- --8. 在RCDA表中求所有人的工资总和,并将列标题改为“工资总和”;
- Select sum(工资现状) as 工资总和 from RCDA
- --9. 在RCDA表中查询男职工和女职工的工资总和;
- Select 性别,sum(工资现状) as 工资总和 from RCDA group by 性别
- --10. 在RCDA表中查询所有人的平均工资;
- Select avg(工资现状) from RCDA
Copy code
|