SQL语句面试题目:多表查询、SQL约束、DDL、DML
2017-09-23 18:49
696 查看
方式一(通用型):SELECT ... FROM ... WHERE
[sql]
view plain
copy
print?
SELECT e.last_name,e.department_id,d.department_name
FROM employees e,departments d
where e.department_id = d.department_id
![](http://static.blog.csdn.net/images/save_snippets.png)
方式二:SELECT ... FROM ...NATURAL JOIN ...
有局限性:会自动连接两个表中相同的列 ( 可能有多个 :department_id 和manager_id)
[sql]
view plain
copy
print?
SELECT last_name,department_id,department_name
FROM employees
NATURAL JOIN departments
![](http://static.blog.csdn.net/images/save_snippets.png)
方式三:SELECT ... JOIN ... USING...
有局限性:好于方式二,但若多表的连接列列名不同,此法不合适
[sql]
view plain
copy
print?
SELECT last_name,department_id,department_name
FROM employees
JOIN departments
USING(department_id)
![](http://static.blog.csdn.net/images/save_snippets.png)
方式四:SELECT ... FROM ... JOIN... ON ...
常用方式,较方式一,更易实现外联接(左、右、满)
[sql]
view plain
copy
print?
SELECT last_name,e.department_id,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
![](http://static.blog.csdn.net/images/save_snippets.png)
--内连接
1)
--等值连接
--不等值连接
2)
--非自连接
--自连接
--外连接
--左外连接、右外连接、满外连接
1.创建表
1)直接创建
[sql]
view plain
copy
print?
create table emp1(
name varchar2(20),
salary number(8,2)default 1000,
id number(4),
hire_date date
);
![](http://static.blog.csdn.net/images/save_snippets.png)
2)通过子查询的方式创建
[sql]
view plain
copy
print?
create table emp2
as
select last_name name,employee_id id,hire_date
from employees;
![](http://static.blog.csdn.net/images/save_snippets.png)
或者
[sql]
view plain
copy
print?
create table emp2
as
select last_name name,employee_id id,hire_date
from employees
where department_id = 80;/where 1=2;
![](http://static.blog.csdn.net/images/save_snippets.png)
2.修改表
1)增加新的列
[sql]
view plain
copy
print?
alter table emp1
add(birthday date)
![](http://static.blog.csdn.net/images/save_snippets.png)
2)修改现有的列
[sql]
view plain
copy
print?
alter table emp1
modify(name varchar2(25) default 'abc')
![](http://static.blog.csdn.net/images/save_snippets.png)
3)重命名现有的列
[sql]
view plain
copy
print?
alter table emp1
rename column salary to sal;
![](http://static.blog.csdn.net/images/save_snippets.png)
4)删除现有的列
[sql]
view plain
copy
print?
alter table emp1
drop column birthday;
![](http://static.blog.csdn.net/images/save_snippets.png)
3.清空表中的数据(与 delete from table_name区分开)
[sql]
view plain
copy
print?
truncate table emp2;
![](http://static.blog.csdn.net/images/save_snippets.png)
4.重命名表
[sql]
view plain
copy
print?
rename emp2 to emp3;
![](http://static.blog.csdn.net/images/save_snippets.png)
5.删除表
[sql]
view plain
copy
print?
drop table emp3;
![](http://static.blog.csdn.net/images/save_snippets.png)
1.1 增添一条记录
[sql]
view plain
copy
print?
insert into [表名](,,,,,)
values(,,,,,)
![](http://static.blog.csdn.net/images/save_snippets.png)
1.2 从其它表中拷贝数据
[sql]
view plain
copy
print?
insert into [表名]
select .... from [另一个表]
where ....
![](http://static.blog.csdn.net/images/save_snippets.png)
2)改
[sql]
view plain
copy
print?
update [表名]
set .....
where ....
![](http://static.blog.csdn.net/images/save_snippets.png)
3)删
[sql]
view plain
copy
print?
delete from [表名]
where ....
![](http://static.blog.csdn.net/images/save_snippets.png)
4)查(最常用的数据库操作)
select ....
from …
where ….
group by …
having …
order by ….
1.如何定义约束---在创建表的同时,添加对应属性的约束
1.1 表级约束 & 列级约束
[sql]
view plain
copy
print?
create table emp1(
employee_id number(8),
salary number(8),
hire_date date not null, --列级约束
dept_id number(8),
email varchar2(8) constraint emp1_email_uk unique,
name varchar2(8) constaint emp1_name_uu not null,
first_name varchar2(8),
--表级约束
constraint emp1_emp_id_pk primarykey(employee_id),
constraint emp1_fir_name_uk unique(first_name),
constraint emp1_dept_id_fk foreignkey(dept_id) references departments(department_id) ON DELETE CASCADE
)
![](http://static.blog.csdn.net/images/save_snippets.png)
1.2 只有 not null 只能使用列级约束。其他的约束两种方式皆可
2.添加和删除表的约束--在创建表以后,只能添加和删除,不能修改
2.1 添加
[sql]
view plain
copy
print?
alter table emp1
add constaint emp1_sal_ck check(salary > 0)
![](http://static.blog.csdn.net/images/save_snippets.png)
2.1.1 对于 not null 来讲,不用 add,需要使用modify:
[sql]
view plain
copy
print?
alter table emp1
modify (salary not null)
![](http://static.blog.csdn.net/images/save_snippets.png)
2.2 删除
[sql]
view plain
copy
print?
alter table emp1
drop constaint emp1_sal_ck
![](http://static.blog.csdn.net/images/save_snippets.png)
2.3 使某一个约束失效:此约束还存在于表中,只是不起作用
[sql]
view plain
copy
print?
alter table emp1
disable constraint emp1_email_uk;
![](http://static.blog.csdn.net/images/save_snippets_01.png)
2.4 使某一个约束激活:激活以后,此约束具有约束力
[sql]
view plain
copy
print?
alter table emp1
enable constraint emp1_email_uk;
1 SQL语句多表查询
例如:按照 department_id 查询 employees(员工表)和 departments(部门表)的信息。方式一(通用型):SELECT ... FROM ... WHERE
[sql]
view plain
copy
print?
SELECT e.last_name,e.department_id,d.department_name
FROM employees e,departments d
where e.department_id = d.department_id
![](http://static.blog.csdn.net/images/save_snippets.png)
SELECT e.last_name,e.department_id,d.department_name FROM employees e,departments d where e.department_id = d.department_id
方式二:SELECT ... FROM ...NATURAL JOIN ...
有局限性:会自动连接两个表中相同的列 ( 可能有多个 :department_id 和manager_id)
[sql]
view plain
copy
print?
SELECT last_name,department_id,department_name
FROM employees
NATURAL JOIN departments
![](http://static.blog.csdn.net/images/save_snippets.png)
SELECT last_name,department_id,department_name FROM employees NATURAL JOIN departments
方式三:SELECT ... JOIN ... USING...
有局限性:好于方式二,但若多表的连接列列名不同,此法不合适
[sql]
view plain
copy
print?
SELECT last_name,department_id,department_name
FROM employees
JOIN departments
USING(department_id)
![](http://static.blog.csdn.net/images/save_snippets.png)
SELECT last_name,department_id,department_n 4000 ame FROM employees JOIN departments USING(department_id)
方式四:SELECT ... FROM ... JOIN... ON ...
常用方式,较方式一,更易实现外联接(左、右、满)
[sql]
view plain
copy
print?
SELECT last_name,e.department_id,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
![](http://static.blog.csdn.net/images/save_snippets.png)
SELECT last_name,e.department_id,department_name FROM employees e JOIN departments d ON e.department_id = d.department_id
--内连接
1)
--等值连接
--不等值连接
2)
--非自连接
--自连接
--外连接
--左外连接、右外连接、满外连接
2 创建和管理表(DDL)
CRAETE TABLE/TRUNCATE TABLE /ALTER TABLE /REANME...TO/DROP TABLE ..操作完以后,自动 commit;所以,rollback 对其操作,没有效果1.创建表
1)直接创建
[sql]
view plain
copy
print?
create table emp1(
name varchar2(20),
salary number(8,2)default 1000,
id number(4),
hire_date date
);
![](http://static.blog.csdn.net/images/save_snippets.png)
create table emp1( name varchar2(20), salary number(8,2)default 1000, id number(4), hire_date date );
2)通过子查询的方式创建
[sql]
view plain
copy
print?
create table emp2
as
select last_name name,employee_id id,hire_date
from employees;
![](http://static.blog.csdn.net/images/save_snippets.png)
create table emp2 as select last_name name,employee_id id,hire_date from employees;
或者
[sql]
view plain
copy
print?
create table emp2
as
select last_name name,employee_id id,hire_date
from employees
where department_id = 80;/where 1=2;
![](http://static.blog.csdn.net/images/save_snippets.png)
create table emp2 as select last_name name,employee_id id,hire_date from employees where department_id = 80;/where 1=2;
2.修改表
1)增加新的列
[sql]
view plain
copy
print?
alter table emp1
add(birthday date)
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 add(birthday date)
2)修改现有的列
[sql]
view plain
copy
print?
alter table emp1
modify(name varchar2(25) default 'abc')
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 modify(name varchar2(25) default 'abc')
3)重命名现有的列
[sql]
view plain
copy
print?
alter table emp1
rename column salary to sal;
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 rename column salary to sal;
4)删除现有的列
[sql]
view plain
copy
print?
alter table emp1
drop column birthday;
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 drop column birthday;
3.清空表中的数据(与 delete from table_name区分开)
[sql]
view plain
copy
print?
truncate table emp2;
![](http://static.blog.csdn.net/images/save_snippets.png)
truncate table emp2;
4.重命名表
[sql]
view plain
copy
print?
rename emp2 to emp3;
![](http://static.blog.csdn.net/images/save_snippets.png)
rename emp2 to emp3;
5.删除表
[sql]
view plain
copy
print?
drop table emp3;
![](http://static.blog.csdn.net/images/save_snippets.png)
drop table emp3;
3 数据处理 DML
1)增1.1 增添一条记录
[sql]
view plain
copy
print?
insert into [表名](,,,,,)
values(,,,,,)
![](http://static.blog.csdn.net/images/save_snippets.png)
insert into [表名](,,,,,) values(,,,,,)
1.2 从其它表中拷贝数据
[sql]
view plain
copy
print?
insert into [表名]
select .... from [另一个表]
where ....
![](http://static.blog.csdn.net/images/save_snippets.png)
insert into [表名] select .... from [另一个表] where ....
2)改
[sql]
view plain
copy
print?
update [表名]
set .....
where ....
![](http://static.blog.csdn.net/images/save_snippets.png)
update [表名] set ..... where ....
3)删
[sql]
view plain
copy
print?
delete from [表名]
where ....
![](http://static.blog.csdn.net/images/save_snippets.png)
delete from [表名] where ....
4)查(最常用的数据库操作)
select ....
from …
where ….
group by …
having …
order by ….
4 约束
对创建的表的列属性、字段进行的限制。诸如 :not null/unique/primary key/foreign key/check1.如何定义约束---在创建表的同时,添加对应属性的约束
1.1 表级约束 & 列级约束
[sql]
view plain
copy
print?
create table emp1(
employee_id number(8),
salary number(8),
hire_date date not null, --列级约束
dept_id number(8),
email varchar2(8) constraint emp1_email_uk unique,
name varchar2(8) constaint emp1_name_uu not null,
first_name varchar2(8),
--表级约束
constraint emp1_emp_id_pk primarykey(employee_id),
constraint emp1_fir_name_uk unique(first_name),
constraint emp1_dept_id_fk foreignkey(dept_id) references departments(department_id) ON DELETE CASCADE
)
![](http://static.blog.csdn.net/images/save_snippets.png)
create table emp1( employee_id number(8), salary number(8), hire_date date not null, --列级约束 dept_id number(8), email varchar2(8) constraint emp1_email_uk unique, name varchar2(8) constaint emp1_name_uu not null, first_name varchar2(8), --表级约束 constraint emp1_emp_id_pk primarykey(employee_id), constraint emp1_fir_name_uk unique(first_name), constraint emp1_dept_id_fk foreignkey(dept_id) references departments(department_id) ON DELETE CASCADE )
1.2 只有 not null 只能使用列级约束。其他的约束两种方式皆可
2.添加和删除表的约束--在创建表以后,只能添加和删除,不能修改
2.1 添加
[sql]
view plain
copy
print?
alter table emp1
add constaint emp1_sal_ck check(salary > 0)
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 add constaint emp1_sal_ck check(salary > 0)
2.1.1 对于 not null 来讲,不用 add,需要使用modify:
[sql]
view plain
copy
print?
alter table emp1
modify (salary not null)
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 modify (salary not null)
2.2 删除
[sql]
view plain
copy
print?
alter table emp1
drop constaint emp1_sal_ck
![](http://static.blog.csdn.net/images/save_snippets.png)
alter table emp1 drop constaint emp1_sal_ck
2.3 使某一个约束失效:此约束还存在于表中,只是不起作用
[sql]
view plain
copy
print?
alter table emp1
disable constraint emp1_email_uk;
![](http://static.blog.csdn.net/images/save_snippets_01.png)
alter table emp1 disable constraint emp1_email_uk;
2.4 使某一个约束激活:激活以后,此约束具有约束力
[sql]
view plain
copy
print?
alter table emp1
enable constraint emp1_email_uk;
相关文章推荐
- SQL语句面试题目:多表查询、SQL约束、DDL、DML
- SQL语句面试题目:一般查询和高级子查询
- SQL语句面试题目:一般查询和高级子查询
- SQL语句面试题目:一般查询和高级子查询
- SQL语句面试题目:一般查询和高级子查询
- Oracle Class2. SQL查询和SQL函数(Oracle数据类型,ddl,dml,dcl,事务控制语言tcl,sql操作符,sql函数,select语句,运算符,分析函数,临时表)
- SQL语句面试题目:一般查询和高级子查询
- SQL语句面试题目:一般查询和高级子查询
- SQL语句面试题目:一般查询和高级子查询
- SQL语句面试题目
- SQL语句之DDL和DML
- sql重要的DML和DDL语句
- Sql语句中的DDL、DML、DCL的介绍
- SQL 语句 类型 DCL,DDL,DQL,DML
- MySql(4)------SQL基础之DML(Data Manipulation Language)下的查询(select)语句
- 7.PL_SQL――在PL_SQL程序中内嵌查询语句、DML语句、事物处理语句和游标属性
- Sql语句中两个比较迷糊的概念:“连接查询” 与 “外键约束”
- Sql语句中的DDL、DML、DCL的介绍
- 超有用的sql查询语句。项目、面试都有用
- SQL语句 DML,DDL,DCL