oracle 创建表sql语句,主键,自增长
在创建表之前请必须了解一下两点
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;