140 likes | 164 Views
CREATE TABLE ARTIST ( ArtistID int NOT NULL IDENTITY (1,1), Name char(25) NOT NULL, TEXT ERROR Nationality char (30) NULL, Birthdate numeric (4,0) NULL, DeceasedDate numeric (4,0)NULL , CONSTRAINT ArtistPK PRIMARY KEY (ArtistID)
E N D
CREATE TABLE ARTIST • ( • ArtistID int NOT NULL IDENTITY (1,1), • Name char(25) NOT NULL, TEXT ERROR • Nationality char (30) NULL, • Birthdate numeric (4,0) NULL, • DeceasedDate numeric (4,0)NULL , • CONSTRAINT ArtistPK PRIMARY KEY (ArtistID) • CONSTRAINT ArtistAK UNIQUE (Name) • );
CREATE TABLE ARTIST ( ArtistID int NOT NULL IDENTITY (1,1), Name char(25) NOT NULL, Nationality char (30) NULL, Birthdate numeric (4,0) NULL, DeceasedDate numeric (4,0) NULL CONSTRAINT ArtistPK PRIMARY KEY (ArtistID) CONSTRAINT ArtistAK UNIQUE (Name) ); Also erroneous – see following slide
Altered CREATE statement • CREATE TABLE ARTIST • ( • ArtistID int NOT NULL IDENTITY (1,1) • CONSTRAINT ArtistPK PRIMARY KEY (ArtistID), • Name char(25) NOT NULL • CONSTRAINT ArtistAK UNIQUE (Name), • Nationality char (30) NULL, • Birthdate numeric (4,0) NULL, • DeceasedDate numeric (4,0)NULL • );
CREATE TABLE example CREATE TABLE MYARTIST ( ArtistID int NOT NULL IDENTITY (1,1) CONSTRAINT ArtistPK PRIMARY KEY (ArtistID), Aname char(25) NOT NULL CONSTRAINT ArtistAK UNIQUE (Aname), Birthdate numeric(4,0) NULL, DeceasedDate numeric (4,0) NULL );
Created to show effect of NO ACTION CREATE TABLE WORK ( WorkID int NOT NULL IDENTITY (500,1), Title char(25) NOT NULL, Copy char(8) NOT NULL, Description varchar (1000) NULL, ArtistID int NOT NULL CONSTRAINT WorkPK PRIMARY KEY(WorkID), CONSTRAINT WorkAK1 UNIQUE (Title, Copy), CONSTRAINT ArtistFK FOREIGN KEY (ArtistID) REFERENCES ARTIST (ArtistID) ON DELETE NO ACTION ON UPDATE NO ACTION We were NOT able to delete a row in ARTIST whose Artist ID was referenced by WORK
Created to show effect of CASCADE CREATE TABLE SOMEWORK ( WorkID int NOT NULL IDENTITY (500,1), Title char(25) NOT NULL, Copy char(8) NOT NULL, Description varchar (1000) NULL, ArtistID int NOT NULL CONSTRAINT WorkPK3 PRIMARY KEY(WorkID), CONSTRAINT WorkAK3 UNIQUE (Title, Copy), CONSTRAINT ArtistFK3 FOREIGN KEY (ArtistID) REFERENCES MYARTIST (ArtistID) ON DELETE CASCADE ON UPDATE CASCADE ); We were able to delete a row in MYARTIST whose Artist ID was referenced by SOMEWORK and the referencing rows in SOMEWORK were also deleted.
ALTER dropped column with no data in it • ALTER TABLE MYARTIST DROP COLUMN DeceasedDate;
ALTER dropped column with data in it • ALTER TABLE ARTIST DROP COLUMN DeceasedDate;