0% found this document useful (0 votes)
8 views

SQL Modified Commands

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Modified Commands

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Data Definition and Manipulation

Commands
1. Write a database description for each of the relations using SQL DDL:

CREATE TABLE Student (


StudentID INT PRIMARY KEY,
StudentName VARCHAR(25)
);

CREATE TABLE Faculty (


FacultyID INT PRIMARY KEY,
FacultyName VARCHAR(25)
);

CREATE TABLE Course (


CourseID CHAR(8) PRIMARY KEY,
CourseName VARCHAR(15) UNIQUE
);

CREATE TABLE Qualified (


FacultyID INT,
CourseID CHAR(8),
DateQualified DATE DEFAULT '2000-01-01' NULL,
PRIMARY KEY (FacultyID, CourseID),
FOREIGN KEY (FacultyID) REFERENCES Faculty(FacultyID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);

CREATE TABLE Section (


SectionNo INT,
Semester VARCHAR(7),
CourseID CHAR(8),
PRIMARY KEY (SectionNo, Semester),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);

CREATE TABLE Registration (


StudentID INT,
SectionNo INT,
Semester VARCHAR(7),
PRIMARY KEY (StudentID, SectionNo, Semester),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (SectionNo, Semester) REFERENCES Section(SectionNo, Semester)
);

3. SQL Data Definition Commands:

a. Add an attribute, Class, to the Student table:

ALTER TABLE Student


ADD Class VARCHAR(20);

b. Remove the Registration table:

DROP TABLE IF EXISTS Registration;

c. Change the FacultyName field from 25 characters to 40 characters:

ALTER TABLE Faculty


MODIFY FacultyName VARCHAR(40);

d. Remove a foreign key from the Section table:

ALTER TABLE Section


DROP CONSTRAINT fk_course_id;

4. SQL Commands for Data Manipulation:

a. Create two different forms of the INSERT command to add a student with
StudentID 65798 and last name Lopez:

INSERT INTO Student (StudentID, StudentName)


VALUES (65798, 'Lopez');

INSERT INTO Student


VALUES (65798, 'Lopez');

b. Insert all data as in the screenshot into each table:

-- Insert into Student table


INSERT INTO Student (StudentID, StudentName) VALUES (38214, 'Letersky');
INSERT INTO Student (StudentID, StudentName) VALUES (54907, 'Altaver');
INSERT INTO Student (StudentID, StudentName) VALUES (66324, 'Aiken');
INSERT INTO Student (StudentID, StudentName) VALUES (70542, 'Marra');

-- Insert into Faculty table


INSERT INTO Faculty (FacultyID, FacultyName) VALUES (2143, 'Birkin');
INSERT INTO Faculty (FacultyID, FacultyName) VALUES (3467, 'Berndt');
INSERT INTO Faculty (FacultyID, FacultyName) VALUES (4756, 'Collins');

-- Insert into Course table


INSERT INTO Course (CourseID, CourseName) VALUES ('ISM 3113', 'Syst Analysis');
INSERT INTO Course (CourseID, CourseName) VALUES ('ISM 3112', 'Syst Design');
INSERT INTO Course (CourseID, CourseName) VALUES ('ISM 4212', 'Database');
INSERT INTO Course (CourseID, CourseName) VALUES ('ISM 4930', 'Networking');

-- Insert into Section table


INSERT INTO Section (SectionNo, Semester, CourseID) VALUES (2712, 'I-2008', 'ISM 3113');
INSERT INTO Section (SectionNo, Semester, CourseID) VALUES (2713, 'I-2008', 'ISM 3112');
INSERT INTO Section (SectionNo, Semester, CourseID) VALUES (2714, 'I-2008', 'ISM 4212');
INSERT INTO Section (SectionNo, Semester, CourseID) VALUES (2715, 'I-2008', 'ISM 4930');

-- Insert into Registration table


INSERT INTO Registration (StudentID, SectionNo, Semester) VALUES (38214, 2714, 'I-
2008');
INSERT INTO Registration (StudentID, SectionNo, Semester) VALUES (54907, 2715, 'I-
2008');
INSERT INTO Registration (StudentID, SectionNo, Semester) VALUES (54907, 2712, 'I-
2008');
INSERT INTO Registration (StudentID, SectionNo, Semester) VALUES (66324, 2713, 'I-
2008');

c. Remove Lopez from the Student table:

DELETE FROM Student


WHERE StudentName = 'Lopez';
d. Modify the name of course ISM 4212 from Database to Introduction to
Relational Databases:

UPDATE Course
SET CourseName = 'Introduction to Relational Databases'
WHERE CourseID = 'ISM 4212';

You might also like