oracle表的操作SQL語句

2022-05-15 15:33:12 字數 2053 閱讀 3507

這篇文章的內容包括:表的増刪改查,欄位的増刪改查,主鍵、外來鍵、唯

一、非空、預設約束的増刪改

檢視自己使用者的所有表:

select * from user_tab_comments;

www.2cto.com  

建立表:

create table cqytest(

id number(1),

username varchar2(11),

password varchar2(11)--最後乙個字段後面必須沒有逗號

)tablespace cqyspace;

複製表:

create table test1 as select * from cqy.cqytest;  --複製cqy使用者的cqytest表

刪除表:

drop table test1 cascade constraints;

給表新增注釋:

comment on table cqytest is '我的測試表';

給字段新增注釋:

comment on column cqyt1.id is '主鍵';

修改欄位名:

alter table cqytest rename column id to userid;

新增字段:

alter table cqytest add email varchar2(11);

刪除字段:

alter table cqytest drop column email;

修改字段型別:

alter table cqytest modify email varchar2(20);

查詢所有約束:

select constraint_name from user_cons_columns;

建表時新增預設值、主鍵、外來鍵,非空約束

create table cqyt1(

id number(11) not null primary key,--非空,主鍵,唯一(unique),foreign key id references cqytable  www.2cto.com  

groupid number(11) check(groupid>22 and groupid<33),--條件約束

username varchar2(20),

password varchar2(20)

)tablespace cqyspace;

create table cqyt2(

id number(11),foreign key (id) references cqyt1(id),--建表時新增外來鍵

username varchar2(20),

password varchar2(20)

)tablespace cqyspace;

建表後新增主鍵

alter table cqyt2 modify (id primary key); --不帶約束名

alter table cqyt add constraint cqyt_pk_id primary key(id);--cqyt_pk為約束名

建表後新增外來鍵

alter table cqyt4 add constraint cqyt4_fk_cqyt1_id foreign key (id) references cqyt1(id);  www.2cto.com  

建表後新增非空約束

alter table cqyt2 modify (username not null);

建表後新增唯一性約束

alter table cqyt2 modify (username unique);

建表後新增預設值

alter table cqyt2 modify username default 'uu';

建表後新增條件約束

alter table cqyt2 add constraints cqyt2_ck_id check (id>11 and id<20);

刪除約束

alter table cqyt4 drop constraint cqyt4_pk;

oracle表的操作sql語句

一 非空 預設約束的増刪改 檢視自己使用者的所有表 select from user tab comments www.2cto.com 建立表 create table cqytest id number 1 username varchar2 11 password varchar2 11 最後...

oracle 操作表的sql

新建表 create table table1 id varchar 300 primary key,name varchar 200 not null 插入資料 insert into table1 id,name values aa bb 更新資料 update table1 set id bb...

oracle相關操作SQL語句

一 選擇出oracle中的所有使用者 select from dba users 此語句可以查詢出,所有使用者的詳細資訊,包括使用者名稱 登陸密碼 預設表空間 預設臨時表空間 使用者建立的日期 使用者的id等等資訊 select from all users 此語句可以查詢出,所有使用者的使用者名稱...