day1
1.创建数据库 Create database 数据库名(参数)
2.创建数据表 Create table 表名(参数 属性1 数据类型, 属性2 数据类型, ID int, Name varchar(10), Sex nchar(1) )
3.删除表 drop table 表名
4.修改表 Alter table 表名 –增加属性 add 属性名 属性值 –删除属性 drop column 属性名 –修改属性 alter column 属性名 属性类型 是否允许为空 (修改属性类型一般情况只允许向范围大的类型修改)
5.数据的添加
数据的添加方法1 insert[into] 表名 values(属性值列表) (数据的添加是以元组为单位添加的,一加一行) 将属性值存入表中对位的属性中。
数据的添加方法2 insert[into] 表名(属性列表) values(属性值列表)
属性列表表示要进行赋值的属性,属性值列表中的属性值个数、顺序、类型要与属性类表中的属性名相同。在添加时没有出现在属性列表中的属性去取null。不允许为空的属性必须出现在属性名列表中。
create table student(
sno nchar(10) not null,
sname nvarchar(8),
sex nchar(1),
birthday datetime,
clno nchar(6),
point smallint,
phone nchar(11),
email nvarchar(30)
);
create table course(
cno nchar(6) not null,
cname nvarchar(20),
type nvarchar(8),
period tinyint,
credit numeric(4,1)
);
create table score(
sno nchar(10) not null,
cno nchar(6) not null,
usually numeric(6,2),
final numeric(6,2)
);
create table teacher(
tno nchar(6) not null,
tname nvarchar(8),
major nvarchar(10),
prof nvarchar(10),
department nvarchar(12)
);
create table class(
clno nchar(6) not null,
classname nvarchar(12),
department nvarchar(12),
monitor nvarchar(8)
);
create table teach_class(
tno nchar(6) not null,
clno nchar(6) not null,
cno nchar(6) not null
);
alter table student
add nation nchar(10);
alter table student
alter column nation varchar(10);
alter table student
drop column nation;
insert into student
values('0922221326','李杰','男','1988-2-3','090501','754','13456734522','Lijie@sina.com');
day2
查询:select Select子句用于实现投影操作
查询表中所有行,所有列 Select * From 表名
查询指定的列,结果中显示顺序与select子句中的属性名列表相同 distinct Select 属性名1,属性二,······ From 表名
distinct:用于消除查询结果中的重复元组,卸载select关键词后,属性名列表之前 Select + distinct = 投影 –员工的编号、工资、年工资 Select id,salary,salary*12 From emp (已知月工资,导出年工资)
属性的别名:在查询结果中可以通过属性别名更改结果中列的名称,定义别名只需在属性名之后空格隔开,直接定义属性别名。定义别名只需在属性名之后空格隔开,直接定义属性别名。 Select 属性名 属性别名 例如: Select id salary,salary*12 year_salary From emp
结果排序 Order by 属性名 :将属性名结果按照指定的属性进行排序 Order by 后可以加多个属性,按照属性从左到右的顺序进行排序,当前一个属性相同时,按照后一个属性进行排序。 例子:查询员工的姓名,职位,工资,并将结果按照工资降序排列。 Select id,name,title,salary From emp Order by salary desc 语法:order by 关键字 desc(降序)/asc(升序) 默认为升序。
代码:
SELECT DISTINCT sno,sname,clno,point,isnull('无',email)
FROM student
SELECT DISTINCT cno,sno,usually*0.3+final*0.7 finals
FROM score
ORDER BY cno ASC,finals DESC
day3
Select e.name,l.name
From emp e,emp l
Where e.manager_id = l.id
Select e.name,l.name
From emp e,emp l
Where e.manager_id = l.id and
e.name= 'Mark'
Select e.name,l.name
From emp e,emp l
Where e.manager_id = l.id
and l.name= 'Carmen'
Select distinct l.name
From emp e,emp l
Where e.manager_id = l.id
Select e.id,e.name,l.name
From emp e,emp l
Where e.manager_id = l.id
Select e.id,e.name,l.name
From emp e left outer join emp l on e.manager_id = l.id
Select e.id,e.name,l.*
From emp e left outer join emp l on e.manager_id = l.id
Select e.id,e.name,l.*
From emp l right outer join emp e on e.manager_id = l.id
Select e.id,e.name,l.*
From emp l full outer join emp e on e.manager_id = l.id
Select e.id,e.name, l.name
From emp e right outer join emp l on e.manager_id = l.id
Select e.id,e.name, l.name
From emp e right outer join emp l on e.manager_id = l.id
Where e.id is null
Select d.name
From dept d left join emp e
on e.dept_id = d.id
Where e.id is null
Select e.name,l.name
From emp e inner join emp l
on e.manager_id = l.id inner join dept d on e.dept_Id = d.id
inner join region r on d.region_id = r.id
select salary
from emp
Select avg(salary)
From emp
Select max(comm_pct)
From emp
Select avg(comm_pct)
From emp
Select count(comm_pct)
From emp
where comm_pct is not null
Select count(distinct comm_pct)
From emp
select count(*)
from emp
select dept_id,avg(salary)
From emp
group by dept_id
select dept_id,title,avg(salary)
From emp
group by dept_id, title
select avg(salary)
From emp
Where dept_id = 2
select dept_Id,avg(salary)
From emp
Where dept_id = 2
select dept_Id,avg(salary)
From emp
Where dept_id = 2
group by dept_id
select dept_Id,avg(salary)
From emp
Where dept_id = 2
select avg(dept_Id),avg(salary)
From emp
Where dept_id = 2
select max(dept_Id),avg(salary)
From emp
Where dept_id = 2
select dept_id,avg(salary)
From emp
Where avg(salary)> 1500
group by dept_id
select dept_id,avg(salary)
From emp
group by dept_id
having avg(salary)> 1500
day4
update emp
set salary = salary + 500
where id in(
select e.id
From emp e,region r,dept d
Where e.dept_id= d.id and d.region_id = r.id and
city = 'beijing')
delete
from emp
where id in(
select e.id
From emp e,region r,dept d
Where e.dept_id= d.id and d.region_id = r.id and
city = 'beijing')
嵌套查询
select *
from emp
where salary = (select min(salary) from emp)
select *
from emp
where title = (select title from emp where name = 'molly')
and name <> 'molly'
insert into emp(id,name,manager_id,dept_id,title,salary)
values(25,'molly',1,2,'Stock clerk',1300)
select *
from emp
where title = in (select title from emp where name = 'molly')
and name <> 'molly'
select *
from emp
where salary > all
(
select salary
from emp
where name = 'molly'
)
select *
from emp >
(
select max(salary)
from emp
where name = 'molly'
)
select distinct l.name
from emp e,emp l
where e.manager_id = l.id
select *
from emp
where id in
(
select manager_id
from emp
)
select distinct l.name
from emp e right join emp l on e.manager_id = l.id
where e.id is null
select *
from emp
where id not in
(
select manager_id
from emp
where manager_id is not null
)
select dept_id
from emp
Group by dept_id
having avg(salary)>
(
select avg(salary)
from emp
where dept_id = 2
)
select *
from emp
where salary>
(
select avg(salary)
from emp
)
select *
from emp me
where salary>
(
select avg(salary)
from emp
where dept_id = me.dept_id
)
select *
from dept d
where exists
(
select *
from emp e
where e.dept_id = d.id
)
select *
from dept d
where not exists
(
select *
from emp e
where e.dept_id = d.id
)
select *
from emp l
where exists
(
select *
from emp e
where e.manager_id = l.id
)
select sname
from student my
where not exists
(
select *
from course c
where not exists
(
select *
from score
where score.cno = c.cno
and score.sno = my.sno
)
)
修改/删除+子查询
update emp
set salary = salary + 500
where id in(
select e.id
From emp e,region r,dept d
Where e.dept_id= d.id and d.region_id = r.id and
city = 'beijing')
delete
from emp
where id in(
select e.id
From emp e,region r,dept d
Where e.dept_id= d.id and d.region_id = r.id and
city = 'beijing')
|