0% found this document useful (0 votes)
27 views8 pages

CMJD Class Note 01 - Database Management Systems

The document discusses different types of database relationships like one-to-many, one-to-one and many-to-many relationships. It shows how to implement these relationships through examples using tables like student, notebook, laptop, subject etc. and provides the SQL queries to create, insert and retrieve data from these tables.

Uploaded by

MUSIC & TECH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views8 pages

CMJD Class Note 01 - Database Management Systems

The document discusses different types of database relationships like one-to-many, one-to-one and many-to-many relationships. It shows how to implement these relationships through examples using tables like student, notebook, laptop, subject etc. and provides the SQL queries to create, insert and retrieve data from these tables.

Uploaded by

MUSIC & TECH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CMJD | Comprehensive Master Java Developer

Database Management Systems - DBMS

Relationship Implementation

SHOW DATABASES;
CREATE DATABASE relationships;
USE relationships;

One to Many

Step 01

CREATE TABLE student(


id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;
CREATE TABLE notebook(
nbId VARCHAR(5),
subject VARCHAR(50) NOT NULL,
PRIMARY KEY(nbId)
);

DESC notebook;

Page 1 of 8
CMJD | Comprehensive Master Java Developer

Step 02

DROP TABLE student;


CREATE TABLE student(
id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;

DROP TABLE notebook;


CREATE TABLE notebook(
nbId VARCHAR(5),
subject VARCHAR(50) NOT NULL,
studentId INT(5),
PRIMARY KEY(nbId),
FOREIGN KEY (studentId) REFERENCES student(id)
);

DESC notebook;

INSERT INTO student VALUES


(1,"Kamal","Galle","0714515122"),
(2,"Nimal","Galle","0725515122"),
(3,"Amal","Galle","0780015122");

SELECT * FROM student;

INSERT INTO notebook VALUES


("n1","Maths",2),
("n2","Science",1),
("n3","Maths",1),
("n4","History",1),
("n5","Music",2);

SELECT * FROM notebook;

INSERT INTO notebook VALUES


("n6","Art",4);// error

INSERT INTO notebook VALUES


("n6","Art",NULL);// OK

Page 2 of 8
CMJD | Comprehensive Master Java Developer

Step 3 with NOT NULL FOREIGN KEY

DROP TABLE student;


CREATE TABLE student(
id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;

DROP TABLE notebook;


CREATE TABLE notebook(
nbId VARCHAR(5),
subject VARCHAR(50) NOT NULL,
studentId INT(5) NOT NULL,
PRIMARY KEY(nbId),
FOREIGN KEY (studentId) REFERENCES student(id)
);

DESC notebook;

INSERT INTO student VALUES


(1,"Kamal","Galle","0714515122"),
(2,"Nimal","Galle","0725515122"),
(3,"Amal","Galle","0780015122");

SELECT * FROM student;

INSERT INTO notebook VALUES


("n1","Maths",2),
("n2","Science",1),
("n3","Maths",1),
("n4","History",1),
("n5","Music",2);

SELECT * FROM notebook;

INSERT INTO notebook VALUES


("n6","Art",4);// error

INSERT INTO notebook VALUES


("n6","Art",NULL);// error

Page 3 of 8
CMJD | Comprehensive Master Java Developer

One to One
Step 01
From One to Many

DROP TABLE student;


CREATE TABLE student(
id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;

DROP TABLE laptop;


CREATE TABLE laptop(
lapId VARCHAR(5),
brand VARCHAR(50) NOT NULL,
price DECIMAL(7,2) NOT NULL,
ram INT NOT NULL,
studentId INT(5) NOT NULL,
PRIMARY KEY(lapId),
FOREIGN KEY (studentId) REFERENCES student(id)
);

DESC laptop;

INSERT INTO student VALUES


(1,"Kamal","Galle","0714515122"),
(2,"Nimal","Galle","0725515122"),
(3,"Amal","Galle","0780015122");

SELECT * FROM student;

INSERT INTO laptop VALUES


("l1","Asus",999.00,8,1),
("l2","Acer",950.00,8,2),
("l3","Dell",900.00,4,1);

SELECT * FROM laptop;

INSERT INTO laptop VALUES


("l4","Asus",650.00,3,6);// error

INSERT INTO laptop VALUES


("l4","Asus",650.00,3,NULL);// error

Page 4 of 8
CMJD | Comprehensive Master Java Developer

Step 02

DROP TABLE student;


CREATE TABLE student(
id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;

DROP TABLE laptop;


CREATE TABLE laptop(
lapId VARCHAR(5),
brand VARCHAR(50) NOT NULL,
price DECIMAL(7,2) NOT NULL,
ram INT NOT NULL,
studentId INT(5) UNIQUE NOT NULL,
PRIMARY KEY(lapId),
FOREIGN KEY (studentId) REFERENCES student(id)
);

DESC laptop;

INSERT INTO student VALUES


(1,"Kamal","Galle","0714515122"),
(2,"Nimal","Galle","0725515122"),
(3,"Amal","Galle","0780015122");

SELECT * FROM student;

INSERT INTO laptop VALUES


("l1","Asus",999.00,8,1),
("l2","Acer",950.00,8,2);

SELECT * FROM laptop;

INSERT INTO laptop VALUES


("l4","Asus",650.00,3,1);// error
//(Duplicate entry '1' for key 'laptop.studentId')

INSERT INTO laptop VALUES


("l4","Asus",650.00,3,6);// error

INSERT INTO laptop VALUES


("l4","Asus",650.00,3,NULL);// error

Page 5 of 8
CMJD | Comprehensive Master Java Developer

Many to Many
Step 01

DROP TABLE student;


CREATE TABLE student(
id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;

DROP TABLE subject;


CREATE TABLE subject(
subId VARCHAR(5),
sub_name VARCHAR(20) NOT NULL,
medium VARCHAR(20) NOT NULL,
hours INT NOT NULL,
PRIMARY KEY(subId)
);

DESC subject;

CREATE TABLE student_subject(


studentId INT(5),
subjectId VARCHAR(5),
FOREIGN KEY (studentId)
REFERENCES student(id),
FOREIGN KEY (subjectId)
REFERENCES subject(subId)
);

DESC student_subject;

INSERT INTO student VALUES


(1,"Kamal","Galle","0714515122"),
(2,"Nimal","Galle","0725515122"),
(3,"Amal","Galle","0780015122");

SELECT * FROM student;

INSERT INTO subject VALUES


('s1',"Maths","English",16),
('s2',"Maths","Sinhala",16),
('s3',"Science","English",18);

SELECT * FROM subject;

Page 6 of 8
CMJD | Comprehensive Master Java Developer

INSERT INTO student_subject VALUES


(1,'s1'),
(1,'s3'),
(2,'s2'),
(2,'s3');

SELECT * FROM student_subject;

INSERT INTO student_subject VALUES


(4,'s1');// error

INSERT INTO student_subject VALUES


(3,'s4');// error

**
INSERT INTO student_subject VALUES
(1,'s1');// should be avoid

INSERT INTO student_subject VALUES


(2,'s3');// should be avoid
**

Step 02

DROP TABLE student;


CREATE TABLE student(
id INT(5),
name VARCHAR(50) NOT NULL,
address VARCHAR(250) NOT NULL,
contact VARCHAR(12) UNIQUE,
PRIMARY KEY(id)
);

DESC student;

DROP TABLE subject;


CREATE TABLE subject(
subId VARCHAR(5),
sub_name VARCHAR(20) NOT NULL,
medium VARCHAR(20) NOT NULL,
hours INT NOT NULL,
PRIMARY KEY(subId)
);

DESC subject;

DROP TABLE student_subject;


CREATE TABLE student_subject(
studentId INT(5),
subjectId VARCHAR(5),

Page 7 of 8
CMJD | Comprehensive Master Java Developer

PRIMARY KEY(studentId,subjectId),
FOREIGN KEY (studentId)
REFERENCES student(id),
FOREIGN KEY (subjectId)
REFERENCES subject(subId)
);
**
//PRIMARY KEY(studentId,subjectId),
// Composite primary key
**
DESC student_subject;

INSERT INTO student VALUES


(1,"Kamal","Galle","0714515122"),
(2,"Nimal","Galle","0725515122"),
(3,"Amal","Galle","0780015122");

SELECT * FROM student;

INSERT INTO subject VALUES


('s1',"Maths","English",16),
('s2',"Maths","Sinhala",16),
('s3',"Science","English",18);

SELECT * FROM subject;

INSERT INTO student_subject VALUES


(1,'s1'),
(1,'s3'),
(2,'s2'),
(2,'s3');

SELECT * FROM student_subject;

INSERT INTO student_subject VALUES


(4,'s1');// error
INSERT INTO student_subject VALUES
(3,'s4');// error
INSERT INTO student_subject VALUES
(1,'s1');// error
INSERT INTO student_subject VALUES
(2,'s3');// error

Page 8 of 8

You might also like