oracle 建立表sql語句,主鍵,自增長

2021-07-03 14:36:01 字數 1580 閱讀 3080

在建立表之前請必須了解一下兩點

1,oracle 沒有identity,所以需要自己用squence 和 trigger 配合使用 實現 自增長。

2,oracle中如果使用關鍵字,需要用 雙引號引起了。所以下面例子中出現的雙引號可以理解成轉義的。

/    在執行多個sql是用 / 可以區分。

現在就直接來sql語句。

檢查是否存在該錶,並刪除

declare 

num number;

begin

select count(1) into num from all_tables where table_name = 'tableinfo' and owner='scott';

if num=1 then

execute immediate 'drop table tableinfo';

end if;

end;

/declare

num number;

begin

select count(1) into num from user_sequences where sequence_name='seq_tableinfoid';

if num =1 then

execute immediate 'drop sequence seq_tableinfoid';

end if;

end;

/declare

num number;

begin

select count(1) into num from user_triggers where trigger_name='tri_tableinfoid';

if num=1 then

execute immediate 'drop trigger tri_tableinfoid';

end if;

end;

/create table tableinfo

( "id" number(4) not null,

tablename varchar2(40) not null,

constraint pk_tableinfoid primary key ("id"));/

create sequence seq_tableinfoid

minvalue 1

nomaxvalue

start with 1

increment by 1

nocache;

/create trigger tri_tableinfoid

before insert on tableinfo

for each row

declare

begin

if inserting and :new."id" is null or :new."id" =0 then

:new."id" :=seq_tableinfoid.nextval;

end if;

end tri_tableinfoid;

oracle建立表空間的SQL語句

oracle建立表空間語句 create tablespace shopping 建立乙個叫shopping的表空間 datafile shopping.dbf 物理檔名 size 50m 大小 autoextend on 自動增長 next 50m maxsize 20480m 每次擴充套件50m...

sql語句建立表

create table search custom mall id int 11 not null primary keyauto increment,uid int 11 not null name varchar 150 not null search mall id int 11 not n...

Oracle中用sql語句建立和管理表

create table schema.tablename column datatype default expr constaint desc tablename轉殖整個表 create table emp as select from scott.emp 轉殖表結構 create table ...