當前位置:首頁 » 編程語言 » oracle建表sql默認值
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

oracle建表sql默認值

發布時間: 2022-12-10 13:16:57

sql語句 給表增加一列並設置默認值

你好!
alter
table
t1
add
user_id
varchar(10)
default
'000001'
向表T1添加欄位user_id,默認值000001
希望對你有所幫助,望採納。

⑵ Oracle sql語法

參考網上資料,整理Oracle sql語法:

DDL:

1、創建表

     create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

     根據已有的表創建新表:

 create table tab_new as select col1,col2… from tab_old

2、刪除表

     drop table tabname

3、重命名表

     說明:alter table 表名 rename to 新表名

        eg:alter table tablename rename to newtablename

4、增加欄位

     說明:alter table 表名 add (欄位名 欄位類型 默認值 是否為空);

        例:alter table tablename add (ID int);

       eg:alter table tablename add (ID varchar2(30) default '空' not null);

5、修改欄位

     說明:alter table 表名 modify (欄位名 欄位類型 默認值 是否為空);

        eg:alter table tablename modify (ID number(4));

6、重名欄位

     說明:alter table 表名 rename column 列名 to 新列名 (其中:column是關鍵字)

        eg:alter table tablename rename column ID to newID;

7、刪除欄位

     說明:alter table 表名 drop column 欄位名;

        eg:alter table tablename drop column ID;

8、添加主鍵

     alter table tabname add primary key(col)

9、刪除主鍵

     alter table tabname drop primary key(col)

10、創建索引

     create [unique] index idxname on tabname(col….)

11、刪除索引

     drop index idxname

     註:索引是不可更改的,想更改必須刪除重新建。

12、創建視圖

     create view viewname as select 語句

13、刪除視圖

     drop view viewname

14.    創建表空間

create tablespace schooltbs datafile 『D:\oracle\datasource\schooltbs.dbf』 size 1000M autoextend on;

15.    刪除表空間

drop tablespace schooltbs[including contents and datafiles];

註:查詢表空間和表空間存儲路徑

SELECT * FROM dba_data_files WHERE tablespace_name = 表空間名;

DML:

1、數據查詢

     select <列名> from <表名> [where <查詢條件表達試>] [order by <排序的列名>[asc或desc]]

2、插入數據

     insert into 表名 values(所有列的值);

     insert into test values(1,'zhangsan',20);

     insert into 表名(列) values(對應的值);

     insert into test(id,name) values(2,'lisi');

3、更新數據

     update 表 set 列=新的值 [where 條件] -->更新滿足條件的記錄

     update test set name='zhangsan2' where name='zhangsan'

     update 表 set 列=新的值 -->更新所有的數據

     update test set age =20;

4、刪除數據

delete from 表名 where 條件 -->刪除滿足條件的記錄

     delete from test where id = 1;

     delete from test -->刪除所有

     commit; -->提交數據

     rollback; -->回滾數據

     delete方式可以恢復刪除的數據,但是提交了,就沒辦法了 delete刪除的時候,會記錄日誌 -->刪除會很慢很慢

truncate table 表名

     刪除所有數據,不會影響表結構,不會記錄日誌,數據不能恢復 -->刪除很快

drop table 表名

     刪除所有數據,包括表結構一並刪除,不會記錄日誌,數據不能恢復-->刪除很快

5、數據復制

表數據復制

     insert into table1 (select * from table2);

復製表結構

     create table table1 select * from table2 where 1>1;

復製表結構和數據

     create table table1 select * from table2;

復制指定欄位

     create table table1 as select id, name from table2 where 1>1;

⑶ ORACLE建表SQL

每個欄位後面必須得跟欄位的大小。
例如:create table student( name varchar2(20), age number(4), sal number(4), comm number(4), job varchar(4));
不然的話提示錯誤為:缺失左括弧。
希望能夠幫你。

⑷ oracle資料庫的一個表中,怎麼設置欄位的默認值

如果表已經存在,用如下方法設置默認值:

altertable表名modify欄位名default默認值;

如test表中設置age欄位為30,可用如下語句:

;

(4)oracle建表sql默認值擴展閱讀:

Oracle關於默認值的其他用法

添加、修改默認值:alter table table_namemodifycolumn_namedefault具體內容;

刪除默認值:alter table table_namemodifycolumn_namedefaultnull;

增加列(約束、默認值)

語法:alter table tb_name add column datatype [default val] constraint .....

如果添加not null(primary key約束要求值也不能為null)約束,需要保證當前表中沒有數據存在。

新添加的列,相當於表定義中最後一個定義的列。