標籤:欄位 student pre art null xxxx const logs _id
學習來源: http://www.jb51.net/article/45539.htm
http://blog.csdn.net/j958703732/article/details/11066935
1. 建立資料表
文法:create table 表名 ( 欄位1 資料類型,欄位2 資料類型,...) tablespace 資料表空間
create table STUDENT( student_ID NUMBER not null, student_NAME VARCHAR2(20), student_AGE NUMBER, status VARCHAR2(2), version NUMBER default 0)tablespace users----tablespace 用來指定資料表空間
①查看資料表的資料表空間資訊
通過視圖 user_tables 可以獲得目前使用者所擁有的表資訊,利用如下SQL語句可以查看錶 student 的資料表空間資訊。
select table_name, tablespace_namefrom user_tableswhere upper(table_name) = ‘STUDENT‘;
②查看資料表的表結構
----使用 describe 來查看資料表的表結構desc student;
③表的重新命名
文法:alter table 原表名 rename to 新表名 ;
2. 添加新欄位
文法:alter table 表名 add (欄位名 欄位類型 [default value],.....);
3. 修改欄位
修改欄位類型:alter table 表名 modify (欄位名 欄位類型 [default value][null / not null],..... );
欄位重新命名:alter table 表名 rename column 原欄位名 to 新欄位名 ;
4. 刪除欄位
文法:alter table 表名 drop column 欄位名 ;
5. 主鍵
①主鍵可以在建立表的同時進行建立,主鍵可以有名字,也可以沒有名字
------1.建立有主鍵,但主鍵沒有名字的表格create table student(student_ID int primary key not null,student_NAME VARCHAR2(8),student_AGE NUMBER,);------刪除無名主鍵:沒有主鍵名,需要先擷取select *from user_cons_columns;------上述SQL可得到 student 表的主鍵名 為 XXXXX(由系統命名),再刪除alter table student drop constraint XXXXX;
-----2.建立有主鍵,且主鍵有名字的表格create table student(student_ID int,student_NAME VARCHAR2(8),student_AGE NUMBERconstraint keyName primary key(student_ID));-----2.刪除有名主鍵:無需尋找,直接刪除alter table student drop constrain keyName;
------3.向表中指定主鍵alter table student add constraint keyName primary key(student_ID);
6. 修改資料表的資料表空間資訊
修改意在將表移至其他資料表空間,以防最初建立時,資料表空間資訊錯誤
文法:drop table student move tablespace users;
7. 刪除資料表
------1.刪除無外鍵約束的資料表drop table student;------2.刪除有外鍵約束的資料表drop table student cascade constraints ;
.
Oracle的基本使用