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

In MySQL, sorting uses the ORDERBY clause, and ranking uses the RANK(), DENSE_RANK(), and ROW_NUMBER() functions. 1. Sort: Use ORDERBY clause, such as SELECT*FROMemployeesORDERBYsalaryDESC; 2. Ranking: Use window functions, such as SELECTemployee_name, salary, RANK()OVER(ORDERBYsalaryDESC)ASrankFROMemployees; these operations are based on SQL query optimizer and execution engine, and are often used to sort quickly or merge sort, and ranking depends on window function calculation.

To create and call stored procedures in MySQL, follow the following steps: 1. Create stored procedures: Use the CREATEPROCEDURE statement to define stored procedures, including names, parameters, and SQL statements. 2. Compile stored procedures: MySQL compiles stored procedures into executable code and stores them. 3. Call stored procedure: use CALL statement and pass parameters. 4. Execute stored procedures: MySQL executes the SQL statements in it, processes parameters and returns the result.

The MySQL service can be set to automatically start on Windows, Linux, and macOS. 1) On Windows, use the command "scconfigmysqlstart=auto" to configure. 2) On Linux, enable it using "sudosystemctlenablemysql". 3) On macOS, create and load the launchd configuration file to achieve automatic startup.

The methods to view the MySQL table structure include: 1. Use the DESCRIBE command to view column information; 2. Use the SHOWCREATETABLE command to view table creation statements; 3. Use information_schema to query more detailed information. These methods help to quickly understand table structure and improve work efficiency.

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

In MySQL, conditional filtering is implemented through the WHERE clause and grouping is completed through the GROUPBY clause. 1. Use the WHERE clause to filter data, such as finding employees with salary above 5,000. 2. Use the GROUPBY clause to group and aggregate data, such as counting the number of employees by department. 3. Choose the appropriate index to optimize query performance and avoid using functions or expressions as WHERE conditions. 4. Combining subqueries and EXPLAIN commands improve the efficiency of complex queries.

In MySQL, clearing table data but preserving table structure can be implemented through the TRUNCATETABLE and DELETE commands. 1. The TRUNCATETABLE command quickly deletes all records and resets the self-increment column. 2. The DELETE command deletes data line by line, does not reset the self-increment column, and can delete specific records in combination with the WHERE clause.

Deduplication in MySQL mainly uses DISTINCT and GROUPBY. 1.DISTINCT is used to return unique values, such as SELECTDISTINCTname, ageFROMusers. 2. GROUPBY realizes deduplication through grouping and can perform aggregation operations, such as SELECTid, name, MAX(created_at)aslatest_dateFROMusersGROUPBYname.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

Zend Studio 13.0.1
Powerful PHP integrated development environment
