SQL

2017-09-20  本文已影响21人  zlcook
 #院系表
create table department(
 dept_name varchar(20),
 building varchar(15),
 budget numeric(12,2),
 primary key (dept_name)
);
#课目表
create table course(
 course_id varchar(7),
 title varchar(50),
 dept_name varchar(20),
 credits numeric(2,0),
 primary key (course_id),
 foreign key (dept_name) references department
);
#教师表
create table instructor(
 ID varchar(5),
 name varchar(20) not null,
 dept_name varchar(20),
 primary key (ID),
 foreign key (dept_name) references department
);
#排课表
create table section(
 course_id varchar(8),
 sec_id varchar(8) ,
 semester varchar(6),
 year numeric(4,0),
 building varchar(15),
 room_number varchar(7),
 time_slot_id varchar(4),
 primary key (course_id,sec_id,semester,year),
 foreign key (course_id) references course
);
#教师授课表
create table teaches(
 ID varchar(5),
 course_id varchar(8),
 sec_id varchar(8) ,
 semester varchar(6),
 year numeric(4,0),
 primary key (ID,course_id,sec_id,semester,year),
 foreign key (course_id,sec_id,semester,year) references section,
 foreign key(ID) references instructor
);

笛卡尔积

自然连接(内连接)

select name ,course_id 
from instructor ,teaches 
where instructor.ID = teaches.ID;

等于下面语句

select name ,course_id
form instructor natural join teaches; 

排列元组显示次序

select name ,salary
from instructor
where dept_name = 'Physics'
order by name desc, name asc ;

集合运算

image.png image.png image.png

空值

聚集函数

image.png

having子句

select dept_name , avg(salary) as avg_salary 
from instructor
group by dept_name
having avg(salary) > 42000 ;

外连接

DDL

USE recommendation;
ALTER TABLE experiment ADD category TINYINT(1) DEFAULT 0 COMMENT '实验类别:单业务线、跨业务线' 
USE recommendation;
ALTER TABLE experiment DROP COLUMN category  , name
上一篇下一篇

猜你喜欢

热点阅读