1. 程式人生 > >Oracle和SQLServer的增刪改查和某些區別

Oracle和SQLServer的增刪改查和某些區別

一下午花在檢視約束名和刪除約束名,反反覆覆的執行,就為看看其中的區別,(借鑑CSDN使用者jkqiang885和a154832918,還借鑑了百度知道)

現在做個筆記,順便寫入建立約束,方便檢視!!!

create table class--下面要引用外來鍵聯絡classno來知道classname
(
       classno int primary key,
       classname varchar(50)
)

建立主鍵、外來鍵、唯一、檢查約束是分兩種(我執行過的預設約束只有欄位級別的執行成功):

    一是在建表時(欄位級別的) 二是在建表後(表級別的)

①:

Oracle:

create table student
(
       stuno number(8) not null constraint  pk_stuno primary key,--主鍵約束
       stuname varchar2(16) default 'Jack',--預設約束
       classno number(8) constraint fk_class_student references class(classno),--外來鍵約束
       stuid varchar2(18) constraint uq_stuid unique,--唯一約束
       stusex varchar2(4) constraint ck_stusex check(stusex in('帥哥','美女')),--檢查約束
       stuage number(3) constraint ck_stuage check(stuage<100 and stuage>10)--檢查約束
)

stusex欄位建立檢查約束也可以是:

stusex varchar2(4) constraint ck_stusex check(stusex='帥哥' or stusex='美女'),

(這個雖然可以,但可能不方便,因為當你不止'帥哥''美女'兩個範圍的詞時,用in是最好不過了,in後的括號裡的值用逗號分隔即可,對了,補充下:一個字佔兩個位元組或長度,字母、數字、符號都佔一個位元組或者長度,最基礎的啦)

其實我們一般這樣寫:

create table student
(
       stuno number(8) not null primary key,--主鍵約束
       stuname varchar2(16) default 'Jack',--預設約束
       classno number(8)  references class(classno),--外來鍵約束
       stuid varchar2(18) unique,--唯一約束
       stusex varchar2(4) check(stusex='帥哥' or stusex='美女'),--檢查約束
       stuage number(3) check(stuage<100 and stuage>10)--檢查約束
)

不過這樣的話,主鍵、外來鍵、唯一、檢查約束名就需要系統隨機分配名了,當你想刪除主鍵約束時,將會變得不方便,因為你不知道名字,若你要找約束名字(我會在文章最後部分(點選藍色字,會自動跳轉)說到“如何找約束名”),當找到名字了,名字將會很長,你還不如用第一種的,自己起個名字簡短並易認出(名字隨便取,自己認得出就行,最好別人也認得出啦),也就多加那幾個英文單詞而已啦!!!(不偷懶,就是方便自己。。。隨便啦)

建表時欄位裡不能這樣寫:

constraint  pk_stuno primary key(stuno),
constraint fk_class_student foreign key(classno) references  classQ2(classno)),
classno number(8) foreign key references classQ2(classno),

分析上面三句語句:第一句和第二句都沒有指定資料型別,而且欄位名要寫在前面,第三句,會報錯

對了,複習下:

表完整性:

1,實體完整性:主鍵約束,保證資料唯一性

2,域完整性:欄位規則,如性別必須是男或女,年齡在0-200

3,參照完整性:外來鍵約束,外來鍵對應的記錄必須存在

主鍵和唯一約束:記錄或資料都是唯一的,不重複的;但主鍵自動not null,而唯一可以null.

外來鍵約束:一般聯絡的是外表的主鍵欄位,欄位的資料型別必須與外來鍵對應欄位的資料型別一樣,長度可以不同.

SQLServer:(那三種預設約束,也是可以用在Oracle中)

create table student
(
       stuno int not null constraint  pk_stuno primary key,--主鍵約束
       stuname varchar(10) default 'Tony',--字串的預設約束
       schooltime date default '2018-9-8 17:00',--時間的預設約束
       classno int constraint fk_class_student references class(classno),--外來鍵約束
       email int default 9,--整型的預設約束
       stuid varchar(18) constraint uq_stuid unique,--唯一約束
       stusex varchar(4) constraint ck_stusex check(stusex in('帥哥','美女')),--檢查約束
       stuage int constraint ck_stuage check(stuage<100 and stuage>10)--檢查約束
)

接著,我們一般這樣寫

create table student
(
       stuno int primary key,--主鍵約束
       stuname varchar(10) default 'Tony',--字串的預設約束
       schooltime date default '2018-9-8 17:00',--時間的預設約束
       classno int foreign key references class(classno),--外來鍵約束
       email int DEFAULT 1001,--整型的預設約束
       stuid varchar(18) unique,--唯一約束
       stusex varchar(4) check(stusex in('帥哥','美女')),--檢查約束
       stuage int check(stuage<100 and stuage>10)--檢查約束
)

建立外來鍵約束 可以

classno int references class(classno),

正如上面所說的:為了刪除約束,第一種更好些

注:在Oracle裡可以用int和varchar,在SQLServer裡不可用number和varchar2

②:

Oracle:

alter table student add constraint pk_stunoQ primary key(stuno);--建主鍵約束
alter table student add constraint fk_classQ_studentQ foreign key(classno)references class(classno);--外來鍵約束
alter table student add constraint ck_stuageQ check(stuage<100 and stuage>10);--檢查約束

刪除約束:

在Oracle:

alter table student drop primary key;--刪除該表所有主鍵約束 (1)
alter table student drop constraint pk_stuno;--刪除該表指定欄位的主鍵約束  (2)
alter table student drop constraint  ck_stuage;--刪除檢查約束
alter table student drop constraint fk_classQ_studentQ;--刪除外來鍵約束

也可以:
 alter table 表名 drop constraint pk_stuno/約束名 cascade;

在SQLServer:

alter table student/表名 drop constraint pk_stuno/約束名;

⑴在mysql:

       --⒈刪除主鍵約束:

alter table 表名 drop primary key;
       --⒉刪除外來鍵約束:

alter table 表名 drop foreign key 外來鍵(區分大小寫)

⑵完整系列:

ALTER [ignore] TABLE tbl_name alter_specification [,alter_specification] ... alter_specification: table_option ...
/ADD [column] column_definition [first/after col_name ] --新增欄位
/ ADD [COLUMN](column_definition,...) --新增欄位
/ ADD {INDEX|KEY} [index_name] [index_type](index_col_name,...) --新增索引
/ ADD [constraint[symbol]] PRIMARY KEY [index_type] (index_col_name,...) --新增主鍵約束
/ ADD [CONSTRAINT[symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) --新增唯一約束
/ ADD [fulltext/spatial] [INDEX|KEY] [index_name] (index_col_name,...) --新增索引
/ ADD [CONSTRAINT[symbol]] FOREIGN KEY [index_name] (index_col_name,...) [reference_definition] --新增外來鍵約束
/ ALTER [COLUMN]col_name {SET DEFAULT literal | DROP DEFAULT} --建預設值
/change [COLUMN] old_col_name column_definition [FIRST|AFTER col_name] --改舊欄位名
/ modify [COLUMN]column_definition [FIRST | AFTER col_name] --修改欄位型別或長度
/ DROP [COLUMN] col_name   --刪欄位
/ DROP PRIMARY KEY --刪主鍵
/ DROP {INDEX|KEY} index_name --刪索引
/ DROP FOREIGN KEY fk_symbol --刪外來鍵
/ disable KEYS | enable KEYS | rename [TO] new_tbl_name --對鍵的禁用啟用重新命名
/ order BY col_name [, col_name] ...
/ convert TO characterset charset_name [COLLATE collation_name]
/ [DEFAULT] CHARACTERSET charset_name [COLLATE collation_name]
/ discard tablespace
/ import tablespace

⑶最後:

在SQLServer裡:   sp_helpconstraint STUDENT/表名; --找到資料表中的所有列的約束

在Oracle裡:

在Command Window命令窗口裡執行:     desc student;--查詢表的結構(檢視是否為空,資料型別和長度)

無須在命令視窗執行:(一個不帶條件,一個帶"表名為什麼"的條件)

select constraint_name,table_name,constraint_type from  user_constraints

select table_name,constraint_name,constraint_type from user_constraints where table_name='STUDENT/大寫的表名';

其實也可以直接   select * from user_constraints

(用user_constrains表來獲取當前使用者的約束,用all_constraints來獲取所以使用者的約束還包含了表的約束資訊)

還可以查詢索引表

select * from USER_INDEXES;

類似的,有序列表USER_SEQUENCES,觸發器表USER_TRIGGERS,儲存過程表USER_PROCEDURES,還有USER_TABLES,VIEW_TABLES 等。也就是將"USER_INDEXES"代替掉咯,要是執行select * from view_tables;時彈出"表或檢視不存在"提示,就是表示沒有view檢視的table