Oracle 建立、修改、刪除表等常規操作
建立表
create table test01(
id int not null,
name varchar(8) not null,
gender varchar2(2) not null,
age int not null,
address varchar2(20) default ‘地址不詳’ not null,
regdata date
);
修改表
alter table test01 add constraint s_id primary key;
alter table test01 add constraint CK_INFOS_GENDER check(gender=’男’ or gender=’女’)
alter table test01 add constraint CK_INFOS_AGE(age>=0 and age<=50)
alter table 表名 modify 欄位名 default 預設值; //更改欄位型別
alter table 表名 add 列名 欄位型別; //增加欄位型別
alter table 表名 drop column 欄位名; //刪除欄位名
alter table 表名 rename column 列名 to 列名 //修改欄位名
rename 表名 to 表名 //修改表名
刪除表
truncate table 表名 //刪除表中的所有資料,速度比delete快很多,截斷表
delete from table 條件//
drop table 表名 //刪除表
插入語句
insert into 表名(值1,值2) values(值1,值2);
修改語句
update 表名 set 欄位=值 [修改條件]
查詢語句
帶條件的查詢
where
模糊查詢
like % _
範圍查詢
in
對查詢結果進行排序
oeder by desc||asc
case…when
select username,case username when ‘aaa’ then ‘計算機部門’ when ‘bbb’ then ‘市場部門’ else ‘其他部門’ end as 部門 from users;
select username,case username=’aaa’ then ‘計算機部門’ when username=’bbb’ then ‘市場部門’ else ‘其他部門’ as 部門 from users;
運算子和表示式
算數運算子和比較運算子
distinct 去除多餘的行
column 可以為欄位設定別名 比如 column column_name heading new_name
decode 函式的使用 類似於case…when
select username,decode(username,’aaa’,’計算機部門’,’bbb’,’市場部門’,’其他’) as 部門 from users;
複製表
create table 表名 as 一個查詢結果 //複製查詢結果
insert into 表名 值 一個查詢結果 //新增時查詢
查看錶資訊
desc test01;
建立表空間
永久表空間
create tablespace test1_tablespace datafile ‘testfile.dbf’ size 10m;
臨時表空間
create temporary tablespace temptest1_tablespace tempfile ‘tempfile.dbf’ size 10m;
desc dba_data_files;
select file_name from dba_data_files where tablespace_name=’TEST1_TABLESPACE’;
約束
非空約束 |主鍵約束|外來鍵約束|唯一約束|檢查約束
not null |primary key|unique|check|
聯合主鍵 constraint pk_id_username primary key(id,username);
檢視資料字典 desc user_constraint
修改表時重新命名 rename constraint a to b;
修改表刪除約束
禁用約束 disable constraint 約束名字;
刪除約束 drop constraint 約束名字; drop primary key;直接刪除主鍵
外來鍵約束
create table typeinfo(typeid varchar2(20) primary key, typename varchar2(20));
create table userinfo_f( id varchar2(10) primary key,username varchar2(20),typeid_new varchar2(10) references typeinfo(typeid));
insert into typeinfo values(1,1);
建立表時設定外來鍵約束
constraint 名字 foregin
create table userinfo_f2 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new foreign key(typeid_new) references typeinfo(typeid));
create table userinfo_f3 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade);
外來鍵約束包含
刪除外來鍵約束
禁用約束 disable constraint 約束名字;
刪除約束 drop constraint 約束名字;
唯一約束 與主鍵區別 唯一約束可以有多個,只能有一個null
create table userinfo_u( id varchar2(20) primary key,username varchar2(20) unique,userpwd varchar2(20));
建立表時新增約束
constraint 約束名字 unique(列名);
修改表時新增唯一約束 add constraint 約束名字 unique(列名);
檢查約束
create table userinfo_c( id varchar2(20) primary key,username varchar2(20), salary number(5,0) check(salary>50));
constraint ck_salary check(salary>50);
/* 獲取表:*/
select table_name from user_tables; //當前使用者的表
select table_name from all_tables; //所有使用者的表
select table_name from dba_tables; //包括系統表
select table_name from dba_tables where owner=’zfxfzb’
/*