Mysql與Oracle追加(刪除)主外鍵、索引、check約束(實測有效)

Oracle和Mysql的這些區別真叫人頭大,你說啥時候這些區別才能消失呢?

唉,就簡單地整理了一部分,如有不對的地方,還請各位大佬批評指正!

修改列結構

ALTER TABLE 表名 MODIFY 列名 varchar(20);

Oracle:

alter table student modify sno varchar(20);

Mysql:

alter table student modify sno varchar(20);

追加、刪除主鍵

ALTER TABLE 表名 ADD CONSTRAINT 約束名 PRIMARY KEY;

Oracle可直接刪除約束,Mysql:drop primary key

Oracle:

alter table student add constraint stu_pk primary key (sno);
alter table student drop constraint stu_pk;

Mysql:

alter table student add constraint stu_pk primary key (sno);
alter table student drop primary key;

追加、刪除外鍵

外鍵不一定是另一個表的主鍵,但必須是唯一性索引。主鍵約束和唯一性約束都是唯一性索引

Oracle:

alter table SC add constraint sc_fk foreign key (cno) references Course (cno);
alter table SC drop constraint sc_fk ;

Mysql:

其實fk並非外鍵約束名,有時可能恰巧是。
SHOW CREATE TABLE sc;可以查看到外鍵約束名

ALTER TABLE sc ADD CONSTRAINT fk FOREIGN KEY (sno) REFERENCES student(sno);
SHOW CREATE TABLE sc;
alter table sc drop foreign key fk;

追加、刪除索引

CREATE UNIQUE INDEX  約束名 ON 表名(列名);

刪除時Mysql需要加on

Oracle:

CREATE UNIQUE INDEX  Stuname ON Student(Sname);
DROP INDEX Stuname;

Mysql:

CREATE UNIQUE INDEX  Stusno ON Student(Sno);
DROP INDEX Stusno on student;

建立、刪除CHECK約束

ALTER TABLE 表名 ADD CONSTRAINT 約束名 CHECK(具體約束);

刪除mysql得寫check

Oracle:

alter table student add constraint c1 check(Sage>=0 and Sage<=100);
alter table student drop constraint c1;

Mysql:

alter table student add constraint c1 check(Sage>=0 and Sage<=100);
alter table student drop check c1;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章