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

sql2

The document details a series of MySQL commands executed to create and manipulate database tables for departments, projects, and employees. It includes creating tables, inserting data, handling foreign key constraints, and performing various SQL queries including joins. Additionally, it demonstrates altering table structures by adding, dropping, and renaming columns, as well as creating and dropping indexes.

Uploaded by

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

sql2

The document details a series of MySQL commands executed to create and manipulate database tables for departments, projects, and employees. It includes creating tables, inserting data, handling foreign key constraints, and performing various SQL queries including joins. Additionally, it demonstrates altering table structures by adding, dropping, and renaming columns, as well as creating and dropping indexes.

Uploaded by

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

Enter password: *****

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 13
Server version: 8.0.40 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use cnc;


Database changed
mysql> create table dept (did int primary key not null auto_increment,dname
varchar(50));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into dept values("IT");


ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into dept (dname) values("IT");
Query OK, 1 row affected (0.01 sec)

mysql> insert into dept (dname) values("Account");


Query OK, 1 row affected (0.01 sec)

mysql> insert into dept (dname) values("Admint");


Query OK, 1 row affected (0.01 sec)

mysql> create table project (pid int primary key not null,pname varchar(50),client
varchar(50));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into project values(101,"Ecom","FK");


Query OK, 1 row affected (0.01 sec)

mysql> insert into project values(102,"Banking","SBI");


Query OK, 1 row affected (0.01 sec)

mysql> insert into project values(103,"Insuarance","HDFC");


Query OK, 1 row affected (0.01 sec)

mysql> create table emp (eid int primary key not null,ename varchar(40),eloc
varchar(50),esal int,did int,pid int,foreign key(did) references dept(did),foreign
key(pid) references project(pid));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into emp values(201,"Ajay","Nerul",554565,1,2);


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`cnc`.`emp`, CONSTRAINT `emp_ibfk_2` FOREIGN KEY (`pid`) REFERENCES
`project` (`pid`))
mysql> select * from dept;
+-----+---------+
| did | dname |
+-----+---------+
| 1 | IT |
| 2 | Account |
| 3 | Admint |
+-----+---------+
3 rows in set (0.00 sec)

mysql> select * from project;


+-----+------------+--------+
| pid | pname | client |
+-----+------------+--------+
| 101 | Ecom | FK |
| 102 | Banking | SBI |
| 103 | Insuarance | HDFC |
+-----+------------+--------+
3 rows in set (0.00 sec)

mysql> insert into emp values(201,"Ajay","Nerul",554565,1,201);


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`cnc`.`emp`, CONSTRAINT `emp_ibfk_2` FOREIGN KEY (`pid`) REFERENCES
`project` (`pid`))
mysql> insert into emp values(201,"Ajay","Nerul",554565,1,102);
Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(202,"Rohan","Vashi",556665,1,103);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(203,"Karan","Panvel",556665,2,103);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(203,"Neha","Panvel",556665,2,103);


ERROR 1062 (23000): Duplicate entry '203' for key 'emp.PRIMARY'
mysql> insert into emp values(204,"Neha","Panvel",556665,5,103);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`cnc`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`did`) REFERENCES `dept`
(`did`))
mysql> insert into emp values(204,"Neha","Panvel",556665,2,103);
Query OK, 1 row affected (0.01 sec)

mysql> select * from emp;


+-----+-------+--------+--------+------+------+
| eid | ename | eloc | esal | did | pid |
+-----+-------+--------+--------+------+------+
| 201 | Ajay | Nerul | 554565 | 1 | 102 |
| 202 | Rohan | Vashi | 556665 | 1 | 103 |
| 203 | Karan | Panvel | 556665 | 2 | 103 |
| 204 | Neha | Panvel | 556665 | 2 | 103 |
+-----+-------+--------+--------+------+------+
4 rows in set (0.00 sec)

mysql> select * from emp where did in(select did from dept where dname="it");
+-----+-------+-------+--------+------+------+
| eid | ename | eloc | esal | did | pid |
+-----+-------+-------+--------+------+------+
| 201 | Ajay | Nerul | 554565 | 1 | 102 |
| 202 | Rohan | Vashi | 556665 | 1 | 103 |
+-----+-------+-------+--------+------+------+
2 rows in set (0.00 sec)

mysql> select dept.did,dept.dname,emp.eid,emp.ename,emp.did from dept inner join


emp on dept.did=emp.did;
+-----+---------+-----+-------+------+
| did | dname | eid | ename | did |
+-----+---------+-----+-------+------+
| 1 | IT | 201 | Ajay | 1 |
| 1 | IT | 202 | Rohan | 1 |
| 2 | Account | 203 | Karan | 2 |
| 2 | Account | 204 | Neha | 2 |
+-----+---------+-----+-------+------+
4 rows in set (0.00 sec)

mysql> select dept.did,dept.dname,emp.eid,emp.ename,emp.did from dept left outer


join emp on dept.did=emp.did;
+-----+---------+------+-------+------+
| did | dname | eid | ename | did |
+-----+---------+------+-------+------+
| 1 | IT | 201 | Ajay | 1 |
| 1 | IT | 202 | Rohan | 1 |
| 2 | Account | 203 | Karan | 2 |
| 2 | Account | 204 | Neha | 2 |
| 3 | Admint | NULL | NULL | NULL |
+-----+---------+------+-------+------+
5 rows in set (0.00 sec)

mysql> select dept.did,dept.dname,emp.eid,emp.ename,emp.did from dept right outer


join emp on dept.did=emp.did;
+------+---------+-----+-------+------+
| did | dname | eid | ename | did |
+------+---------+-----+-------+------+
| 1 | IT | 201 | Ajay | 1 |
| 1 | IT | 202 | Rohan | 1 |
| 2 | Account | 203 | Karan | 2 |
| 2 | Account | 204 | Neha | 2 |
+------+---------+-----+-------+------+
4 rows in set (0.01 sec)

mysql> select dept.did,dept.dname,emp.eid,emp.ename,emp.did from dept inner join


emp on dept.did=emp.did where dept.did=1;
+-----+-------+-----+-------+------+
| did | dname | eid | ename | did |
+-----+-------+-----+-------+------+
| 1 | IT | 201 | Ajay | 1 |
| 1 | IT | 202 | Rohan | 1 |
+-----+-------+-----+-------+------+
2 rows in set (0.00 sec)

mysql> select * from employee;


+------+--------+-------+-------+
| eid | ename | eloc | esal |
+------+--------+-------+-------+
| 1 | Ajay | Nerul | 67000 |
| 2 | Roahn | Nerul | 47000 |
| 3 | Vishal | Vashi | 89000 |
| 4 | Pankaj | Vashi | 49000 |
+------+--------+-------+-------+
4 rows in set (0.00 sec)

mysql> alter table employee add column mobile varchar(12);


Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from employee;


+------+--------+-------+-------+--------+
| eid | ename | eloc | esal | mobile |
+------+--------+-------+-------+--------+
| 1 | Ajay | Nerul | 67000 | NULL |
| 2 | Roahn | Nerul | 47000 | NULL |
| 3 | Vishal | Vashi | 89000 | NULL |
| 4 | Pankaj | Vashi | 49000 | NULL |
+------+--------+-------+-------+--------+
4 rows in set (0.00 sec)

mysql> alter table employee drop column mobile;


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from employee;


+------+--------+-------+-------+
| eid | ename | eloc | esal |
+------+--------+-------+-------+
| 1 | Ajay | Nerul | 67000 |
| 2 | Roahn | Nerul | 47000 |
| 3 | Vishal | Vashi | 89000 |
| 4 | Pankaj | Vashi | 49000 |
+------+--------+-------+-------+
4 rows in set (0.00 sec)

mysql> alter table employee rename column esal to sal;


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from employee;


+------+--------+-------+-------+
| eid | ename | eloc | sal |
+------+--------+-------+-------+
| 1 | Ajay | Nerul | 67000 |
| 2 | Roahn | Nerul | 47000 |
| 3 | Vishal | Vashi | 89000 |
| 4 | Pankaj | Vashi | 49000 |
+------+--------+-------+-------+
4 rows in set (0.00 sec)

mysql> select * from employee where eid=2;


+------+-------+-------+-------+
| eid | ename | eloc | sal |
+------+-------+-------+-------+
| 2 | Roahn | Nerul | 47000 |
+------+-------+-------+-------+
1 row in set (0.00 sec)

mysql> create index in_index on employee(eid);


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from employee where eid=2;


+------+-------+-------+-------+
| eid | ename | eloc | sal |
+------+-------+-------+-------+
| 2 | Roahn | Nerul | 47000 |
+------+-------+-------+-------+
1 row in set (0.00 sec)
mysql> alter table employee drop index id_index;
ERROR 1091 (42000): Can't DROP 'id_index'; check that column/key exists
mysql> alter table employee drop index in_index;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

You might also like