当前位置:首页 » 编程语言 » 外键引用sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

外键引用sql

发布时间: 2023-01-19 00:02:50

sql中外键怎么写

1、创建测试表;

create table test_class(class_id varchar2(10), class_name varchar2(30));

create table test_student(student_id varchar2(10), student_name varchar2(30), class_id varchar2(10));

㈡ 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;

㈢ SQL 数据库 外键

首先楼主要明白不同表中的相同字段是没有任何关系的 ,然后也没看到你的B表
你的问题:你的第一个问题你自己不觉得是自相矛盾吗?
A表中的主键是B表的外键,那么这个外键在B表中可以是主键吗? 这句话你的意思就相当于问 主键和外键可以是一个字段吗? 那么回答肯定是否定的 。一个字段要么是主键 要么是外键 不可能又是主键又是外键 ,然后主键和主键是同一级别的 没有谁约束谁,只有主键可以约束外键,然后我给楼主讲解一下主外键的关系,不要死记硬背定义,要理解。

比如a表的主键 如果是b表的外键的话 那么这个外键的每一个值都必须在a的主键里存在,如果b的这个外键定义可以为空的话,那么b这个外键的值只有两个情况:要么值在a的主键里选,要么为空。
就这么简单的一句话。

㈣ Sql server怎样创建主外键关系

Sqlserver怎样创建主外键关系的方法。

如下参考:

1.为了帮助你理解,用一个例子来帮助你理解。它基本上是指通过引用表2中的字段来约束表1中的字段。

㈤ sql如何引用外键

首先要建立关联的主外键 绑定数据,然后根据主外键关系给你(就好是B)的字段赋值

㈥ sql主码引用外码怎么输入

SQL语言创建表时候用Primary Key(属性名)定义主码,用Foreign Key(属性名)定义外码。
主码是一种唯一关键字,表定义的一部分。一个表的主码可以由多个关键字共同组成,并且主码的列不能包含空值。主码是可选的,并且可在 CREATE TABLE语句中用Primary Key(属性名)定义。
将一个表的值放入第二个表来表示关联,所使用的值是第一个表的主键值(在必要时可包括复合主键值)。此时,第二个表中保存这些值的属性称为外键,用Foreign Key(属性名)定义。
结构化查询语言(Structured Query Language)简称SQL,结构化查询语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。
sql语句就是对数据库进行操作的一种语言。
总结如下:
主码不应包含动态变化的数据,如时间戳、创建时间列、修改时间列等。实际上,因为主码除了唯一地标识一行之外,再没有其他的用途了,所以也就没有理由去对它更新。如果主码需要更新,则说明主码应对用户无意义的原则被违反了。

㈦ sql怎么设置外键

sql server中建立外键约束有3中方式:enterprise manager中,tables,design table,设置table的properties,可以建立constraint, reference key;enterprise manager中,diagrams, new diagrams,建立两个表的关系;直接用transact sql语句。

1、三个方法都需要先建立数据表。

1)创建表author :

create table [dbo].[author] (
[id] [bigint] not null ,
[authorname] [char] (10) null ,
[address] [char] (480) null ,
[introction] [ntext] null
)

2)创建表mybbs:

reate table [dbo].[mybbs] (
[id] [bigint] identity (1, 1) not null ,
[authorid] [bigint] not null ,
[title] [char] (40) null ,
[date_of_created] [datetime] null ,
[abstract] [char] (480) null ,
[content] [ntext] null
)

2、设置表mybbs中的authorid为外键,参照author表的id字段,直接使用transact sql语句,过程如下:

1)增加表mybbs(authorid)的外键约束fk_mybbs_author,表mybbs中的authorid受表author中的主键id约束:

begin transaction
alter table dbo.mybbs add constraint fk_mybbs_author
foreign key (authorid)
references dbo.author([id]) on update cascade on delete cascade

2)删除外键约束fk_mybbs_author:
--alter table dbo.mybbs drop constraint fk_mybbs_author
--rollback
commit transaction

上面on update cascade,on delete cascade两个选项,指明以后author表的id字段有delete,update操作时,mybbs表中的id也会被级联删除或更新。如果没有选中,是不可以对author表中已被mybbs表关联的id进行update或者delete操作的。

拓展资料:

SQL的主键和外键的作用:

1、插入非空值时,如果主键表中没有这个值,则不能插入。

2、更新时,不能改为主键表中没有的值。

3、删除主键表记录时,你可以在建外键时选定外键记录一起级联删除还是拒绝删除。

4、更新主键记录时,同样有级联更新和拒绝执行的选择。

简而言之,SQL的主键和外键就是起约束作用。

㈧ 关于SQL建表引用外键问题

系主任电话 char(15) default'', Constraint SdeptPK Primary Key (系号,系名),)建立这个Sdept表成功了。 Create table Teacher( 教师编号 char(10) not null, 教师姓名 char(15) not null, 教师性别 char(15) default'', 教师出生日期 datetime, 职称 char(10) default'', 教师政治面貌 char(10) default'', 办公室房间号 char(10) default'', 教师电话 char(15) default'', 系名 char(30) default'', Constraint TeacherPK Primary Key (教师编号), Constraint TeacherFK Foreign Key (系名) References Sdept (系名), Constraint DatatimeCK Check (教师出生日期1900-1-1 AND 教师出生日期<2000-1-1))出现错误!服务器: 消息 1776,级别 16,状态 1,行 1 在被引用表 'Sdept' 中没有与外键 'SdeptFK' 的引用列的列表匹配的主键或候选键。 服务器: 消息 1750,级别 16,状态 1,行 1 未能创建约束。请参阅前面的错误信息。 哪位牛人给出结果啊?!!!急!