创建数据库时为了防止其他人不小心删除操作错误, 所有的外键都没有加级联删除。哪知,不知什么时候自己入了一批错误的数据进去,入库使用的是软件自动的,一下点错给自己带来无尽麻烦啊,删除就不好办了 表间的关系比较复杂,数据量又比较多,一个个删绝对
创建数据库时为了防止其他人不小心删除操作错误, 所有的外键都没有加级联删除。哪知,不知什么时候自己入了一批错误的数据进去,入库使用的是软件自动的,一下点错给自己带来无尽麻烦啊,删除就不好办了
表间的关系比较复杂,数据量又比较多,一个个删绝对会出大问题。于是实验了几种解决的办法,现小结一下。
方法一:创建约束时设定级联删除
(但一般由于各种原因或出于各种考虑在创建数据库时没有设定级联删除)
SQL语句:
复制代码 代码如下:
CREATE TABLE "U_WEN_BOOK"."FARTAB" ("FAR_ID" NUMBER(10) NOT NULL,
"FAR_NAME" VARCHAR2(10), "OTHER_NAME" VARCHAR2(10),
CONSTRAINT "PK_FER" PRIMARY KEY("FAR_ID"))
CREATE TABLE "U_WEN_BOOK"."CHILTAB" ("CHIL_ID" NUMBER(10) NOT
NULL, "CHIL_NAME" VARCHAR2(10), "CHIL_OTHER" VARCHAR2(10),
"FAR_ID" NUMBER(10) NOT NULL,
CONSTRAINT "PK_CHIL" PRIMARY KEY("CHIL_ID"),
CONSTRAINT "FK_CHIL" FOREIGN KEY("FAR_ID")
REFERENCES "U_WEN_BOOK"."FARTAB"("FAR_ID") ON DELETE CASCADE)
方法二:创建约束时没有使用级联删除,,在需要使用级联删除时,删除原来的外键约束,重建带级联删除的约束
(实验证明完全可行,注意需要对已经存在的数据进行验证,否则新建的约束对原有数据不具备效率,默认是验证的,若强制要求不验证,使用NOVALIDATE关键词。还有外键引用的只能是唯一主键)
SQL语句:
复制代码 代码如下:
ALTER TABLE "U_WEN_BOOK"."GCHILTAB1"
DROP CONSTRAINT "FK_G1"
ALTER TABLE "U_WEN_BOOK"."GCHILTAB1"
ADD (CONSTRAINT "FK_G1" FOREIGN KEY()
REFERENCES "U_WEN_BOOK"."CHILTAB"()
ON DELETE CASCADE)
(这样就可以级联删除了,删除完后,如果不放心这样的约束条件,并且不嫌麻烦可以再重建为不带级联删除等外键约束,防止误操作)
方法三:使用触发器(创建时没有级联删除)
(比较灵活,可以根据自己编写的程序进行,引用的不是唯一主键也可以)
(1)创建表及插入数据
SQL语句:
复制代码 代码如下:
create table orderCombine (
O_Id VARCHAR2(16) not null,
OrderId VARCHAR2(15) not null,
FormerId VARCHAR2(16) not null,
constraint PK_ORDERCOMBINE primary key (FormerId) );
create table VIPForm (
V_Id VARCHAR2(16) not null,
IsValid CHAR(1) default '0' not null ,
Constraint fk_vipform foreign key(V_id) references ordercombine(formerid) );
insert into orderCombine values('1','1','1'); insert into orderCombine values('2','2','2'); insert into vipform values('1','5'); insert into vipform values('2','4'); insert into vipform values('1','2');
结果:
(2)创建触发器:
SQL:
复制代码 代码如下:
CREATE OR REPLACE TRIGGER "FG123"."TER_OV"
BEFORE
DELETE ON "ORDERCOMBINE" FOR EACH ROW
BEGIN
DELETE FROM VIPForm
WHERE VIPForm.V_Id=:OLD.FormerId;
END;
(3)删除及结果:
SQL:
复制代码 代码如下:
DELETE FROM "FG123"."ORDERCOMBINE"
WHERE FormerId ='1'
方法四:若表间关系简单(就两张表),涉及到的记录也很少(总共就几行记录),直接删除子表中的相关记录,再删除父表中的记录即可。
(前面的方法在涉及数据量较大以及表间关系比较复杂时才有效率上的优势,简单的直接删除来的更快)
如上例中,直接删除
SQL语句:
复制代码 代码如下:
DELETE FROM "FG123"."ORDERCOMBINE"
WHERE VIPForm ='1';
DELETE FROM "FG123"."ORDERCOMBINE"
WHERE FormerId ='1'

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Notepad++7.3.1
Easy-to-use and free code editor
