oracle建立表和約束的SQL語句

2021-10-19 13:57:11 字數 1910 閱讀 5759

--

-1、建立模擬的資料表 -----

1.1.建立學生表student

create table student

( stuid number not null,

--學生id

stuname varchar2(10

) not null,

--名稱

gender varchar2(10

)not null,

-- 性別

age number(2

) not null,

-- 年齡

joindate date null,

--入學時間

classid number not null,

--班級id

address varchar2(50

) null --家庭住址

);--1.2、建立班級表stuclass

create table stuclass

( classid number not null,

-- 班級id

classname varchar2(20

) not null,

--班級名稱

notes varchar2(50

) null default

'班級資訊'

,--備註,預設班級資訊

);----

2、建立資料表的約束-----

2.1)建立主鍵約束--

alter table student add constraint pk_student_stuid primary key

(stuid)

; alter table stuclass add constraint pk_stuclass_classid primary key

(classid);--

2.2) 建立檢查約束--

alter table student add constraint ck_student_gender check

(gender=

'男' or gender=

'女')

; alter table student add constraint ck_student_age check

(age>=

0 and age<=

100);--

2.3)建立唯一約束--

alter table student add constraint uq_student_stuname unique

(stuname);--

2.4)建立預設約束--

--alter table student add constraint df_student_address default

('位址不詳');

alter table student modify address varchar(50

)default

'位址不詳'

; alter table student modify joindate date default sysdate;

--2.5

)建立外來鍵約束--

alter table student add constraint fk_student_stuclass_classid

foreign key

(classid) references stuclass

(classid);

注意:建立表還是約束,與sql server基本相同,注意:在oracle中default是乙個值,而sql server中default是乙個約束,

因此oracle的default設定可以在建表的時候建立或者通過modify函式建立

Oracle的表建立和約束

oracle建立表同sql server一樣,使用create table命令來完成。建立約束則使用如下命令 語法格式 altertable命令 alter table 表名add constraint d4 約束名約束內容。不論建立表還是約束,與sql server基本相同,注意 在oracle中...

建立表和約束條件

oracle中建立表主要關注表字段的型別和對應的約束條件。1 建立表語句 create table tname fied1 型別1,fied1 型別2,create table tname as 子查詢 複製現成表 2 字段型別 varchar n 建立可以存放n個字元的字段,資料長度可以自動變長,...

SQL初級 建立表和約束

在自己建立的資料庫中建立表 use sb 使用某個資料庫,格式 ues 資料庫名 create table 123 格式 create table 自定義的表名 欄位名一般為有一定意義的英文 names char 15 格式 欄位名 型別 括號裡面的是允許輸入的長度 age int,int型的後面不...