create TABLE zhao(\x0d\x0a id number primary key,\x0d\x0a mingcheng nvarchar2(50),\x0d\x0a neirong nvarchar2(50),\x0d\x0a jiezhiriqi date,\x0d\x0a zhuangtai nvarchar2(50)\x0d\x0a);\x0d\x0acreate TABLE tou(\x0d\x0a id number primary key,\x0d\x0a zhao_id number,\x0d\x0a toubiaoqiye nvarchar2(50),\x0d\x0a biaoshuneirong nvarchar2(50),\x0d\x0a toubiaoriqi date,\x0d\x0a baojia number,\x0d\x0a zhuangtai nvarchar2(50),\x0d\x0a foreign KEY(zhao_id) REFERENCES zhao(id)\x0d\x0a);\x0d\x0aforeign key (zhao_id) references to zhao(id)\x0d\x0a多了個to
創(chuàng)新互聯(lián)建站是一家專業(yè)提供海城企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、H5場景定制、小程序制作等業(yè)務(wù)。10年已為海城眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
SQL?drop?table?Stu;
drop?table?Stu
*
ERROR?位于第?1?行:
ORA-00942:?表或視圖不存在
SQL?create?table?Stu(
2??sno?varchar2(10)?primary?key,
3??sname?varchar2(10)?not?null,
4??sex?varchar2(5)?check(sex='男'or?sex='女'),
5??class?varchar2(10),
6??spasswd?varchar2(10)
7??);
表已創(chuàng)建。
SQL?drop?table?Cou;
drop?table?Cou
*
ERROR?位于第?1?行:
ORA-00942:?表或視圖不存在
SQL?create?table?Cou(
2??cno?varchar2(10)?primary?key,
3??cname?varchar2(30)?not?null,
4??kcxz?varchar2(20),
5??lesson_hours?number(4),
6??test_hours?number(4),
7??credit?number(3)
8??);
表已創(chuàng)建。
SQL?drop?table?SC;
drop?table?SC
*
ERROR?位于第?1?行:
ORA-00942:?表或視圖不存在
SQL?create?table?SC(
2??sno?varchar2(10),
3??cno?varchar2(10),
4??grade?number(3),
5??primary?key(sno,cno)
6??);
表已創(chuàng)建。
SQL?
SQL?
SQL?ALTER?TABLE?SC?ADD?CONSTRAINT?fk_SC_Stu??FOREIGN?KEY?(sno)??REFERENCES??Stu;
表已更改。
SQL?
SQL?ALTER?TABLE?SC?ADD?CONSTRAINT?fk_SC_Cou??FOREIGN?KEY?(cno)??REFERENCES??Cou;
表已更改。
SQL
create TABLE zhao(
id number primary key,
mingcheng nvarchar2(50),
neirong nvarchar2(50),
jiezhiriqi date,
zhuangtai nvarchar2(50)
);
create TABLE tou(
id number primary key,
zhao_id number,
toubiaoqiye nvarchar2(50),
biaoshuneirong nvarchar2(50),
toubiaoriqi date,
baojia number,
zhuangtai nvarchar2(50),
foreign KEY(zhao_id) REFERENCES zhao(id)
);
foreign key (zhao_id) references to zhao(id)
多了個to
以oracle自帶的用戶scott為例。
create?table?dept(
deptno?number(2)?primary?key,?--deptno?為?dept表的主鍵
dname?varchar2(10),
loc?varchar2(9)
);
create?table?emp(
empno?number(4)?primary?key,?--empno?為?emp表的主鍵
ename?varchar2(10),
job?varchar2(9),
mgr?number(4),
hiredate?date,
sal?number(7,2),
comm?number(7,2),
deptno?number(2)?references?dept(deptno)?--dept表中deptno字段?為?emp表的外鍵
);
主鍵:ALTER TABLE A ADD CONSTRAINT PK_XXX PRIMARY KEY (XXX)
外鍵;ALTER TABLE B ADD CONSTRAINT FK_YYY FOREIGN KEY(YYY) REFERENCES A(XXX)
B表的YYY字段 引用A表的XXX字段值域為自身值域
主鍵約束,外鍵約束就是這樣建立的
刪除約束: ALTER TABLE A DROP CONSTRAINT 約束名