You are on page 1of 5

1.

Creating and using a Database in mysql



CREATE DATABASE assignment1;
SHOW DATABASES;
USE assignment1;

2. Creating tables in database assignment1



CREATE TABLE Department(DNO INT(2) PRIMARY KEY,
DNAME VARCHAR(10) NOTNULL);

CREATE TABLE Employee(ENO INT(2) PRIMARY KEY,


ENAME VARCHAR(10) NOT NULL, DNO INT(2), FOREIGN
KEY REFERENCES Department(DNO), SALARY INT(6));
3. Insert Datas in tables

INSERT INTO Department(DNO,DNAME) VALUES
(10,"Admin");
INSERT INTO Department(DNO,DNAME) VALUES
(20,"Accounts");
INSERT INTO Department(DNO,DNAME) VALUES
(30,"Sales");
INSERT INTO Department(DNO,DNAME) VALUES
(40,"Marketing");
INSERT INTO Department(DNO,DNAME) VALUES
(50,"Purchasing");
INSERT INTO Employee(ENO,ENAME,DNO,SALARY)
VALUES (1,"Amal",10,30000);
INSERT INTO Employee(ENO,ENAME,DNO,SALARY)
VALUES (2,"Shyamal",30,50000);
INSERT INTO Employee(ENO,ENAME,DNO,SALARY)
VALUES (3,"kamal",40,10000);
INSERT INTO Employee(ENO,ENAME,DNO,SALARY)
VALUES (4,"nirmal",50,60000);
INSERT INTO Employee(ENO,ENAME,DNO,SALARY)
VALUES (5,"Bimal",20,40000);
INSERT INTO Employee(ENO,ENAME,DNO,SALARY)
VALUES (6,"Parimal",20,20000);
4. Display all data from the Department table.

SELECT * FROM Department;
5. Display all data from the Employee table.

SELECT * FROM Employee;

6. Insert [1,akash,60,70000] value in Employee table(copy error


message).

UPDATE Employee SET ENO=1,
ENAME="akash",DNO=60,SALARY=70000;

ERROR 1452 (23000): Cannot add or update a child row: a


foreign key constraint fails (`assignment1`.`employee`,
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`DNO`)
REFERENCES `department` (`DNO`))

7. Insert [7,akash,60,70000] value in Employee table(copy error


message).

UPDATE Employee SET
ENO=7,ENAME="akash",DNO=60,SALARY=70000;
ERROR 1452 (23000): Cannot add or update a child row: a foreign
key constraint fails (`assignment1`.`employee`, CONSTRAINT
`employee_ibfk_1` FOREIGN KEY (`DNO`) REFERENCES
`department` (`DNO`))

8. Display name and salary of all employees whose department no is


10.

SELECT ENAME,SALARY FROM Employee WHERE


DNO=10;

9. Display the name and salary of all employees who are working in the
Accounts department.

SELECT ENAME,SALARY FROM EMPLOYEE,DEPARTMENT


WHERE EMPLOYEE.DNO=DEPARTMENT.DNO AND
DNAME='Accounts';

You might also like