SQL/MySQL/Join/Delete From Join

Материал из SQL эксперт
Версия от 10:16, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Delete records with JOIN

Drop table DVDs;
Drop table Studios;

CREATE TABLE DVDs
(
   DVDID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   DVDName VARCHAR(60) NOT NULL,
   NumDisks TINYINT NOT NULL DEFAULT 1,
   YearRlsd YEAR NOT NULL,
   StudID VARCHAR(4) NOT NULL
) ENGINE=INNODB;

CREATE TABLE Studios
(
   StudID VARCHAR(4) NOT NULL,
   StudDescrip VARCHAR(40) NOT NULL,
   PRIMARY KEY (StudID)
)
ENGINE=INNODB;
INSERT INTO Studios VALUES ("s101", "Universal Studios"),
                           ("s102", "Warner Brothers"),
                           ("s103", "Time Warner"),
                           ("s104", "Columbia Pictures"),
                           ("s105", "Paramount Pictures"),
                           ("s106", "Twentieth Century Fox"),
                           ("s107", "Merchant Ivory Production");

INSERT INTO DVDs 
(DVDName, NumDisks, YearRlsd, StudID)
VALUES 
     ("Christmas", 1, 2000, "s105"),
     ("What",      1, 2001, "s103"),
     ("Out",       1, 2000, "s101"),
     ("Falcon",    1, 2000, "s103"),
     ("Amadeus",   1, 1997, "s103"),
     ("Show",      2, 2000, "s106"),
     ("View",      1, 2000, "s107"),
     ("Mash",      2, 2001, "s106");
select * from DVDs;
DELETE DVDs
FROM DVDs, Studios
WHERE DVDs.StudID=Studios.StudID
   AND Studios.StudDescrip="Metro-Goldwyn-Mayer";

select * from DVDs;



Delete with JOIN 3

Drop table DVDs;
Drop table Studios;

CREATE TABLE DVDs
(
   DVDID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   DVDName VARCHAR(60) NOT NULL,
   NumDisks TINYINT NOT NULL DEFAULT 1,
   YearRlsd YEAR NOT NULL,
   StudID VARCHAR(4) NOT NULL
) ENGINE=INNODB;

CREATE TABLE Studios
(
   StudID VARCHAR(4) NOT NULL,
   StudDescrip VARCHAR(40) NOT NULL,
   PRIMARY KEY (StudID)
)
ENGINE=INNODB;
INSERT INTO Studios VALUES ("s101", "Universal Studios"),
                           ("s102", "Warner Brothers"),
                           ("s103", "Time Warner"),
                           ("s104", "Columbia Pictures"),
                           ("s105", "Paramount Pictures"),
                           ("s106", "Twentieth Century Fox"),
                           ("s107", "Merchant Ivory Production");

INSERT INTO DVDs 
(DVDName, NumDisks, YearRlsd, StudID)
VALUES 
     ("Christmas", 1, 2000, "s105"),
     ("What",      1, 2001, "s103"),
     ("Out",       1, 2000, "s101"),
     ("Falcon",    1, 2000, "s103"),
     ("Amadeus",   1, 1997, "s103"),
     ("Show",      2, 2000, "s106"),
     ("View",      1, 2000, "s107"),
     ("Mash",      2, 2001, "s106");

select * from DVDs;
DELETE FROM DVDs
USING DVDs, Studios
WHERE DVDs.StudID=Studios.StudID
   AND Studios.StudDescrip="Universal Studios";

select * from DVDs;



Using the "using join delete" Alternative to Delete Data

Drop table DVDs;
Drop table Studios;

CREATE TABLE DVDs
(
   DVDID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   DVDName VARCHAR(60) NOT NULL,
   NumDisks TINYINT NOT NULL DEFAULT 1,
   YearRlsd YEAR NOT NULL,
   StudID VARCHAR(4) NOT NULL
) ENGINE=INNODB;

CREATE TABLE Studios
(
   StudID VARCHAR(4) NOT NULL,
   StudDescrip VARCHAR(40) NOT NULL,
   PRIMARY KEY (StudID)
)
ENGINE=INNODB;
INSERT INTO Studios VALUES ("s101", "Universal Studios"),
                           ("s102", "Warner Brothers"),
                           ("s103", "Time Warner"),
                           ("s104", "Columbia Pictures"),
                           ("s105", "Paramount Pictures"),
                           ("s106", "Twentieth Century Fox"),
                           ("s107", "Merchant Ivory Production");

INSERT INTO DVDs 
(DVDName, NumDisks, YearRlsd, StudID)
VALUES 
     ("Christmas", 1, 2000, "s105"),
     ("What",      1, 2001, "s103"),
     ("Out",       1, 2000, "s101"),
     ("Falcon",    1, 2000, "s103"),
     ("Amadeus",   1, 1997, "s103"),
     ("Show",      2, 2000, "s106"),
     ("View",      1, 2000, "s107"),
     ("Mash",      2, 2001, "s106");
select * from DVDs;
DELETE FROM DVDs
USING DVDs, Studios
WHERE DVDs.StudID=Studios.StudID
   AND Studios.StudDescrip="New Line Cinema";

select * from DVDs;