Sunteți pe pagina 1din 6

DATABASE REDESIGN (DDL) Instructions:

1) Write all SQL statements to make the name change described above. Be sure to show your code for each. (33 Pts) NOTE: When you create the new CLIENT table DO NOT ADD any foreign key constraint at this time. After you add data to the new CLIENT table and before you add the constraint between the CLIENT and INVOICE tables you need to rename the CustomerNumber field in the INVOICE table to ClientID. This code is not covered in the book but you should simply substitute the actual names into the code below. Substitute the actual table for TABLE and the actual field for Field. The first TABLE.field will be the name of the table and old field and the second Field will be the name of the new field. Everything else must stay the same.
sp_RENAME 'TABLE.Field', 'Field', 'COLUMN';

1) Create a new table with the new name.


CREATE TABLE CLIENT( ClientID IDENTITY (1, 1), FirstName LastName Phone Email ); Int Char(25) Char(25) Char(12) Char(100) NOT NULL NOT NULL, NOT NULL, NOT NULL, NULL,

2) Alter any constraints, triggers, or views that may be affected.


ALTER TABLE CUSTOMER DROP CONSTRAINT TABLE INVOICE DROP CONSTRAINT CustomerPK;

ALTER

Invoice_Cust_FK;

ALTER TABLE CLIENT ADD CONSTRAINT ClientPK PRIMARY KEY(ClientID); ALTER TABLE INVOICE WITH NOCHECK ADD CONSTRAINT

Invoice_Client_FK

FOREIGN KEY(CustomerNumber) REFERENCES CLIENT(ClientID) ON UPDATE NO ACTION ON DELETE NO ACTION;

3) Copy all data from the old table to the new table.
SET IDENTITY_INSERT CLIENT ON; INSERT INTO CLIENT (ClientID, FirstName, LastName, Phone, Email) SELECT CustomerID, FirstName, LastName, Phone, Email FROM CUSTOMER;

4) Drop the old table.


DROP TABLE CUSTOMER;

Now, verify that the tables contain the same data BEFORE you delete the WORK table.
SELECT * FROM CUSTOMER; SELECT * FROM CLIENT;

2) Create a new table (PROD) with the following fields: (10 Pts) -ProdID (Make this field an integer, required field and primary key) -ProdName (Allow 20 characters and make it a required field) -UnitPrice (Allow 8 characters and 2 decimals and required)
CREATE TABLE PROD ProdID ProdName UnitPrice ( Int Char(20) Numeric(8,2) NOT NULL, NOT NULL, NOT NULL,

CONSTRAINT );

ProdPK

PRIMARY KEY(ProdID)

3) Insert data in the new PROD table by executing the Prod Table Insert Data file. (5 Pts)

4) Alter the INVOICE_ITEM table by adding a ProdID field as an integer as a null field. (3 Pts)
ALTER TABLE INVOICE_ITEM ADD ProdID Int NULL;

5) Populate the ProdID field in the INVOICE_ITEM table with the following data: (6 Pts) -ProdID 101 for Blouse -ProdID 102 for Dress Shirt -ProdID 103 for Formal Gown -ProdID 104 for Mens Slacks -ProdID 105 for Womens Slacks -ProdID 106 for Mens Suit
UPDATE INVOICE_ITEM SET ProdID = '101' WHERE Item = 'Blouse'; UPDATE INVOICE_ITEM SET ProdID = '102' WHERE Item = 'Dress Shirt'; UPDATE INVOICE_ITEM SET ProdID = '103' WHERE Item = 'Formal Gown'; UPDATE INVOICE_ITEM SET ProdID = '104' WHERE Item = 'Mens Slacks'; UPDATE INVOICE_ITEM SET ProdID = '105' WHERE Item = 'Womens Slacks'; UPDATE INVOICE_ITEM SET ProdID = '106' WHERE Item = 'Mens Suit';

6) Change the ProdID column in the INVOICE_ITEM table from NULL to NOT NULL. (3 Pts)
Alter TABLE INVOICE_ITEM ALTER COLUMN ProdID int NOT NULL;

7) Delete the Item field from the INVOICE_ITEM table. (3 Pts)


ALTER TABLE INVOICE_ITEM DROP COLUMN Item;

8) Create a relationship between the PROD table and INVOICE_ITEM table. (5 Pts)
ALTER TABLE INVOICE_ITEM ADD CONSTRAINT ProdFK FOREIGN KEY (ProdID) REFERENCES PROD (ProdID) ON UPDATE CASCADE ON DELETE NO ACTION;

9) Once all the changes have been made above copy a screen shot of each of the tables in SQL Server below. (5 Pts)

S-ar putea să vă placă și