bitsCN.com
外键和内外左右连接的基础知识
由于现有公司一直不用mysql的外键(影响库的性能,采用代码逻辑控制),所以我对外键也不是很了解,今天特意在网上看了些有关外键的资料,顺便搜到了些内外连接的知识,一起整理起来。
1mysql的外键相关知识
mysql的外键有三种,如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表。外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常的维护工作更加轻松。常见的外键 有级联(cascade)方式,置空(set null)方式及禁止(no action / restrict)方式
a测试级联方式:
01create table test111 (02 id int not null,03 name varchar(30),04 primary key (id)05);06create table test222 (07 id int not null,08 name varchar(30),09 groupid int,10 primary key (id),11 foreign key (groupid) references test111(id) on delete cascade on update cascade12);1314insert into test111 values (1, 'Group1');15insert into test111 values (2, 'Group2');1617insert into test222 values (1, 'qianxin', 1);18insert into test222 values (2, 'yiyu', 2);19insert into test222 values (3, 'dai', 2);20delete from test111 where id=2; 21update test111 set id=2 where id=1;
这种情况下无法插入,用户组3不存在,与参照完整性约束不符
删除时test222r中的2、3记录级联删除
更新时test222的1记录的groupid级联修改为2
b测试置空(set null)方式
01create table test111 (02 id int not null,03 name varchar(30),04 primary key (id)05);06create table test333 (07 id int not null,08 name varchar(30),09 groupid int,10 primary key (id),11 foreign key (groupid) references test111(id) on delete set null on update set null12);1314insert into test111 values (1, 'Group1');15insert into test111 values (2, 'Group2');1617insert into test333 values (1, 'qianxin', 1);18insert into test333 values (2, 'yiyu', 2);19insert into test333 values (3, 'dai', 3);20delete from test111 where id=2; 21update test111 set id=2 where id=1;
无法插入,用户组3不存在,与参照完整性约束不符
删除是导致test333中的2、3记录的groupid被设置为NULL、
更新时导致t_user中的1记录的groupid被设置为NULL
c测试禁止(no action / restrict)方式
01create table test111 (02 id int not null,03 name varchar(30),04 primary key (id)05);06create table test444 (07 id int not null,08 name varchar(30),09 groupid int,10 primary key (id),11 foreign key (groupid) references test111(id) on delete no action on update no action12);1314insert into test111 values (1, 'Group1');15insert into test111 values (2, 'Group2');1617insert into test444 values (1, 'qianxin', 1);18insert into test444 values (2, 'yiyu', 2);19insert into test444 values (3, 'dai', 3);20delete from test111 where id=2; 21update test111 set id=2 where id=1;
插入时无法插入,用户组3不存在,与参照完整性约束不符
删除时从表中有相关引用,因此主表中无法删除
更新时从表中有相关引用,因此主表中无法修改
在MySQL中,restrict方式与no action方式作用相同。
2 oracle的外键
外键是该表是另一个表之间联接的字段 ,必须为另一个表中的主键 用途是确保数据的完整性。它通常包括以下几种:
A实体完整性,确保每个实体是唯一的(通过主键来实施)
B域完整性,确保属性值只从一套特定可选的集合里选择
C关联完整性,确保每个外键或是NULL(如果允许的话)或含有与相关主键值相配的值
下面是一个测试例子,如何建立两个表的外键
01CREATE TABLE ZZ_STUDENT (02 ID CHAR (10),03 NAME VARCHAR (8),04 SEX CHAR (1)05);0607ALTER TABLE ZZ_STUDENT ADD CONSTRAINT PK_STUDENT PRIMARY KEY (ID);0809CREATE TABLE ZZ_SCORE (ID CHAR(10), MATH NUMBER(5, 2));1011ALTER TABLE ZZ_SCORE ADD CONSTRAINT FK_SCROE FOREIGN KEY (ID) REFERENCES ZZ_STUDENT (ID);
3数据库内连接外连接左连接右连接
内连接:把两个表中数据对应的数据查出来
外连接:以某个表为基础把对应数据查出来
全连接是以多个表为基础
首先插入如下测试数据:
1insert into student(sno,sname) values('2005001','小施');2insert into student(sno,sname) values('2005002','小王');3insert into student(sno,sname) values('2005003','小张');4insert into student(sno,sname) values('2005004','小贾');5insert into stu_score(sno,scrore) values('2005001',90.00);6insert into stu_score(sno,scrore) values('2005002',95.00);7insert into stu_score(sno,scrore) values('2005008',80.50);8insert into stu_score(sno,scrore) values('2005009',88.50);
(一) 内连接
内连接,inner join,join 查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的
列值。
----假设a表有M条记录,b表有N条记录,a和b表sno相同的记录有K条----内连接(数据量=K)1select a.*,b.* from student a join stu_score b on a.sno=b.sno;2select a.*,b.* from student a inner join stu_score b on a.sno=b.sno;查询结果如下:2005001 小施 2005001 902005002 小王 2005002 95
(二) 外连接 (左连接、右连接)
左连接或left join,left outer join 返回包括左表中的所有记录和右表中联接字段相等的记录右连接或right join ,right outer join 返回包括右表中的所有记录和由表中联接字段相等的记录----左连接/左外连接(数据量=M) select a.*,b.* from student a left join stu_score b on a.sno=b.sno; select a.*,b.* from student a left outer join stu_score b on a.sno=b.sno;查询结果如下:2005001 小施 2005001 902005002 小王 2005002 952005003 小张 2005004 小贾----右连接/右外连接(数据量=N)select a.*,b.* from student a right join stu_score b on a.sno=b.sno;select a.*,b.* from student a right outer join stu_score b on a.sno=b.sno;查询结果如下:2005001 小施 2005001 902005002 小王 2005002 952005008 80.52005009 88.5
(三) 完全连接()
定义:在内连接的基础上,还包含两个表中所有不符合条件的数据行,并在其中的左表、和右表列填写NULL
关键字:FULL JOIN
----完全连接(数据量=M+N-K)select a.*,b.* from student a full join stu_score b on a.sno=b.sno;
查询的时候mysql是不支持的
(四) 交叉连接
定义:将两个表的所有行进行组合,连接后的行数为两个表的乘积数。(笛卡尔积)
关键词:CROSS JOIN
格式:FROM 表名1 CROSS JOIN 表名2
----交叉连接(数据量=M*N)
select a.*,b.* from student a cross join stu_score b ;
查询结果如下:
2005001 小施 2005001 902005002 小王 2005001 902005003 小张 2005001 902005004 小贾 2005001 902005001 小施 2005002 952005002 小王 2005002 952005003 小张 2005002 952005004 小贾 2005002 952005001 小施 2005008 80.52005002 小王 2005008 80.52005003 小张 2005008 80.52005004 小贾 2005008 80.52005001 小施 2005009 88.52005002 小王 2005009 88.52005003 小张 2005009 88.52005004 小贾 2005009 88.5
bitsCN.com

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

“mysql-connector”是mysql官方提供的驱动器,可以用于连接使用mysql;可利用“pip install mysql-connector”命令进行安装,利用“import mysql.connector”测试是否安装成功。

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。

方法:1、利用“字符串+0”转换,语法为“(column+0)”;2、利用CONVERT函数转换,语法为“CONVERT(column,SIGNED)”;3、利用CAST函数转换,语法为“CAST(column as SIGNED)”。

在mysql中,可利用“ALTER TABLE 表名 DROP INDEX unique key名”语句来删除unique key;ALTER TABLE语句用于对数据进行添加、删除或修改操作,DROP INDEX语句用于表示删除约束操作。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
