数据库
首页 > 数据库> > SQL基本操作

SQL基本操作

作者:互联网

数据定义语句Data definition language (DDL)

数据类型

数据类型 描述
char(n) 字符/字符串,固定长度 n
varchar(n) 字符/字符串,可变长度,最大长度 n
int 整数值,精度为10
smallint 整数值,精度为5
bigint 整数值,精度为19
numeric(p,d) 精准数值表示,p:位数 d:小数点位数 p-d:整数位数
float 浮点数,尾数精度为16
float(n) 浮点数,尾数精度 p
date 存储年、月、日的值,例如‘2001-7-21’
time 存储小时、分、秒的值,例如’09:00:30’、’09:00:30.75’
timestamp 存储年、月、日、小时、分、秒的值,例如‘2001-7-27 09:00:30.75’

不同的数据库对数据类型定义提供不同的选择,具体可查看SQL 通用数据类型

模式定义

create table r (A1 D1, A2 D2, ..., An Dn,
                    (integrity_constraint 1),
                    ...,
                    (integrity_constraint k))

其中约束定义如下:

primary key (A1, ..., An ) # 主码
foreign key (Am, ..., An ) references r # 外码
check (P) # 满足谓词p的约束

例如:

create table instructor (s_id char(5),
                            s_name varchar(20) not null,
                            dept_name  varchar(20),
                            salary numeric(8,2),
                            primary key (s_id),
                            foreign key (dept_name) references department,
                            check(salary >0) );

模式删除

drop table r # 删除表,连通表结构一起删除
delete from r # 清空表,但是保留表结构,只剩空表

模式修改

# 添加域
alter table r add A D
# 例如
alter table instructor add age char(30)

# 删除域
alter table r drop A
# 例如
alter table instructor drop age 

数据操作语句Data-manipulation language (DML)

sql选择查询

select name		
from instructor

select distinct dept_name  # distinct去重
from instructor

select all dept_name # all不去重,默认不主动去重
from instructor

select *    # *表示全部
from instructor
 
select s_id, s_name, salary/12 as monthly_salary  # 计算rename
from instructor

利用where子句,实现条件筛选

select s_name
from instructo
where dept_name = 'Comp. Sci.'

利用from子句,选择模式来源

select  *
from instructor, teaches # 多表连接

select name, course_id
from instructor natural join teaches #自然连接
where instructor. dept_name = 'Art'

sql修改操作

删除操作delete

delete from instructor
where dept_name= ’Finance’

delete from instructor
where dept_name in (select dept_name
                    from department
                    where building = ’Watson’);

插入操作insert

insert into course
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);

insert into course (course_id, title, dept_name, credits) # 指明顺序
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);

insert into student
values ('3003', 'Green', 'Finance', null); # 插入空值

insert into tb
select ...

更新操作update

update instructor
set salary = salary * 1.03
where salary > 100000;

update instructor
set salary = case  # 利用case
            when salary <= 100000 then salary * 1.05
            when ... then ...
            when ... then ...
            else ...
            end

update student S
set tot_cred = (select sum(credits) #可以使用标量子查询
                from takes, course
                where takes.course_id = course.course_id and 
                S.ID= takes.ID.and
                takes.grade <> ’F’ and
                takes.grade is not null);

排序

select distinct s_name
from instructor
order by s_name (desc) # 升序,加desc降序,可以添加多个域,按照字典序进行排序

string操作

select s_name
from instructor
where s_name like '%dar%' # 匹配含有dar子串的字符串

select s_name
from instructor
where s_name like '___%' # 匹配至少含有三个字符的字符串

select s_name
from instructor
where s_name like '100\%' escape '\' # 匹配'100%'

聚集函数和group by

聚集函数包括:

  1. count()计数
  2. sum()求和
  3. avg()平均数
  4. max()最大值
  5. min()最小值
    可以结合group by分组来使用聚集函数
select {A1, A2, ., Ai},ag_fun(Ai+1),...,ag_fun(Ai+k) # select中元素要么是group by中的,要么是聚集函数
from r1,r2,...,rm
where   P1 
{ group by A1, A2, ., Ai 
{ having  P2 } } # having是对group by的对象进行条件筛选

# 例如
select dept_name, avg (salary)  #选出平均工资大于42000的部门
from instructor
group by dept_name  # 根据部门名进行分组
having avg (salary) > 42000;    # 该部门平均工资大于42000

集合操作

集合操作默认去重,加上all后不去重,集合操作作用于两个查询结果

也有其他的谓词,例如

select distinct course_id
from section
where semester = 'Fall' and s_year= 2009 and course_id in (select course_id
                                                        from section
                                                        where semester = 'Spring' and s_year= 2010);

也可以利用集合实现包含关系,若X属于Y ,则有 not exists(X except Y) 为真(不存在元素属于X-Y,则说明X属于Y)

# 例如找到所有选修了Biology部门开设的课的学生
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
                    from course
                    where dept_name = ’Biology’)
                    except
                    (select T.course_id
                    from takes as T
                    where S.ID = T.ID));

with语句

with max_budget(val) as              
(select max(budget)
from department)

select department.name
from department, max_budget
where department.budget = max_budget.val;

标签:salary,name,dept,SQL,基本操作,instructor,where,select
来源: https://www.cnblogs.com/chinono/p/14252320.html