0% found this document useful (0 votes)
13 views20 pages

Cs New sm-103-122

This document provides an overview of database management concepts, focusing on the relational database model and SQL. It covers essential topics such as database terminology, operations, constraints, and types of database management systems (DBMS). Additionally, it includes SQL statements for data definition, manipulation, and transaction control, along with examples of creating and querying tables.

Uploaded by

keerthana
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)
13 views20 pages

Cs New sm-103-122

This document provides an overview of database management concepts, focusing on the relational database model and SQL. It covers essential topics such as database terminology, operations, constraints, and types of database management systems (DBMS). Additionally, it includes SQL statements for data definition, manipulation, and transaction control, along with examples of creating and querying tables.

Uploaded by

keerthana
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/ 20

Unit : 3 Database Management and Mysql

Relational
Database
Model
Introductory
Database
Concepts of
Constraints
Database

Database
Management

Common Database
Database Languages
Operations & SQL

Database
Keys

Structured Query Language (SQL)

SQL

DDL DML TCL Query

Operations in SQL

SELECT ALIAS ORDER BY


MATHS RELATIONAL LOGIC
OPERATION OPS OPS
IN / IS / LIKE /
NOT IN IS NOT NOT LIKE
DISTINCT GROUP BY HAVING
EQUI NATURAL CARTESIAN
JOIN JOIN PRODUCT
Database Management System (DBMS)

Common Terminologies related to database:


 Data -> Raw facts or figures
 Database -> A collection of interrelated data.
 DBMS -> A collection of files and a set of programs allowing users to access/modify
these files are known as Database Management System.
 Data Redundancy - > Duplication of data.
 Data Security -> Protection of data against accidental/intentional disclosure to
unauthorized person or unauthorized modification/destruction.
 Data Privacy -> Right of individual/organization to determine when/how/what information
to be transmitted to others.

Need of using Database:


 Helps to store data in a structured manner
 Query in the Database (i.e. ask questions about the data)
 Sort and Manipulate Data in the Database
 Validate the Data Entered and check for inconsistencies
 Produce Flexible Reports
Advantage of using Database:
 Reduce data redundancy
 Control inconsistency
 Facilitates sharing of data
 Enforce standards
 Ensure data security
 Maintain integrity
Limitations of implementing Database:
 Compromise of Security and Integrity without good control
 Performance overhead
 Extra hardware required sometimes
 Complex system
Types of DBMS:
 Hierarchical DBMS
 Network Based DBMS
 Object Based DBMS
 Relational DBMS

RELATIONAL DATA MODEL


Relational Data Model is defined as a model of defining a database as a collection of
tables/relations i.e. arrangement of values in rows/tuples and columns/fields/attributes.
Common Terminologies related to Relational Data Model:
 Relation: Collection of data organized in rows and columns where each cell has atomic
value. (same as Table)
 Tuple: Row of a table (same as Record)
 Attribute: Column of a table (same as Field)
 Domain: Range of values (data types) allowed for an attribute
 Degree: No. of attributes/columns/fields in a table
 Cardinality: No. of tuples/rows/records in a table
 View: Virtual table (no physical existence) derived from one or more base table for ease of
query only.
 Referential Integrity: Property of database that requires every value of one attribute of a
Relation must be present in another attribute (same datatype) in a different (or the same)
relation.
Example:

In the above table STUDENT, degree = 7 and cardinality = 5.

Database Key
Key in a database is defined as a set of attributes to identify each record uniquely in a table. A Key
must be unique and not null.
Classification of Keys:

Key

Candidate Primary Key Alternate Key Foreign Key


 Key Key
 Candidate Key: Candidate key is defined as a set of minimum no. of attributes to uniquely
identify a record in a table. A table may have multiple candidate keys.
 Primary Key: The one candidate key chosen by Database Administrator for a table to
uniquely identify a record in a table is said to be Primary Key of that table. A table can have
exactly one Primary Key.
 Alternate Key: Candidate key(s) not chosen by Database Administrator in a table is/are
defined as alternate key(s). A table can have 0 or more alternate keys.
 Foreign Key: Foreign Key is a non-key attribute derived from primary key of some other
table. A table can have 0 or more foreign keys.

Data types in SQL


 Numeric data types: Used for representing number in a column
e.g. –
int(m) – Integer of maximum length m i.e. maximum number of digits allowed in m.
float(m,d), decimal (m,d), numeric(m,d) – Real number of maximum length m where
maximum number of digits permissible after decimal point is d and before decimal point is
m-d.

 Date & Time data types: Used to represent date, time, or both in a column. Data is enclosed
with quotes ‘ ’ or “ “.
e.g. - date, datetime, time

 String / Text data types: Used to represent text in a column. Data is enclosed with quotes ‘ ’
or “ “.
e.g. –
char(m) – Fixed length character of length m where 1 character takes 1 Byte in memory.
Memory space is wasted if text size is less than m.
varchar(m) – Variable length character allowing maximum number of characters m. It
saves memory allocation for text having lesser size than m.

blob – Binary Large object for huge size text.

 NULL – NULL is said to be the absence of any value in a column. No arithmetic or


comparison operation can be performed on NULL value.

Classification of Database Language

Data Definition Language (DDL):


Data Definition Language (DDL) defines the different structures in a database like table, view,
index etc.
DDL statements are used to create structure of a table, modify the existing structure of the table
and remove the existing table.
e.g. - CREATE, ALTER, DROP
Syntax of DDL statements:
 CREATE TABLE table_name
(column_name datatype constraint)
 ALTER TABLE table_name
ADD column datatype constraint (if any)
MODIFY column new_datatype new_constraint (if any)
DROP column
 DROP TABLE table_name

Data Manipulation Language (DML):


Data Manipulation Language (DML) statements are used to access and manipulate
data in existing tables.
The manipulation includes inserting data into tables, deleting data from the tables and modifying
the existing data.
e.g. – INSERT, UPDATE, DELETE

Types of DML statements:


 INSERT record
INSERT INTO table_name(columns) VALUES (1 or more comma separated values)
 UPDATE one or more columns in already existing record(s)
UPDATE table_name SET column = value or expression
(comma separated if multiple columns updated) WHERE condition
 DELETE record
DELETE FROM table_name WHERE condition

Transaction Control Language (TCL):


Database ensures that a database transaction i.e. complete set of records involved in a transaction
either fully completed or not taken place at all to maintain data consistency. Transaction Control
Language (TCL) statements allows to save or revert database transactions.
e.g. –
COMMIT – Save the changes permanently in the database
ROLL BACK – Revert back the changes made in database

Query:
Query is a type of SQL commands which accepts tables (relations), columns (fields or attributes)
and conditions or specifications if any and display the output by means of a temporary table which
consists of data represented through fields and records.
Structure of Query:
SELECT < 1, multiple ( comma i.e. , separated) or all columns >
FROM < 1 table or multiple tables ( comma i.e. , separated) in case of join >
WHERE <condition on column(s)>
GROUP BY <1 column>
HAVING < condition on aggregate function on a column only if group by exists >
ORDER BY <0, 1 or more ( comma i.e. , separated) columns >
Note:
I. Among above SELECT and FROM are mandatory statements in a query and all other
statements are optional.
II. SELECT statement contains one or more columns. * should be used to display all columns.
Functions or expressions on columns can also be done.
III. FROM statement contains multiple tables only if columns from more than one tables are
displayed through SELECT statement in case of product or join operations. Here records
can be fetched from multiple tables.
IV. WHERE clause may contain multiple conditions related with logical OR / AND operators.
Logical NOT operator can be used on a condition also.
V. GROUP BY clause is used if statistical records are to be displayed based on a field/column.
In this case SELECT statements should contain GROUP BY field and aggregate function
on another column at least. Once a group is formed individual records cannot be accessed
in the same query.
VI. ORDER BY clause can be used to arrange the output records in ascending (by default) or
descending order of values in one or more columns.
Order of execution of a query
Step 1: Identify table(s) with FROM clause
Step 2: Filter records using WHERE clause
Step 3: Form group if any using GROUP BY clause
Step 4: Filter groups using HAVING clause only if GROUP BY is used
Step 5: Arrange the output records in ascending or descending order using ORDER BY
Step 6: Display the fields mentioned in SELECT clause.

Database Constraints
Rules imposed on the values of one or more columns in the tables are called database constraints.
The database constraints are:
UNIQUE Ensures that all values in a column are different. No two records have
same values in that column.
NOT NULL Ensures that a column can never have NULL values.
PRIMARY KEY Uniquely identify a record. It is a combination of UNIQUE and NOT
NULL.
CHECK Specify the domain of values with certain criteria for a column.
DEFAULT Provides default value for a column when no value is specified.
REFERENCES / Ensures referential integrity between the foreign key of dependent /
FOREIGN KEY referencing table and primary key of independent / referenced table.

SQL STATEMENTS WITH EXAMPLES


Create two tables EMPL and DEPT as follows:
In DEPT table:
1. DEPT_ID is primary key
2. DNAME is not null
3. MAX_STRENGTH should be minimum 1
In EMPL table:
1. EID is primary key
2. ENAME is not null
3. HOMETOWN is ‘BANGALORE’ by default
4. SALARY is between 5000.00 and 300000.00
5. MGR_ID refers to EID of manager
6. DEPT_ID refers to DEPT_ID of table DEPT
A. Write DDL statement to create a database OFFICE and define two tables mentioned as above
under OFFICE database.
Create new database OFFICE in MySQL as following:
CREATE DATABASE OFFICE;
Work inside the database OFFICE as following:
USE OFFICE;
Note: By default, TEST database is used which is in-built database in MySQL. So no need to
create test. Only ‘use test;’ statement can be written to enter test.
DDL statement to create DEPT table is as following:
SOLUTION 1 SOLUTION 2
CREATE TABLE DEPT CREATE TABLE DEPT
( (
DEPT_ID VARCHAR(4) PRIMARY KEY, DEPT_ID VARCHAR(4),
DNAME VARCHAR(15) NOT NULL, DNAME VARCHAR(15) NOT NULL,
DLOC VARCHAR(20), DLOC VARCHAR(20),
MAX_STRENGTH INT(2) CHECK MAX_STRENGTH INT(2),
(MAX_STRENGTH >= 1) PRIMARY KEY(DEPT_ID),
); CHECK (MAX_STRENGTH >= 1)
);
Schema or structure of table DEPT is as follows:

DESC DEPT;

DDL statement to create EMPL table is as following:

CREATE TABLE EMPL


(
EID VARCHAR(6) PRIMARY KEY,
ENAME VARCHAR(30) NOT NULL,
GEN CHAR(1) CHECK (GEN IN ('M', 'F', 'T')),
DOJ DATE,
HOMETOWN VARCHAR(20) DEFAULT 'BANGALORE',
SALARY DECIMAL(8, 2) CHECK (SALARY BETWEEN 5000 AND 300000),
MGR_ID VARCHAR(6) REFERENCES EMPL(EID),
DEPT_ID VARCHAR(4) REFERENCES DEPT(DEPT_ID)
);

or,
CREATE TABLE EMPL
(
EID VARCHAR(6),
ENAME VARCHAR(30) NOT NULL,
GEN CHAR,
DOJ DATE,
HOMETOWN VARCHAR(20) DEFAULT 'BANGALORE',
SALARY DECIMAL(8, 2) ,
MGR_ID VARCHAR(6) ,
DEPT_ID VARCHAR(4) ,
PRIMARY KEY(EID),
CHECK (GEN IN ('M', 'F', 'T')),
CHECK (SALARY BETWEEN 5000.00 AND 300000.00),
FOREIGN KEY(MGR_ID) REFERENCES EMPL(EID),
FOREIGN KEY(DEPT_ID) REFERENCES DEPT(DEPT_ID)
);

Schema or structure of table EMPL is as follows:

DESC EMPL;
Name of tables defined in current database so far.

SHOW TABLES;

B. Write DML statements to insert records in two tables.

DML statements to insert records in DEPT are as follows:

INSERT INTO DEPT VALUES ('D01', 'FINANCE', 'MUMBAI', 20);

INSERT INTO DEPT VALUES ('D02', 'ADMIN', 'KOLKATA', 15);

INSERT INTO DEPT VALUES ('D03', 'IT', 'CHENNAI', 5);

DML statements to insert records in EMPL are as follows:

INSERT INTO EMPL VALUES ('E0001', 'RITU SEN', 'F', '2002-06-20', 'KOLKATA',
40000.00, NULL, 'D03');

INSERT INTO EMPL VALUES ('E0002', 'MALCOM RAY', 'M', '1998-11-12',


'BANGALORE', 50000.00, NULL, 'D02');

INSERT INTO EMPL(EID, ENAME, GEN, DOJ, HOMETOWN, SALARY, DEPT_ID)


VALUES ('E0003', 'SUNDAR P', 'M', '2008-12-09', 'BANGALORE', 40000.00, 'D01');

INSERT INTO EMPL VALUES ('E0004', 'ANISHA RAWAT', 'F', '2019-09-04', 'DELHI',
20000.00, 'E0001', 'D03' );

INSERT INTO EMPL VALUES ('E0005', 'SANA KHAN', 'F', '2017-08-31', 'DELHI',
30000.00, 'E0003', 'D01');

C. Write SQL statements for the following queries and display their outputs.

1. Display all the records from table DEPT.


SELECT * FROM DEPT;

2. Display name and salary of all the employeEs from table EMPL.
SELECT ENAME, SALARY
FROM EMPL;
4. Display DNAME in ascending order of MAX_STRENGTH.

SOLUTION 1 SOLUTION 2 OUTPUT


SELECT DNAME SELECT DNAME
FROM DEPT FROM DEPT
ORDER BY ORDER BY
MAX_STRENGTH; MAX_STRENGTH ASC;

Note:
 Sorting in SQL is by default in ascending order of values be it numeric or alphabetical
order. Hence ASC is default keyword and need not be used in ORDER BY statement.
 In case of arranging the output of query in descending order of values DESC keyword must
be used in ORDER BY statement.

Comparison operators
= > < >= <= <> !=

4. Display name and gender of employees whose hometown is BANGALORE.


SOLUTION OUTPUT
SELECT ENAME, GEN
FROM EMPL
WHERE HOMETOWN =
'BANGALORE';

5. Display the name of departments which are not located in KOLKATA.

SOLUTION 1 SOLUTION 2 OUTPUT


SELECT DNAME SELECT DNAME
FROM DEPT FROM DEPT
WHERE DLOC <> WHERE DLOC !=
KOLKATA'; 'KOLKATA';

6. Display name of employees and salary in descending order of names where DEPT_ID is
not 'D03'.
SOLUTION OUTPUT
SELECT ENAME,
SALARY
FROM EMPL
WHERE DEPT_ID !=
'D03'
ORDER BY ENAME
DESC;

7. Display EID, ENAME of employees whose DOJ is after January, 2015.

SOLUTION OUTPUT
SELECT EID, ENAME
FROM EMPL
WHERE DOJ > '2015-01-31' ;

[Note: DATE should be preferably mentioned in 'yyyy-mm-dd' format.]

Logical Operators
OR AND NOT
Logical operators are used in where clause. AND, OR are binary operations which require 2
conditions. NOT is unary operator which requires one condition only.
 AND : c1 and c2 → If both c1 and c2 are true the overall condition is true.
 OR : c1 or c2 → If at least one between c1 or c2 are true the overall condition is true.
 NOT : not c1 → If c1 is true the overall condition is false and vice versa.

BETWEEN: BETWEEN operator can be used as a substitute of and operation where the minimum
and maximum value is to be checked for a single column.

8. Display the records of those employees whose salary is between 35000 and 45000.
SOLUTION1 SOLUTION2
SELECT * FROM EMPL SELECT * FROM EMPL
WHERE SALARY >=35000 WHERE SALARY BETWEEN
AND SALARY <=45000; 35000 AND 45000;
Checking a list of values

IN: IN operator is a substitute of OR operation(s) among equality checking of a single column with
multiple values.
NOT IN: NOT IN operator is used for non-equality checking of a column with multiple values.

9. Display name and hometown of employees who belong to departments 'D01' or 'D02'.
SOLUTION 1 SOLUTION 2 OUTPUT
SELECT ENAME, SELECT ENAME,
HOMETOWN HOMETOWN
FROM EMPL FROM EMPL WHERE
WHERE DEPT_ID = 'D01' DEPT_ID IN ('D01', 'D02');
OR DEPT_ID = 'D02';

10. Display EID and SALARY of employees whose half of salary is neither 10000 nor 20000.
SOLUTION 1 SOLUTION 2 OUTPUT
SELECT EID, SALARY SELECT EID, SALARY
FROM EMPL FROM EMPL
WHERE NOT (SALARY/2 = WHERE SALARY/2 NOT
10000 OR SALARY/2 = IN (10000, 20000);
20000);

Wildcard Characters

A string constant to be checked with a value stored in a column may have one or more characters
missing in case of sub string checking. Such placeholder can be of two types:
_ → Replacement or placeholder of exactly one character in the string constant value.
(underscore)
% → Replacement or placeholder of 0 or more characters in the string constant value.

LIKE: A string constant containing one or more wildcard characters can be checked for equality
with LIKE operator only, not =.
NOT LIKE: Likewise NOT LIKE operator checks inequality checking with a string constant
containing one or more wildcard characters. It cannot be done using <> or !=.

11. List the name of employees whose name starts with 'S' and have length at least 5.
SOLUTION OUTPUT
SELECT ENAME
FROM EMPL
WHERE ENAME LIKE 'S____%';

[Hints: 4 underscores i.e. _ after S]


12. List the name of employees whose name ends with 'N' or does not contain 'M' in it.

SOLUTION OUTPUT
SELECT ENAME
FROM EMPL
WHERE ENAME LIKE '%N'
AND ENAME NOT LIKE '%M%';

NULL checking
IS: IS is a special operator which is used to check absence of value i.e. NULL in a column as no
other comparison operator can be used on NULL values.
IS NOT: Likewise, IS NOT is used to check the presence of values i.e. NOT NULL in a column.
13. Print ENAME and DEPT_ID of employees who do not have manager i.e. MGR_ID is blank.
SOLUTION OUTPUT
SELECT ENAME, DEPT_ID
FROM EMPL
WHERE MGR_ID IS NULL;

14. Print ENAME and DEPT_ID of employees who have manager i.e. MGR_ID is not empty.
SOLUTION OUTPUT
SELECT ENAME, DEPT_ID
FROM EMPL
WHERE MGR_ID IS NOT NULL;

Display redundant or unique values


ALL: ALL keyword allows all the values occurring including duplicate values to be displayed in
output. SQL allows duplicate values in output. ALL is by default used in SQL, so need not be used
explicitly.
DISTINCT: By default, SQL does not remove any duplicate values in the output on its own.
Hence DISTINCT keyword is used along with a column where redundant values need to be
removed before displayed.
15. List the hometowns of all the employee (Including duplicate values).
SOLUTION 1 SOLUTION 2 OUTPUT
SELECT SELECT ALL
HOMETOWN HOMETOWN
FROM EMPL; FROM EMPL;

16. List the name of places which are hometown of any employee. (No duplicate values)
SOLUTION OUTPUT
SELECT DISTINCT
HOMETOWN
FROM EMPL;

Aggregate functions

SUM( ) AVG( ) MAX( ) MIN( ) COUNT( )

Aggregate or statistical functions can be used on a group of records.


Using GROUP BY clause: Display outputs regarding each group formed by the GROUP BY field.
Without using GROUP BY clause: Display output corresponding to the overall table may or may
not be filtered by where clause.
For example, consider the following ITEM table:

GROUP BY: GROUP BY clause is used if statistical records of a table are to be displayed based
on a field. Once the group is formed individual records cannot be accessed in that query. Several
clusters or groups are formed based on the number of different values in the GROUP BY column
present in the table.
For example, if GROUP BY is applied on TYPE field of ITEM table 3 groups are formed – Crops
have 2 records, Leaves and Pulses have one record each.
Renaming field and table
AS is an optional keyword to rename a column a table in FROM clause or an expression on
column(s) in SELECT clause. If there is blank space in alias then it must be surrounded by ' ' or '' ''.
Column renaming is done for customized display of query output.
Table renaming is done for giving convenient names to the tables in join operations for
ease of access by programmers.
17. Display the number of distinct DLOC mentioned in table DEPT.
SOLUTION OUTPUT
SELECT COUNT(DISTINCT DLOC) as 'NO. OF
LOCATIONS'
FROM DEPT;

18. Display the earliest and latest DOJ of employees as per EMPL.
SOLUTION OUTPUT
SELECT MIN(DOJ) 'EARLIEST', MAX(DOJ)
'LATEST'
FROM EMPL;

19. Display the number of employees of each gender GEN.


SOLUTION OUTPUT
SELECT GEN, COUNT(*) COUNT
FROM EMPL
GROUP BY GEN;

20. Display the total SALARY paid by each department.


SOLUTION OUTPUT
SELECT DEPT_ID, SUM(SALARY) 'TOTAL
SALARY'
FROM EMPL
GROUP BY DEPT_ID;

HAVING: It is a conditional statement used along with group by clause only. It compares the
values with the outcome of aggregate functions belonging to each group already formed by
GROUP BY clause.
Difference between WHERE and HAVING:

21. Display the hometowns and no. of employees belonging to them if the headcount per
hometown is at least 2.
SOLUTION OUTPUT
SELECT HOMETOWN, COUNT(EID) 'NO
OF EMPLOYEE'
FROM EMPL
GROUP BY HOMETOWN
HAVING COUNT(EID) >= 2;

22. Display the number of employees working in each DEPT_ID excepting 'D01' where no. of
employees in the DEPT_ID is more than 1.
SOLUTION OUTPUT
SELECT DEPT_ID, COUNT(*) AS 'NO OF
EMPLOYEE'
FROM EMPL
WHERE DEPT_ID != 'D01'
GROUP BY DEPT_ID
HAVING COUNT(*) > 1;

Cartesian Product
Cartesian product is performed on two tables and it produces all the combination of records in both
tables. It does not require any common column.
If tables A, B have m, n columns and p, q records respectively then resultant table A x B has m+n
columns and p x q records.
23. Perform Cartesian Product between EMPL and DEPT.
SOLUTION 1 SOLUTION 2 SOLUTION 3
SELECT * SELECT * SELECT *
FROM EMPL, DEPT; FROM EMPL INNER JOIN FROM EMPL JOIN
DEPT; DEPT;
[RECOMMENDED
STATEMENT]
JOIN
NATURAL JOIN: Natural join is a binary operator which works on two tables. They should have
one column which have same name and domain. It a combination of Cartesian product and a where
clause with equality checking on the common columns.
 Other conditions in that query are ANDed with the join condition.
 Natural join is mostly done on Foreign key field of one table and Primary key field of
another table.
 If tables A, B have m, n columns and p, q records respectively then resultant table has m+n
columns and minimum(p,q) records.

EQUI JOIN: Equi join is a join operation which works on the equality condition of values in two
columns from two tables having similar data type. NATURAL JOIN, EQUI JOIN are said to be
INNER JOIN.
24. Perform Natural Join between these two tables.
SOLUTION 1 SOLUTION 2
SELECT * SELECT *
FROM EMPL NATURAL JOIN FROM EMPL, DEPT
DEPT; WHERE EMPL.DEPT_ID =
DEPT.DEPT_ID;
[RECOMMENDED STATEMENT]

25. Display every ENAME and their corresponding DNAME.


SOLUTION OUTPUT
SELECT ENAME, DNAME
FROM EMPL, DEPT
WHERE EMPL.DEPT_ID = DEPT.DEPT_ID;

26. List the name of employees who work in ADMIN department.


SOLUTION OUTPUT
SELECT ENAME
FROM EMPL AS E, DEPT AS D
WHERE E.DEPT_ID = D.DEPT_ID
AND DNAME = 'ADMIN';

27. Display no. of employees working in those departments whose DLOC is CHENNAI.
SOLUTION OUTPUT
SELECT COUNT(*) 'NO. OF EMPLOYEES'
FROM EMPL AS E, DEPT AS D
WHERE E.DEPT_ID = D.DEPT_ID
AND DLOC = 'CHENNAI';

28. Display ENAME of employees who have manager along with that display ENAME of their
corresponding manager as well.
SOLUTION OUTPUT
SELECT E.ENAME 'EMPLOYEE', M.ENAME
'MANAGER'
FROM EMPL E, EMPL M
WHERE E.MGR_ID = M.EID;

29. Display ENAME and the amount of bonus to be paid to that employee where bonus = 5000 +
5% of SALARY.
SOLUTION OUTPUT
SELECT ENAME, SALARY,
5000 + 0.05 * SALARY 'BONUS'
FROM EMPL;

D. Write DML statements for the following purpose:


1. Assign DEPT_ID 'D03' to those employees who are presently working at 'D02'.
SOLUTION OUTPUT
UPDATE EMPL
SET DEPT_ID = 'D03'
WHERE DEPT_ID = 'D02';

SELECT * FROM EMPL;

2. Increase SALARY of all the employees by 10%.


SOLUTION OUTPUT
UPDATE EMPL
SET SALARY = 1.1 *
SALARY;

SELECT * FROM EMPL;

3. Delete the department 'D02' from DEPT table.


SOLUTION OUTPUT
DELETE FROM DEPT
WHERE DEPT_ID = 'D02';

SELECT * FROM DEPT;


E. Write DDL statements for the following purpose:
1. Add DPHONE field to table DEPT which should be a number of 10 digits and unique for each
department.
SOLUTION OUTPUT
ALTER TABLE DEPT
ADD DPHONE INT(10) UNIQUE;

DESC DEPT;

2. Drop the column MAX_STRENGTH from DEPT.


SOLUTION OUTPUT
ALTER TABLE DEPT
DROP MAX_STRENGTH;

SELECT * FROM DEPT;

3. Modify the datatype of SALARY in table EMPL to an integer of length 6 and drop the existing
check constraint.
SOLUTION OUTPUT
ALTER TABLE EMPL
MODIFY SALARY INT(6);

DESC EMPL;

Questions ;
Q. No. 1 to 20 are MCQs of 1 mark each

1. An attribute in a table is foreign key if it is the _________key in any other table.


a) Candidate b) Primary c) Unique d) Alternate
2. What is the domain of an attribute?
(a) The set of possible values that the attribute can take
(b) The name of the attribute
(c) The data type of the attribute
(d) None of the above
3. Which of the following is not a database constraint?
a. CHECK b. DEFAULT c. UNIQUE d. NULL
4. The data types CHAR (n) and VARCHAR (n) are used to create _______ and
_______ types of string/text fields respectively in a database.
a) Fixed, equal b) Equal, variable c) Fixed, variable d) Variable, equal
5. Which of the following is a DDL command?
A. UPDATE B. INSERT C. DELETE D. ALTER
6. Which command is used to open the database “SCHOOL”?
a. USE SCHOOL b. OPEN SCHOOL

You might also like