標籤:
增刪改查 交並補 內串連 外串連建立表:
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;
Oracle 表操作(三)