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

ER Model To Table

The document contains SQL commands to create 5 tables - student, course, lecture, hobby and subject. It also inserts sample data into these tables and defines foreign key relationships between the tables. The tables created are: student to store student details, course for course information, lecture for lecturer details, hobby for student hobbies and subject for the subjects associated with courses and lectures. Sample data is inserted and some select queries are run at the end to fetch data from these tables.

Uploaded by

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

ER Model To Table

The document contains SQL commands to create 5 tables - student, course, lecture, hobby and subject. It also inserts sample data into these tables and defines foreign key relationships between the tables. The tables created are: student to store student details, course for course information, lecture for lecturer details, hobby for student hobbies and subject for the subjects associated with courses and lectures. Sample data is inserted and some select queries are run at the end to fetch data from these tables.

Uploaded by

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

-- create a table

CREATE TABLE student (


studentId int NOT NULL,
studnetname varchar(50),
DOB DATE,
DoorNo varchar(10),
Street varchar(50),
City varchar(50),
State varchar(50),
Pin varchar(10),
courseId int,
lectureId int,
PRIMARY KEY(studentId),
FOREIGN KEY(courseId) references course(courseId),
FOREIGN KEY(lectureId) references lecture(lecturerId)

);
CREATE TABLE course (
courseId int NOT NULL,
coursename varchar(50),
PRIMARY KEY(courseId)

);
CREATE TABLE lecture (
lectureId int NOT NULL,
lecturername varchar(50),
lecturerCourseId int,
PRIMARY KEY(lectureId),
FOREIGN KEY(lecturerCourseId) references course(courseId)
);
CREATE TABLE hobby(
studentId int,
hobbyname varchar(50),
FOREIGN KEY(studentId) references student(studentId)
);
CREATE TABLE subject(
subjectId int NOT NULL,
subjectName varchar(50),
CourseId int,
lectureId int,
FOREIGN KEY(CourseId) references course(courseId),
FOREIGN KEY(lectureId) references lecture(lecturerId)
);
-- insert some values
INSERT INTO course(courseId,coursename) VALUES (101,"CSE");
INSERT INTO course(courseId,coursename) VALUES (102,"ECE");
INSERT INTO course(courseId,coursename) VALUES (103,"IT");

INSERT INTO lecture(lectureId,lecturername,lecturerCourseId) VALUES (1,"Arun",101);


INSERT INTO lecture(lectureId,lecturername,lecturerCourseId) VALUES
(2,"Athish",102);
INSERT INTO lecture(lectureId,lecturername,lecturerCourseId) VALUES
(3,"Deeparasan",103);

INSERT INTO
student(studentId,studnetname,DOB,DoorNo,Street,City,State,Pin,courseId,lectureId)
VALUES ("CS101","Pranav","2002-10-11",80,"Main road","Pollachi","Tamil
Nadu","642123",101,1);
INSERT INTO
student(studentId,studnetname,DOB,DoorNo,Street,City,State,Pin,courseId,lectureId)
VALUES ("EC101","Dharshan","2002-05-11",70,"Old Ooty","Ooty","Tamil
Nadu","643001",102,2);
INSERT INTO
student(studentId,studnetname,DOB,DoorNo,Street,City,State,Pin,courseId,lectureId)
VALUES ("IT101","Surya","2002-12-09",83,"Second Street","Tiruppur","Tamil
Nadu","641605",103,3);

INSERT INTO hobby(hobbyname,studentId) VALUES ("Videography","CS101"),


("Playing","EC101"),("Reading Books","IT101");

INSERT INTO subject(subjectId,subjectName,CourseId,lectureId) VALUES (501,


"Computer Networks",101,1);
INSERT INTO subject(subjectId,subjectName,CourseId,lectureId) VALUES (502,
"Microproceesor ",102,2);
INSERT INTO subject(subjectId,subjectName,CourseId,lectureId) VALUES (503,
"Compiler Design",103,3);
-- fetch some values
SELECT * FROM course;
SELECT * FROM lecture;
SELECT * FROM student;
SELECT * FROM hobby;
SELECT * FROM subject;

You might also like