0% found this document useful (0 votes)
8 views3 pages

Composite PK

The document outlines the creation of three MySQL tables: 'student', 'course', and 'enroll', along with their respective fields and constraints. It includes various insert operations demonstrating successful entries and handling of duplicate entries and foreign key constraints. The final queries display the contents of the 'student', 'course', and 'enroll' tables, confirming the successful data insertion.

Uploaded by

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

Composite PK

The document outlines the creation of three MySQL tables: 'student', 'course', and 'enroll', along with their respective fields and constraints. It includes various insert operations demonstrating successful entries and handling of duplicate entries and foreign key constraints. The final queries display the contents of the 'student', 'course', and 'enroll' tables, confirming the successful data insertion.

Uploaded by

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

mysql> create table student(

-> sid int primary key,


-> fname varchar(20) not null,
-> lname varchar(20) not null,
-> dob DATE NOT NULL,
-> g char(1) check (g in('m','M','F','f')),
-> email varchar(20) unique);
Query OK, 0 rows affected (0.08 sec)

mysql> create table course


-> ( cid int primary key,
-> cname varchar(20) NOT NULL,
-> duration int not null,
-> fees float not null check(fees>=0));
Query OK, 0 rows affected (0.04 sec)

mysql> create table enroll


-> (erno int unique auto_increment,
-> sid int references student(sid),
-> cid int references course(cid),
-> enrolldate DATE NOT NULL,
-> primary key(sid,cid));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into student values(1001,'Ajay','Dev',now(),'M','Kajol@devgan.com');


Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> insert into student values(1001,'Ajay','Dev',now(),'M','Kajol@devgan.com');


ERROR 1062 (23000): Duplicate entry '1001' for key 'student.PRIMARY'
mysql> insert into student values(1002,'Ajay','Dev',now(),'M','Kajol@devgan.com');
ERROR 1062 (23000): Duplicate entry 'Kajol@devgan.com' for key 'student.email'
mysql> insert into student
values(1002,'Ajaya','Devi',now(),'F','Kajoli@devgani.com');
Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> select * from student;


+------+-------+-------+------------+------+--------------------+
| sid | fname | lname | dob | g | email |
+------+-------+-------+------------+------+--------------------+
| 1001 | Ajay | Dev | 2024-08-05 | M | Kajol@devgan.com |
| 1002 | Ajaya | Devi | 2024-08-05 | F | Kajoli@devgani.com |
+------+-------+-------+------------+------+--------------------+
2 rows in set (0.00 sec)

mysql> desc course;


+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| cid | int | NO | PRI | NULL | |
| cname | varchar(20) | NO | | NULL | |
| duration | int | NO | | NULL | |
| fees | float | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into course values(10,'C Language',2,2500);


Query OK, 1 row affected (0.02 sec)

mysql> insert into course values(20,'Java',3,3500);


Query OK, 1 row affected (0.02 sec)

mysql> insert into course values(20,'Java',3,3500);


ERROR 1062 (23000): Duplicate entry '20' for key 'course.PRIMARY'
mysql> insert into course values(30,'Python',3,7500);
Query OK, 1 row affected (0.01 sec)

mysql> insert into course values(10,'Python',3,7500);


ERROR 1062 (23000): Duplicate entry '10' for key 'course.PRIMARY'
mysql> select * from course;
+-----+------------+----------+------+
| cid | cname | duration | fees |
+-----+------------+----------+------+
| 10 | C Language | 2 | 2500 |
| 20 | Java | 3 | 3500 |
| 30 | Python | 3 | 7500 |
+-----+------------+----------+------+
3 rows in set (0.00 sec)

mysql> select * from student;


+------+-------+-------+------------+------+--------------------+
| sid | fname | lname | dob | g | email |
+------+-------+-------+------------+------+--------------------+
| 1001 | Ajay | Dev | 2024-08-05 | M | Kajol@devgan.com |
| 1002 | Ajaya | Devi | 2024-08-05 | F | Kajoli@devgani.com |
+------+-------+-------+------------+------+--------------------+
2 rows in set (0.00 sec)

mysql> desc enroll;


+------------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+----------------+
| erno | int | NO | UNI | NULL | auto_increment |
| sid | int | NO | PRI | NULL | |
| cid | int | NO | PRI | NULL | |
| enrolldate | date | NO | | NULL | |
+------------+------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> insert into enroll (sid,cid,enrolldate)values(1001,20,now());


Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> insert into enroll (sid,cid,enrolldate)values(1001,20,now());


ERROR 1062 (23000): Duplicate entry '1001-20' for key 'enroll.PRIMARY'
mysql> insert into enroll (sid,cid,enrolldate)values(1001,30,now());
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> insert into enroll (sid,cid,enrolldate)values(1001,10,now());


Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> insert into enroll (sid,cid,enrolldate)values(1001,10,now());


ERROR 1062 (23000): Duplicate entry '1001-10' for key 'enroll.PRIMARY'
mysql> insert into enroll (sid,cid,enrolldate)values(1001,30,now());
ERROR 1062 (23000): Duplicate entry '1001-30' for key 'enroll.PRIMARY'
mysql> insert into enroll (sid,cid,enrolldate)values(1002,30,now());
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into enroll (sid,cid,enrolldate)values(1002,30,now());


ERROR 1062 (23000): Duplicate entry '1002-30' for key 'enroll.PRIMARY'
mysql> insert into enroll (sid,cid,enrolldate)values(1002,10,now());
Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> insert into enroll (sid,cid,enrolldate)values(100,10,now());


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`students`.`enroll`, CONSTRAINT `enroll_ibfk_1` FOREIGN KEY (`sid`)
REFERENCES `student` (`sid`))
mysql> select * from enroll;
+------+------+-----+------------+
| erno | sid | cid | enrolldate |
+------+------+-----+------------+
| 4 | 1001 | 10 | 2024-08-05 |
| 1 | 1001 | 20 | 2024-08-05 |
| 3 | 1001 | 30 | 2024-08-05 |
| 9 | 1002 | 10 | 2024-08-05 |
| 7 | 1002 | 30 | 2024-08-05 |
+------+------+-----+------------+
5 rows in set (0.00 sec)

mysql> select * from student;


+------+-------+-------+------------+------+--------------------+
| sid | fname | lname | dob | g | email |
+------+-------+-------+------------+------+--------------------+
| 1001 | Ajay | Dev | 2024-08-05 | M | Kajol@devgan.com |
| 1002 | Ajaya | Devi | 2024-08-05 | F | Kajoli@devgani.com |
+------+-------+-------+------------+------+--------------------+
2 rows in set (0.00 sec)

mysql> select * from course;


+-----+------------+----------+------+
| cid | cname | duration | fees |
+-----+------------+----------+------+
| 10 | C Language | 2 | 2500 |
| 20 | Java | 3 | 3500 |
| 30 | Python | 3 | 7500 |
+-----+------------+----------+------+
3 rows in set (0.00 sec)

mysql>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy