㈠ sql怎样查询外键关系
看了楼主的问题补充,你可以在对象资源管理器中,选中表A,右键选‘设计’,然后在表中选中任意键值,右键选‘关系’,这个表所有的外键关系都会列出来了。
另外在对象资源管理器中,还有一个是数据库关系图,但是那个是你自己设计好的,如果之前没有人设计这些,那么默认是空的。
㈡ sql 通过外键查出每个外键对应数据的条数
2个表tab1,tab2,tab1的key外键关联tab2的key
select a.key,b.cnt as 条数 from tab1 a
left join (select key,count(*) as cnt from tab2 group by key) b
on a.key=b.key
㈢ 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主键外键查询
selectt1.agefromt1,t2
wheret1.name=t2.name
andt2.point='xxx';
㈤ 怎样通过sql语句查看外键 sql server2000
select INDEX_NAME 索引名, b.TABLE_NAME 主键表名, a.TABLE_NAME 外键表名, CONSTRAINT_TYPE, CONSTRAINT_NAME 约束名
from all_indexes a, all_constraints b
where b.TABLE_NAME='AC' AND CONSTRAINT_TYPE IN('P','R')
and R_CONSTRAINT_NAME=INDEX_NAME(+)
/
CONSTRAINT_TYPE='P'为主键,='R'为外键
㈥ mysql如何查看外键
查看mysql外键方式主要是通过第三方工具或者是sql语句,主要有以下三种方式
1、使用Navicateformysql,打开数据库、查看数据库表、查看设计表、选择外键选项卡,就可以查看外键
2、使用sql语句
showcreatetable表名;这个命令可以查看表的所有信息,包括一些字段类型,字段的约束,外键,主键,索引,字符编码等等。
3、查看某个表或者某个列的外键信息
selectTABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = '<table>';
如果需要查看某一列上的外键关系,需要添加列的条件REFERENCED_COLUMN_NAME.xx=xx
方法一比较直观,方法三比较准确!
(6)sql查外键引用次数扩展阅读:
MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理。
由于MySQL是开放源代码的,因此任何人都可以在GeneralPublicLicense的许可下下载并根据个性化的需要对其进行修改。MySQL因为其速度、可靠性和适应性而备受关注。大多数人都认为在不需要事务化处理的情况下,MySQL是管理内容最好的选择。
㈦ Sql server2005查询主表所有名称,以及与主表每条记录相关的外键记录条数
SELECT
主表.所有名称,
COUNT(子表.主键) AS 与主表每条记录相关的外键记录条数
FROM
主表 LEFT OUTER JOIN 子表 ON (主表.主键 = 子表.外键)
GROUP BY
主表.所有名称
㈧ SQL找出一个外键所被引用过的表
select
oSub.nameAS[子表名称],
fk.nameAS[外键名称],
SubCol.nameAS[子表列名],
oMain.nameAS[主表名称],
MainCol.nameAS[主表列名]
from
sys.foreign_keysfk
JOINsys.all_objectsoSub
ON(fk.parent_object_id=oSub.object_id)
JOINsys.all_objectsoMain
ON(fk.referenced_object_id=oMain.object_id)
JOINsys.foreign_key_columnsfkCols
ON(fk.object_id=fkCols.constraint_object_id)
JOINsys.columnsSubCol
ON(oSub.object_id=SubCol.object_id
ANDfkCols.parent_column_id=SubCol.column_id)
JOINsys.columnsMainCol
ON(oMain.object_id=MainCol.object_id
ANDfkCols.referenced_column_id=MainCol.column_id)
自己在最后 加一个 WHERE fk.name = 'Plan01_FK'
注: 上面的 SQL Server 2008 下的 sql 语句。
㈨ SQL中,一个表有多个外键,如何查询外键对应的值,和如何进行插入
这个就是典型的插入异常..
以为你插入的这个表和其他表有外键联系..插入的时候其他表中没有这个外键数据所以就会报错..
最好是在程序里面先插入外键说需要的数据再插入这条数据,这样就不会出错..如果不想这么麻烦就去除外键约束,但是这样虽然可以插入成功但是以后会出现很多孤儿数据..
查询没什么问题...如果你想插入一条A表的数据,那么就必须保证A表的j,k,i字段在响应的B,C,D表有数据..不然就会报以上错误..具体程序可以先判断你要插入的这条数据中的j,k,i在B,C,D存在不..不存在提示用户,存在这插入..
㈩ SQL怎么查询外键引用次数
1、select A.*,B.AidNum from A,(select Aid,count(Aid) as AidNum from B group by Aid) as B
where A.Aid=B.Aid
2、select A.*,C.CountNum from A,
(select B.Aid,count(B.Aid) as CountNum from B,C where B.Bid=C.Bid group by B.Aid) as C
where A.Aid=C.Aid