標籤:unique oracl taf 直接 sel nbsp ons from 刪除
1.建立table
create table test01( id int not null primary key, name varchar(8) not null, gender varchar2(2) not null, age int not null, address varchar2(20) default ‘地址不詳’ not null, regdata date);
約束
非空約束 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 名字 foregincreate 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 約束名字;唯一約束 與主鍵區別 唯一約束可以有多個,只能有一個nullcreate 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’/*
2.修改表
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 表名 //修改表名
3.刪除表格
truncate table 表名 //刪除表中的所有資料,速度比delete快很多,截斷表delete from table 條件//drop table 表名 //刪除表
4.插入語句
insert into 表名(值1,值2) values(值1,值2);
5.修改語句
update 表名 set 欄位=值 [修改條件]update t_scrm_db_app_user set password = :pwd where login_name = :user
6.查詢語句
帶條件的查詢 where模糊查詢 like % _範圍查詢 in對查詢結果進行排序 order by desc||asc
7.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;
8.運算子和運算式
算數運算子和比較子
distinct 去除多餘的行
column 可以為欄位設定別名 比如 column column_name heading new_name
decode 函數的使用 類似於case…when
select username,decode(username,’aaa’,’電腦部門’,’bbb’,’市場部門’,’其他’) as 部門 from users;
9.複製表
create table 表名 as 一個查詢結果 //複製查詢結果
insert into 表名 值 一個查詢結果 //添加時查詢
10.查看錶空間
desc test01;
11.建立資料表空間
永久資料表空間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’;
ORACLE基本操作