利用SQL创建表结构

一、创建图书管理系统,其中涉及到的对象有(图书分类,图书,学生,借书记录)

1、列出关系模式

(1) 书本类别(种类编号,种类名称)
(2) 学生(学生编号,学生姓名,学生性别,学生种类)
(3) 图书(书籍编号,书籍名称,书籍种类编号,书籍作者,书籍出版社,出版日期,登记日期,是否被借出)
(4) 借阅记录(书籍编号,学生编号,借书时间,还书时间)

2、数据库各表实现

–创建书本类别表
create table book_class
(
bookclassno varchar2(30) primary key,–种类编号
bookclass varchar2(30) --种类名称
);
–创建图书表
create table book_system
(
bookno varchar2(64) primary key, --书籍编号
bookname varchar2(64) not null, --书籍名称
bookclassno varchar2(30) not null, --书籍种类编号
bookauthor varchar2(64) not null , --书籍作者
bookpub varchar2(30), --书籍出版社
bookpuddate date, --书籍出版日期
bookkindate date, --书籍登记日期
isborrowed varchar2(2) --是否被借出
);
–创建学生表
create table students
(
stu_no varchar2(20), --学生编号
stu_name varchar2(64) not null, --学生姓名
stu_sex varchar2(2), --学生性别
stu_type varchar2(64) --学生种类
);
–创建借书记录表
create table record
(
bookno varchar2(64) not null, --书籍编号
stu_no varchar2(20) not null, --学生编号
borrowdate date not null, --借书时间
returndate date not null --还书时间
);

二、创建表和添加数据,根据这些表查出每个员工的薪资水平

1、首先创建表和添加数据

create table a_demo_emp
(
empno varchar(64),–员工编号
ename varchar(10) not null,–员工姓名
age number(3) not null,–员工年龄
job varchar(50),–职位
mgr_no varchar(64),–上级领导工号
hiredate date not null,–入职时间
sal number(12,2),–工资
comm number(12,2),–提成
dept_no varchar(64),–部门号
constraint age_check check(age>18 and age<100),
constraint empno_PRIMARY primary key(empno)
)

create table a_demo_dept
(
deptno varchar(64),–部门编号
dname varchar(20) not null,–部门名称
loc varchar(100) not null, --所在地
constraint deptno_primarykey primary key(deptno)
)

create table a_demo_salleve
(
GRADE varchar(64),–等级名字
LOSAL number(12,2),–最低工资
HISAL number(12,2)–最高工资
)

insert into a_demo_emp(empno,ename,age,job,mgr_no,hiredate,sal,comm,dept_no)
select ‘7369’ as empno,‘SMITH’ as ename,25 as age,‘CLERK’ as job,‘7902’ as mgr_no,to_date(‘1980/12/17’,‘yyyy/mm/dd’) as hiredate,800 as sal,NULL as comm,‘20’ as dept_no from dual
union all
select ‘7499’ as empno,‘ALLEN’ as ename,30 as age,‘SALESMAN’ as job,‘7698’ as mgr_no,to_date(‘1981/2/20’,‘yyyy/mm/dd’) as hiredate,1600 as sal,300 as comm,‘30’ as dept_no from dual
union all
select ‘7521’ as empno,‘WARD’ as ename,28 as age,‘SALESMAN’ as job,‘7698’ as mgr_no,to_date(‘1981/2/22’,‘yyyy/mm/dd’) as hiredate,1250 as sal,500 as comm,‘30’ as dept_no from dual
union all
select ‘7566’ as empno,‘JONES’ as ename,22 as age,‘MANAGER’ as job,‘7839’ as mgr_no,to_date(‘1981/4/2’,‘yyyy/mm/dd’) as hiredate,2975 as sal,NULL as comm,‘20’ as dept_no from dual
union all
select ‘7654’ as empno,‘MARTIN’ as ename,27 as age,‘SALESMAN’ as job,‘7698’ as mgr_no,to_date(‘1981/9/28’,‘yyyy/mm/dd’) as hiredate,1250 as sal,1400 as comm,‘30’ as dept_no from dual
union all
select ‘7698’ as empno,‘BLAKE’ as ename,34 as age,‘MANAGER’ as job,‘7839’ as mgr_no,to_date(‘1981/5/1’,‘yyyy/mm/dd’) as hiredate,2850 as sal,NULL as comm,‘30’ as dept_no from dual
union all
select ‘7782’ as empno,‘CLARK’ as ename,42 as age,‘MANAGER’ as job,‘7839’ as mgr_no,to_date(‘1981/6/9’,‘yyyy/mm/dd’) as hiredate,2450 as sal,NULL as comm,‘10’ as dept_no from dual
union all
select ‘7788’ as empno,‘SCOTT’ as ename,31 as age,‘ANALYST’ as job,‘7566’ as mgr_no,to_date(‘1987/4/19’,‘yyyy/mm/dd’) as hiredate,3000 as sal,NULL as comm,‘20’ as dept_no from dual
union all
select ‘7839’ as empno,‘KING’ as ename,28 as age,‘PRESIDENT’ as job,NULL as mgr_no,to_date(‘1981/11/17’,‘yyyy/mm/dd’) as hiredate,5000 as sal,NULL as comm,‘10’ as dept_no from dual
union all
select ‘7844’ as empno,‘TURNER’ as ename,39 as age,‘SALESMAN’ as job,‘7698’ as mgr_no,to_date(‘1981/9/8’,‘yyyy/mm/dd’) as hiredate,1500 as sal,0 as comm,‘30’ as dept_no from dual
union all
select ‘7876’ as empno,‘ADAMS’ as ename,29 as age,‘CLERK’ as job,‘7788’ as mgr_no,to_date(‘1987/5/23’,‘yyyy/mm/dd’) as hiredate,1100 as sal,NULL as comm,‘20’ as dept_no from dual
union all
select ‘7900’ as empno,‘JAMES’ as ename,24 as age,‘CLERK’ as job,‘7698’ as mgr_no,to_date(‘1981/12/3’,‘yyyy/mm/dd’) as hiredate,950 as sal,NULL as comm,‘30’ as dept_no from dual
union all
select ‘7902’ as empno,‘FORD’ as ename,25 as age,‘ANALYST’ as job,‘7566’ as mgr_no,to_date(‘1981/12/3’,‘yyyy/mm/dd’) as hiredate,3000 as sal,NULL as comm,‘20’ as dept_no from dual
union all
select ‘7934’ as empno,‘MILLER’ as ename,29 as age,‘CLERK’ as job,‘7782’ as mgr_no,to_date(‘1982/1/23’,‘yyyy/mm/dd’) as hiredate,1300 as sal,NULL as comm,‘10’ as dept_no from dual;

insert into a_demo_salleve(grade,losal,hisal)
select ‘1’,700,1200 from dual
union all
select ‘2’,1201,1400 from dual
union all
select ‘3’,1401,2000 from dual
union all
select ‘4’,2001,3000 from dual
union all
select ‘5’,3001,5000 from dual;

下图为a_demo_emp表中的数据
在这里插入图片描述
下图为a_demo_salleve表中的数据
在这里插入图片描述

2、查出每个员工的薪资水平

select * from a_demo_emp e;
select * from a_demo_salleve sl;

select e.empno,e.ename,e.sal,sl.grade
from a_demo_emp e
join a_demo_salleve sl
on e.sal>=sl.losal and e.sal<=sl.hisal
结果如图所示
在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页