oracle外键怎么写? oracle 主键 外键

oracle 如何创建表外键

--使用表级约束

专注于为中小企业提供做网站、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业辽中免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

CREATE TABLE table_name

(column_1 datatype ,

column_2 datatype ,

...

CONSTRAINT fk_column

FOREIGN KEY (column_1, column_i, ... column_n)

REFERENCES parent_table (column_1, column_i, ... column_n)

);

--使用列级约束

CREATE TABLE table_name

(column_1 datatype ,

column_2 datatype CONSTRAINT fk_column REFERENCES parent_table (column_name),

...

);

在oracle中查询表之间外键的执行语句怎么写?

查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询): \x0d\x0a\x0d\x0aselect * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表 \x0d\x0a\x0d\x0a查询外键约束的列名: \x0d\x0a\x0d\x0aselect * from user_cons_columns cl where cl.constraint_name = 外键名称 \x0d\x0a\x0d\x0a查询引用表的键的列名: \x0d\x0a\x0d\x0aselect * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名 \x0d\x0a\x0d\x0a查询表的所有列及其属性 \x0d\x0a\x0d\x0aselect t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c.table_name and t.column_name = c.column_name and t.table_name = 要查询的表

Oracle中复合主键怎么写?有没有复合外键?

创建成绩表T_GRADE,并把学号S_ID+课程编号C_ID设为复合主键。

Create Table T_GRADE( --学生成绩表

S_ID Number(8), --学生编号

C_ID number(4), --课程编码

G_PS varchar2(6), --平时成绩

G_KS varchar2(6), --考试成绩

Constraint Grade_pk Primary Key (S_ID,C_ID)); --表级复合主键

可以设置复合外键。设置方法与复合主键一样,Primary Key(...) 换成

FOREIGN KEY(...) REFERENCES 主表(...)

或 ALTER TABLE 表 ADD CONSTRAINT 外键名

FOREIGN KEY(复合外键) REFERENCES 主表(主键|唯一键);

oracle中 怎么设主外键?

以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表的外键

);

oracle 创建外键

oracle创建外键约束有两种方法:

1、创建表时直接创建外键约束

create table books(

bookid number(10) not null primary key,

bookName varchar2(20) not null,

price number(10,2),

categoryId number(10) not null references Category(id)  --外键约束

);

2、先创建表,表创建成功后,单独添加外键约束

create table books(

bookid number(10) not null primary key,

bookName varchar2(20) not null,

price number(10,2),

categoryId number(10) not null

);

ALTER TABLE  books ADD CONSTRAINT FK_Book_categoryid FOREIGN KEY(categoryId ) REFERENCES Category(id);

oracle建立 主键 和 外键 的问题

3

stuid

number(10)

references

t_stu(stuid)

就是这个字段关联t_stu表的stuid字段;

4

couseid

number(10),

5

constraint

fk_couseid

foreign

key(couseid)

6

references

t_couse(couseid)

7

on

delete

cascade);

这个是外键关联,并做同步删除操作,就是如果t_couse表中的某个couseid数据被删除了,那么这张表相关的数据也会自动被删除;跟上面的区别就在于有没有做删除操作;

on

delete

cascade

就是同步删除的意思,比如t_couse表中有个couseid=5,t_score表中也有couseid=5的数据,当删除t_couse表中couseid=5的数据时,t_score表中所有couseid=5的数据也会自动删除;

可以写在同一行,但是要用逗号分格开;

望采纳,码字不容易


网站名称:oracle外键怎么写? oracle 主键 外键
本文地址:http://myzitong.com/article/hhhpgh.html