数据库添加外键
A. 数据库外键怎么设置
sql 数据库建表时怎么设置外键,
1> -- 创建测试主表. ID 是主键.
2> CREATE TABLE test_main (
3> id INT,
4> value VARCHAR(10),
5> PRIMARY KEY(id)
6> );
7> go
-- 建表时设置外键
1> CREATE TABLE test_sub (
2> id INT,
3> main_id INT,
4> value VARCHAR(10),
5> PRIMARY KEY(id),
6> FOREIGN KEY (main_id) REFERENCES test_main
7> );
8> go
sql怎么设置外键
可以在创建表的时候创建,也可以在创建表之后创建。
创建表时创建:
create table student
(id int primary key,
name char(4),
dept char(9)
sex char(4))
create table grade
(id int ,
grade int
constraint id_fk foreign key (id) references student (id)
)
或创建了两表之后再建
alter table grade
add constraint id_fk foreign key (id) references student (id)
呵呵,希望能帮助你。
sql server中图形界面如何设置外键
在那个属性上右键 有约束 自己添加就OK了
mysql怎么设置外键?
ALTER TABLE b ADD CONSTRAINT c FOREIGN KEY(c) REFERENCES a(c) ON DELETE CASCADE ON UPDATE CASCADE; 哎呀。。好像写反了。我写的是把表B的c设置为外键了。。你改一下吧。
如何在数据库的建立表的时候设置表的外键
1> -- 创建测试主表. ID 是主键.
2> CREATE TABLE test_main (
3> id INT,
4> value VARCHAR(10),
5> PRIMARY KEY(id)
6> );
7> go
-- 建表时设置外键
1> CREATE TABLE test_sub (
2> id INT,
3> main_id INT,
4> value VARCHAR(10),
5> PRIMARY KEY(id),
6> FOREIGN KEY (main_id) REFERENCES test_main
7> );
8> go
sql server 2008 怎么设置外键
建外键的前提是此外键必须是另外一个表的主键。建外键的步骤: 第一步打开要建外键表的设计器,右击选择“关系”。然后弹出“外键关系”窗体,我们选择“添加”,然后点击“表和列规范”后面的小按钮,就会弹出另外一个窗体让我们选择主键表和列,选好之后点击确定。然后我们INSERT和UPDATE规范,在更新规则和删除规则有四个选项,分别是“不执行任何操作”、“级联”、“设置为NULL”、“设置默认值”。默认的不执行任何操作。如果是“不执行任何操作”,当我们删除或更新主键表的数据时,会告诉用户不能执行删除或更新该操作。“级联”的意思是当我们删除或更新主键表的数据时,会删除或更新外键表中所涉及的相关数据的所有行。 “设置Null”的意思是当我们删除或更新主键表的数据时,外键表中的外键列的值会设为Null,但前提是该列允许为空。 “设置默认值”的意思是如果我们将外键列定义了默认值,当我们删除或更新主键表的数据时,外键表中的外键列的值设为定义的默认值。 当然我们可以用代码创建,当我们在创建数据库表T——Card时只要加上一句话就OK啦,“Foreign key (studentNo) references T_Student(studentNo)"。如果我们已经创建了改表,那如何用代码实现了,这也很简单也就一句话“ add constraint CMPKey(外键名) foreign key(studentNo) references T_Student(studentNo)”。
sql中怎样创建外键约束
在创建表之后,添加外键约束:
alter table yuangong add constraint fk foreign key (部门罚) references bumen(部门号)
或者在创建表的时候添加外键
foreign key (部门号) references bumen(部门号)放在最后,用","与列分隔
数据库中,一对多的时候外键设置在多的那张表吗?如果一对一的时候,外键应该设置在哪里?多对多的时候,
首先,外键引用的那个列在主表中必须是主键列或者唯一列。
所以1:n的肯定把外键建立在n的那张表上。
1:1,一般要看谁是主表,谁是附属表,外键当然建立在附属表中。
n:m的情况,需要建立一个关系表,两个原表和其关系分别是1:n,1
:m
数据库语句怎么加外键
1,创建表的时候添加:foreign key (你的外键) references (表名)(字段名);
2,创建好之后修改:
alter table dbo.mh_User
add constraint FK_mh_User_..._id foreign key (你的外键) references (表名)(字段名);
Sql server怎样创建主外键关系
在要设置关系的外键表中,右击关系→添加→在表和列规范中选择关联的主表再选择外键表与其关联的字段
B. 数据库外键约束
数据库外键约束:这个是实现表与表之间的约束,从表的字段值必须在主表中。存在外键约束毕竟是一个约束,只是保证数据完整性的一个手段。
C. SQL数据库的、外键和查询
增加外键
创建表的时候增加外键:在所有的表字段之后,使用foreign key(外键字段) references 外部表(主键字段)
在新增表之后增加外键:修改表结构,使用alter table 表名 add [constraint 外键名字] foreign key(外键字段) references 父表(主键字段);
修改外键&删除外键
alter table 表名 drop foreign key 外键名;
外键条件
外键要存在,首先必须保证表的存储引擎是innodb
列类型必须与父表的主键类型一致
一张表中的外键名字不能重复
增加外键的字段数据已经存在,必须保证数据与父表主键要求对应
外键约束
有三种约束模式
district:严格模式(默认的)
cascade:级联模式
set null:置空模式
语法:foreign key(外键字段) references 父表(主键字段) on delete 模式 on update 模式;
联合查询
基本语法:
select 语句1
union [union 选项]
select 语句2……
union 选项
all:保留所有,不管重复
distinct:去重,默认的
子查询(sub query)
按位置分类
from子查询
where子查询
exists子查询
按结果分类
标量子查询
列子查询
行子查询
表子查询
子查询
列子查询
=any等价于in; -- 其中一个即可
any等价于some; -- 二者是一样的
=all为全部
-- 创建外键
create table my_foreign1(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id',
-- 增加外键
foreign key(c_id)references
my_class(id)
)charset utf8;
-- 创建表
create table my_foreign2(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id' -- 普通字段
)charset utf8;
-- 增加外键
alter table my_foreign2add
-- 指定外键的名字
constraint student_class_1 -- 可以指定多个外键 但是名字不能相同
-- 指定外键的字段
foreign key(c_id)
-- 引用父表主键
references my_class(id);
-- 删除外键
alter table my_foreign1drop
foreign key my_foreign1_ibfk_1; -- my_foreign1_ibfk_1 通过外键的名字来删
-- 插入数据;外键字段在父表不存在
insert into my_foreign2values (
null,'郭富城',4); -- 没有4号班级
insert into my_foreign2values (
null,'项羽',1);
insert into my_foreign2values (
null,'刘邦',2);
insert into my_foreign2values (
null,'韩信',3);
-- 更新父表的记录
update my_classset id=4 where id=1; -- 失败;id=1记录已经被学生引用
update my_foreign2set c_id=2 where id=4; -- 更新
update my_classset id=4 where id=3; -- 可以;没有学生引用此班级
-- mysql中添加外键约束遇到一下情况:
-- cannot add foreign key constraint
-- 出现这个问题的原因是,外键的使用:
-- 1. 外键字段不能为该表的主键;
-- 2. 外键字段参考字段必须为参考表的主键
-- 插入数据
insert into my_foreign1values (
null,'马超','3'
);
-- 增加外键
alter table my_foreign1add
foreign key(c_id)references
my_class(id); -- 失败;因为没有3号班了
-- 创建外键,指定模式;删除置空;更新级联
create table my_foreign3(
idint primary key auto_increment,
name varchar (20)not null,
c_idint,
-- 增加外键
foreign key (c_id)
-- 引用表
references my_class(id)
-- 指定删除模式
on delete set null
-- 指定更新模式
on update cascade
)charset utf8;
-- 插入数据
insert into my_foreign3values (
null,'刘备',1),
(null,'曹操',1),
(null,'孙权',1),
(null,'祝贺量',2),
(null,'周瑜',2);
-- 解除My_foreign2表的外键
alter table my_foreign2drop
foreign key student_class_1;
-- 更新父表主键
update my_classset id=3 where id=1;
-- 删除父表主键
delete from my_classwhere id=2;
-- 联合查询
select * from my_class
union -- 默认去重
select * from my_class;
select * from my_class
union all -- 不去重
select * from my_class;
select id,c_name,roomfrom my_class
union all -- 不去重
select name,number,idfrom my_student;
-- 需求;男生升序;女生降序(年龄)
(select * from my_student
where sex='男'
order by ageasc limit9999999)
union
(select * from my_student
where sex='女'
order by agedesc limit9999999);
select * from my_studentwhere
c_id=(
-- 标量子查询
select idfrom my_classwhere
c_name='python1903');-- id一定只有一个值(一行一列)
insert into my_classvalues (1,
'python1907','B407');
-- 列子查询
select * from my_studentwhere
c_idin(select idfrom my_class);
-- any,some,all
select * from my_studentwhere
c_id=any(select idfrom my_class);
select * from my_studentwhere
c_id=some(select idfrom my_class);
select * from my_studentwhere
c_id=all(select idfrom my_class);
select * from my_studentwhere
c_id!=any(select idfrom my_class); -- 所有结果(null除外)
select * from my_studentwhere
c_id!=some(select idfrom my_class); -- 所有结果(null除外)
select * from my_studentwhere
c_id!=all(select idfrom my_class); -- 所有2号班级(null除外)
select * from my_studentwhere
age=(select max(age)from
my_student)
and
height=(select max(height))from
my_student);
-- 行子查询
select * from my_student
-- (age,height)称之内为行元素
where (age,height)=(select max(
age),max(height)from my_student);
update my_studentset height=188
where name='王五';
select * from my_studentorder by
agedesc,heightdesc limit1;
select * from my_studentorder by
heightdesc;
-- 表子查询
select * from my_studentgroup by
c_idorder by heightdesc; -- 每个班选出第一个学生再按身高排序
select * from (select * from
my_studentorder by heightdesc)
as studentgroup by student.c_id;
D. mysql数据库如何添加外键
mysql增加外键的方法:1、在CREATE TABLE语句中,通过FOREIGN KEY关键字来添加外键;2、在ALTER TABLE语句中,通过ADD和FOREIGN KEY关键字来添加外键。
E. 怎么为数据库表添加外键
1,创建表的时候添加:foreign key (你的外键) references (表名)(字段名); 2,创建好之后修改: alter table dbo.mh_User add constraint FK_mh_User_..._id foreign key (你的外键) references (表名)(字段名);
F. SQL数据库外键代码
1.创建主测试表(test_class),
Createtabletest_class(class_idnumber,class_namevarchar2(20));

(6)数据库添加外键扩展阅读:
1.高可用性:
分布式组织的可扩展性,决策支持的数据仓库功能,与许多其他服务器软件紧密相关的集成,良好的性价比,等等。
2.数据管理和分析的灵活性:
允许单位在快速变化的环境中做出冷静的反应,从而获得竞争优势。从数据管理和分析的角度来看,将原始数据转换为商业智能并充分利用Web的机会是很重要的。
作为一个完整的数据库和数据分析软件包,SQLServer为新一代企业业务应用的快速发展,为企业赢得核心竞争优势打开了胜利之门。
G. 如何通过sqlyog对数据库表设置外键
要求:建立了两个表一个attence(记作A表),一个leave(记作L表)。
A表中有用户的用户名,密码,id(自增作为主键)。
L表中是一个请假的表,L表中需要标识哪一个用户请的假。
这里需要设置外键:
步骤:
1、

就完成了外键的设置。
H. 教你快速掌握DB2数据库创建外键时的选项
免费看《Windows CE 嵌入式系统开发 从基础到实践》创建外键时的选项
创建测试表
drop table student;drop table class;drop table student_class;Create table student(student_id integer not null student_name varchar( ) CONSTRAINT P_KEY_ primary key (student_id)) in luzl_ k_tb index in luzl_ k_tb ;Create table class(class_id integer not null class_name varchar( ) CONSTRAINT P_KEY_ primary key (class_id)) in luzl_ k_tb index in luzl_ k_tb ;Create table student_class(student_class_id integer student_id integer class_id integer) in luzl_ k_tb index in luzl_ k_tb;alter table student_class add constraint if_class foreign key(class_id) references class(class_id) ON DELETE cascade ON UPDATE RESTRICT;alter table student_class add constraint if_student foreign key(student_id) references student(student_id) ON DELETE cascade ON UPDATE RESTRICT;Insert into student(student_id student_name) values( luzl );Insert into class(class_id class_name) values( db );Insert into student_class(student_class_id student_id class_id) values( );
On Delete 的选项有
Restrict/no action/cascade/set null 其中cascade选项指定的话 如果删除父记录 依赖于他的子记录也会自动删除 相当于级联删除 如果指定no action和cascade都会报错 因为还有子记录所以无法删除该记录 set nul允许删除父记录并且l会将子表中与父表关联的字段设置为null
On Update 只有两个选项 no action/restrict 它们在更新和删除时并没有区别:如果与子表关联不允许删除
lishixin/Article/program/DB2/201311/21928
I. 数据库建表时怎么设置外键
数据库建表时怎么设置外键步骤如下:
第一步、打开要建外键表的设计器,右击选择“关系”。
第二步、然后弹出“外键关系”窗体,我们选择“添加”,然后点击“表和列规范”后面的小按钮。
第三步、弹出另外一个窗体让我们选择主键表和列,记住要选择相同的,选好之后点击确定。
第四步、展开INSERT和UPDATE规范,在更新规则和删除规则有四个选项,分别是“不执行任何操作”、“级联”、“设置为NULL”、“设置默认值”。
J. SQL数据库建表时怎么设置外键
1>
--
创建测试主表.
ID
是主键.
2>
CREATE
TABLE
test_main
(
3>
id
INT,
4>
value
VARCHAR(10),
5>
PRIMARY
KEY(id)
6>
);
7>
go
--
建表时设置外键
1>
CREATE
TABLE
test_sub
(
2>
id
INT,
3>
main_id
INT,
4>
value
VARCHAR(10),
5>
PRIMARY
KEY(id),
6>
FOREIGN
KEY
(main_id)
REFERENCES
test_main
7>
);
8>
go
