Download presentation
Presentation is loading. Please wait.
1
Rob Gleasure R.Gleasure@ucc.ie robgleasure.com
IS6126 Databases for Management Information Systems Lecture 3: SQL III – Solutions Rob Gleasure robgleasure.com
2
Exercise Solution We want to create a new table in our database called Inventory with the following criteria Three columns for Part_ID, Part_Name, and Part_of_Product All three of these columns set to not null The Part_ID set as primary key The Part_of_Product set as a foreign key to Products(ProductID) The default Part_Name is ‘Phalange’ CREATE TABLE Inventory ( Part_ID int PRIMARY KEY, Part_name varchar (50) NOT NULL DEFAULT 'Phalange', Part_of_Product varchar(255) NOT NULL , CONSTRAINT fk FOREIGN KEY (Part_of_Product ) REFERENCES Products(ProductID) );
3
Exercise Solution Run the following query to see what the database looks like INSERT INTO Inventory (Part_ID,Part_of_Product) VALUES ('1', '4'); INSERT INTO Inventory (Part_ID,Part_of_Product,Part_Name) VALUES ('3', '5', 'Cogs'); We want to create an index for the Part_Name, so that this can be searched independently? CREATE UNIQUE INDEX PtNm_index ON Inventory(Part_Name); We want to add a column to Inventory called Cost with type varchar(20)? ALTER TABLE Inventory ADD Cost varchar(20); We want to create a view [Phalanges] showing where Part_ID, Part_of_Product, and Cost where the Part_Name is ‘Phalange’? CREATE VIEW [Phalanges] AS SELECT Part_ID, Part_of_Product, Cost FROM Inventory WHERE Part_name='Phalange';
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.