Oracle 表操作(三)
增删改查 交并补 内连接 外连接
创建表:
create table table_name(
num int IDENTIFIED(1,1) not null, --identified自增量
name VARCHAR2(20) not null, --not null不为空
age VARCHAR2(20),
sex VARCHAR2(2)
)
新增列:
alter table table_name
add col_name data_type
删除列:
alter tabel table_name
drop column col_name
修改列:
alter table table_name
alter column col_name data_type
插入 insert 语句
给指定列插入数据:
insert into table_name(deptno,dname) values(50,'xx');
插入全部列数据:
insert into dept(deptno,dname,loc) values(60,'xx','lll');
insert into dept values(70,'xxx','llll'); 插入值包含所有字段是可省略字段名
批量插入:(新表需要预先创建好)
insert into table_name1 (col_name1,col_name2) --表1列名
select column1,column2 --表2列名
from table_name2 where search_condition --约束条件
使用insert······into批量插入:(新表不需要预先创建好)
select col_name1,col_name2
into new_table
from table_name
where search_condition
更新 update 语句
更新指定数据:update table_name set col_name='new_data' where deptno=50;
删除 delete 语句
删除指定数据:delete from table_name where condition=70;
delete 删除数据库中的数据,drop删除表结构
delete删除行数据,drop删除列
查询 select 语句
查询所有:select * from emp;
指定字段查询:select col_name1,col_name2 from table_name;
加 where 条件:select * from emp where sal>=800; select * from table_name where sal>=1500 and job='SALESMAN';
Distinct 去重复记录: select distinct col_name from table_name;
Group by 分组查询:select job,count(ename) as num from EMP t group by job;
Having 过滤分组:select job,count(ename) as num from EMP t group by job having count(ename)>=2;
Order by 排序:select * from emp order by sal desc;
Order by子句一定放在所有句子的最后(不论包含多少子句)
子查询:查询出基本工资大于平均工资的员工:select * from emp where sal>(select avg(sal) from emp)
asc顺序查询 desc 逆序查询,只作用最近的列,多列逆序需要多个desc
联合查询:
并集(去重复):
select * from t_user1
union
select * from t_user2;
并集:
select * from t_user1
union all
select * from t_user2;
交集:
select * from t_user1
intersect
select * from t_user2;
差集:
select * from t_user1
minus
select * from t_user2;
内连接:
select * from emp t,dept d where t.deptno=d.deptno;
类似:select * from emp e inner join dept d on e.deptno=d.deptno; inner 可以省略;
外连接:
左外连接:select * from emp e left join dept d on e.deptno=d.deptno;
右外连接:select * from emp e right join dept d on e.deptno=d.deptno;