Database Management and MySQL Part - 1
Database Management and MySQL Part - 1
What is Database?
► A database system is basically a computer based record system
which can be defined as a collection of logically related data
items stored together to be manipulated by multiple applications.
► The intention of a database is that all related data of any
organization will be stored in a central place and all applications
will use the data for different purpose.
► In other words, a database is used by an organization as a method
of storing, managing and retrieving information. Databases are
managed using a database management system (DBMS).
Need of database systems
► 1. Databases reduce the data redundancy:
The database system do not maintain separate copies of the same data,
rather all the data are kept at one place and all application that require
data refer to the centrally maintained database. If any change is to be
made it will be made at just one place and the changed information will
be available to other applications.
Degree
The number of attributes
(columns) in a relation is called
the degree of a relation.
Cardinality
The number of tuples (rows) in a
relation is called the cardinality
of the relation.
Degree – 5 Cardinality - 5
RDBMS Terminology
Keys : Keys refers to fields of a table which are
used to
► To maintain uniqueness of a tuple (row) in a
table.
► To create relationships between two tables.
Answer : d. 7 and 5
Which of the following refers to copies of the same data stored multiple times
a. Data Inconsistency
b. Data integrity
c. Data Redundancy
d. Data Mining
Answer : b. Tuple
Mr X finds data of one of his student stored in two different tables not matching
with each other. Which issue is discussed here
a. Data Redundancy
b. Data Integrity
c. Data reference
d. Data Inconsistency
Answer : c. Relation
Assessment
An advantage of RDBMS is
a. Data is dependent on application
b. It increases data Redundancy
c. Data can be accessed by multiple application
d. Data is Platform Dependent
The set of values from which attribute of a relation can draw their values is known
as :
_____________
Answer : domain
Assessment
For the table trainer
TID ACCT_NO CITY HIREDATE SALARY
101 A001 MUMBAI 1998-10-15 90000
102 S003 DELHI 1994-12-24 80000
103 A004 DELHI 2001-12-18 20000
The attributes which can become primary keys are:
a. TID, City
b. TID, HIREDATE
c. TID, ACCT_NO
d. None of the above
► Unlike CHAR data type, for VARCHAR the length of the field is the
actual length in bytes of the data entered.
All character data items of MySQL should be enclosed within single or
double quotes.
Difference Between Char and Varchar
Data types in MySQL
Numeric data type
Numeric data type represents data which can be used for calculation
➢ Int/ Integer(w):
int/integer data type represents integer data (signed or unsigned) of
width w. By default it is 11 digits
➢ Decimal(m,d)
Decimaldata type represents floating point numbers. m is the length
and the number of decimals is represented by d
Date datatype
Date type Represents all valid dates in the format of YYYY-MM-DD. The
date data items should be enclosed in single or double quotes.
Time datatype
Time - represents time. Format: HH:MM:SS
MySQL commands
MySQL commands are basically the instructions given to SQL
database for to manipulate and manage data stored in
different tables through commands.
each field’s name, its data type and its length within a table
► modifications of table structure like changing the length of a
Use command
Once the database is created, it needs to be opened to work on it. Use
command is used to open the database. Once the database is opened,
any table created, will be stored under it.
Syntax: Use <database name>;
Example: Use Emp;
Category : DML
SQL commands
Create Table Command
After creating the database CREATE TABLE command creates an
empty table i.e a table with no rows. While creating the table the
table name, column name, data type and width of the column are
given.
Syntax:
Create Table <table-name>(<column name1><data type><size>,
<column name2><data type> [(<size>]
………………...);
Example:
Create table employee (empno char(6), name varchar(15),
basic decimal(8,2), dept varchar(10), dob date);
Category : DDL
SQL commands
SHOW TABLES command
This command helps to view names of all the tables contained
in the current database
Syntax : Show Tables;
Category : DML
Viewing Structure of the table
The DESCRIBE statement is used to see the structure of a
table. It displays the Column names, their data types, along
with its constraint like Primary key etc.
Syntax : DESCRIBE <table_name>;
OR DESC <table_name>;
Example : Desc employee;
Category : DML
SQL commands
Adding Records Using Insert Into
Insert into command adds new rows (records) to a table.
Syntax : Insert into <table name> values (<field
name1>, <field name2,....]);
Example : Insert into employee values (‘1’, ‘Raju’,
12000, ‘Acct’,’1979-01-12’);
► While providing data items for char and varchar fields, enclose
If the table has 5 more rows, what will be the degree and Cardinality of Teacher
The user tried to insert another record with values (1,Raji, Maths, 2, 7000). But
MySQL showed error for this. What can be the reason
The user tried to insert another record with values (Null, Raji, Maths,
2, 7000). But MySQL showed error for this. What can be the reason
Answer : IN operator
Answer : NULL
________ key word can be used to search for a column to check whether it is
empty or not.
Answer : IS NULL
Command which will display the names of department existing in Emp table.
The Department names should appear only once
SELECT
Answer : * FROM SELECT
employee WHERE
* FROM Dept WHERE
employee NOT in(“accts”, “FIN”,or“SYS”)
Not (dept=“accts”
dept=“Fin” or Dept =“sys”);
Assessment
Consider a table BUS having fields – bus_no, Area, capacity,
tot_studs, distance, Transporter, charges, dt_of_cont. Give
commands to do the following
Display all the Bus details of those bus where no of students
travelled is more than 100.
Answer : SELECT * FROM Bus WHERE tot_studs>100;
Display Bus no and Area of those bus whose charges is more than
50000.
Answer : SELECT bus_no, area FROM Bus WHERE charges>50000;
Display the details of the Anand travels and Bhalla co. transporters
Answer : SELECT * FROM Bus WHERE transporter = “Anand travels”
OR transporter = “Bhalla co. “;
Assessment
Consider a table BUS having fields – Bus_no, Area, capacity,
tot_studs, distance, Transporter, charges, dt_of_cont. Give
commands to do the following
Display all the records arranged in descending order of Total
Students
Answer : SELECT * FROM Bus ORDER BY tot_studs DESC;
Display the details all bus records which has the word travels in the
transporters field.
Display the details all bus records whose area field not filled by
the user
Answer : SELECT * FROM Bus WHERE area is Null;
Assessment
Consider a table BUS having fields – Bus_no, Area, capacity,
tot_studs, distance, Transporter, charges, dt_of_ct. Give commands
to do the following
Display all Bus_no and Area whose Transporter name is starting with
Delhi and area is North
Answer : SELECT Bus_no, Area FROM Bus WHERE Tranporter like
“Delhi%” and Area=“North”;
Display all records whose charges are less than 10000 and tot_studs
are 50
Answer : SELECT * FROM Bus WHERE charges <10000 and tot_studs
=50;
Display the Bus no and Transporter who have taken contract in the
month of January 2021
Answer : SELECT Bus_no, Transporter FROM Bus WHERE dt_of_ct
BETWEEN “2021-01-01” and “2021-01-31”;
SQL commands
Create Table Command with constraints
► While creating a table constraints can also be imposed on the
columns of a table.
► Constraints in a RDBMS are used to ensure validity of the data.
specified column.
► Primary Key - Sets a column or a group of columns as the
(ii) Display all records from empdets whose salary > 15000 and experience > 5 years.
Select * from empdets where salary>15000 and experience>5;
(iv) Delete the employee details whose experience is less than 3 yr.
(viii) To display empno,emp_name of all employees whose name ends with ‘n’
TABLE :TRAINER
TID TNAME CITY HIREDATE SALARY
101 SUNAINA MUMBAI 1998-10-15 90000
102 ANAMIKA DELHI 1994-12-24 80000
103 DEEPTI CHANDIGARH 2001-12-18 20000
105 RICHA MUMBAI 1996-01-12 95000
Assessment
(i) Display the Trainer Name, City and Salary only for the trainers whose city is Delhi
in descending order of their Hiredate
Select Tname, City, Salary from trainer where city=‘Delhi’ order by Hiredate desc;
(ii) To display trainer name along with 10% of salary which is labelled as revised salary
for all trainers
TID SALARY
101 90000
102 80000
Assessment
Which of the following commands are used to define the structure of the relation?
a. DML
b. DDL
c. Query
d. Referential Integrity
Answer : b. DDL
To remove duplicate rows from the result of a query ,the following clause is used:
a. distinct
b. Unique
c. Primary key
d. Different
Answer : a. distinct
Assessment
Command to display all records whose ename is of 4 characters from table employee?
a. Select * from employee where ename like ‘- - - -’;
b. Select * from employee where ename like ‘%%%%’;
c. Select from employee where ename like ‘- - - -’;
d. Select * from employee where ename like ‘-%’;
Answer : a. Select * from employee where ename like ‘- - - -’;
Command to display all records whose ename is having ‘L’ as any alphabet of the name from
table employee
a. Select * from employee where ename like ‘ %L%’;
b. Select * from employee where ename like ‘ %L’;
c. Select * from employee where ename like ‘ L%’;
Answer : a 5,3
Assessment
For the table trainer
TID CITY HIREDATE SALARY
101 MUMBAI 1998-10-15 90000
102 DELHI 1994-12-24 80000
103 CHANDIGARH 2001-12-18 20000
The attributes which can become primary key are:
a. TID
b. HIREDATE
c. TID,CITY
d. None of the above
Answer : a TID
Assessment
The command to remove a table trainer from the database is
a. DELETE FROM TRAINER;
b. DROP TABLE TRAINER;
c. Remove trainer
d. None of the above
The _________ operators are used to combine more than 1 conditions in Select
command
a. Relational operator
b. Logical operator
c. Mathematical operators
d. None of the above
Answer : b logical operator
Assessment
The columns or fields of tables are generally referred to as __________
a. tuple
b. Attributes
c. Domain
d. None of the above
Answer : b Attributes
The _________ clause with Select is used to sort the rows in a table .
a. Order by
b. Group by
c. Sort by
d. None of these
Answer : a Order by
Assessment
Which command is used to select a database to make it current?
a. Open databasename
b. Use databasename
c. Select databasename
d. None of the above
Answer : b Use databasename
Answer : b where
Assessment
Which of the following commands is used to define the structure of the relation?
a. DML
b. DDL
c. Query
d. Referential Integrity
Answer : b. DDL
To remove duplicate rows from the result of a query ,the following clause is used:
a. distinct
b. unique
Answer : a distinct
Assessment
Command to display all records whose ename is of 4 characters from table employee?
a. Select * from employee where ename like ‘- - - -’;
b. Select * from employee where ename like ‘%%%%’;
c. Select from employee where ename like ‘- - - -’;
d. Select * from employee where ename like ‘-%’;
Answer : a. Select * from employee where ename like ‘- - - -’;
Command to display all records whose ename is having ‘L’ as any alphabet of the name from
table employee
a. Select * from employee where ename like ‘ %L%’;
b. Select * from employee where ename like ‘ %L’;
c. Select * from employee where ename like ‘ L%’;
Answer : a 5,3
Assessment
The command to remove a table trainer from the database is
a. DELETE FROM TRAINER;
b. DROP TABLE TRAINER;
c. None of the above
The _________ operators are used to combine more than 1 conditions in Select command
a. Relational operator
b. Logical operator
c. None of the above
Answer : b Attributes
Answer : a char
Assessment
A ______key is a set of one or more attributes that can uniquely identify the rows within a table
a. Alternate key
b. Primary key
c. Foreign key
d. None of the above
Answer : b Primary key
The _________ clause with Select is used to sort the rows in a table .
a. Order by
b. Group by
c. Sort by
d. None of these
Answer : a Order by
Assessment
Which command is used to select a database to make it current?
a. Open databasename
b. Use databasename
c. Select databasename
d. None of the above
Answer : b Use databasename
Answer : b where
Assessment
Assessment
1. Create the database LOANS.
CREATE DATABASE loans;
2. Make the database LOANS as active.
USE loans;
3. Create the table Loan_Accounts and insert tuples in it.
CREATE TABLE loan_account (accno int(4) PRIMARY KEY, cust_name VARCHAR(20)
NOT NULL, loan_amount DECIMAL (14,2) NOT NULL, instalements int (3), int_rate
DECIMAL(5,2) NOT NULL, start_date DATE, interest DECIMAL(12,2));
INSERT INTO loan_accounts VALUES(1,”R.k.Gupta”,100000,36,12,”2009-07-
19”,null)
4. Display the details of all the loans.
SELECT * FROM loan_Accounts;
5. Display the AccNo, Cust_Name, and Loan_Amount of all the loans.
SELECT AccNo, Cust_Name, Loan_Amount FROM loan_Accounts;
Assessment
6. Display the details of all the loans with less than 40 instalments.
SELECT * FROM loan_Accounts WHERE instalements < 40;
7. Display the AccNo and Loan_Amount of all the loans started before 01- 04-
2009.
SELECT AccNo, Loan_Amount FROM loan_Accounts WHERE Start_date <
“2009-04-01”;
8. Display the Int_Rate of all the loans started after 01-04-2009.
SELECT int_rate FROM loan_Accounts WHERE Start_date > “2009-04-01”;
9. Display the details of all the loans whose rate of interest is NULL.
SELECT * FROM loan_Accounts WHERE interest IS NULL:
10. Display the details of all the loans whose rate of interest is not NULL.
SELECT * FROM loan_Accounts WHERE interest IS NOT NULL:
Assessment
11. Display the amounts of various loans from the table Loan_Accounts. A loan
amount should appear only once.
SELECT DISTINCT loan_amounts FROM loan_Accounts ;
12. Display the number of instalments of various loans from the table
Loan_Accounts. An instalment should appear only once.
SELECT DISTINCT insalement FROM loan_Accounts ;
13. Display the details of all the loans started after 31-12-2008 for which the
number of instalments are more than 36.
SELECT * FROM loan_Accounts WHERE start_date > “2008-12-31” AND instalements >
36;
14. Display the Cust_Name and Loan_Amount for all the loans which do not have
number of instalments is not 36.
SELECT Cust_Name,Loan_Amount FROM loan_Accounts WHERE instalements <> 36;
15. Display the Cust_Name and Loan_Amount for all the loans for which the loan
amount is less than 500000 or int_rate is more than 12.
SELECT Cust_Name,Loan_Amount FROM loan_Accounts WHERE loan_amount < 500000
OR int_rate > 12;
16. Assessment
Display the details of all the loans which started in the year 2009.
SELECT * FROM loan_Accounts WHERE start_date BETWEEN “2009-01-01” AND
“2009-12-31”;
17. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000.
SELECT * FROM loan_Accounts WHERE loan_amount BETWEEN 400000 AND 500000;
18. Display the details of all the loans whose rate of interest is in the range 11% to
12%.
SELECT * FROM loan_Accounts WHERE Int_rate Between 0.11 AAND 0.12;
19. Display the Cust_Name and Loan_Amount for all the loans for which the
number of instalments are 24, 36, or 48.
SELECT Cust_Name,Loan_Amount FROM loan_Accounts WHERE instalements IN
(24,36,48);
20. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with 'Sharma'.
SELECT AccNo, cust_name,Loan_Amount FROM loan_Accounts WHERE cust_name LIKE
“%Sharma”;
Assessment
21. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with 'a'.
SELECT AccNo, cust_name,Loan_Amount FROM loan_Accounts WHERE cust_name LIKE
“%a”;
22. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a'
SELECT AccNo, cust_name,Loan_Amount FROM loan_Accounts WHERE cust_name LIKE
“%a%”;
23. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name does not contain 'P'.
SELECT AccNo, cust_name,Loan_Amount FROM loan_Accounts WHERE cust_name NOT
LIKE “%P%”;
24. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a' as the second last character.
SELECT AccNo, cust_name,Loan_Amount FROM loan_Accounts WHERE cust_name
LIKE “%a_”;
Assessment
25. Display the details of all the loans in the ascending order of their
Loan_Amount.
SELECT * FROM loan_Accounts ORDER BY loam_amount;
26. Display the details of all the loans in the descending order of their
Start_Date.
SELECT * FROM loan_Accounts ORDER BY start_date DESC;
27. Display the details of all the loans in the ascending order of their
Loan_Amount and within Loan_Amount in the descending order of their
Start_Date.
SELECT * FROM loan_Accounts ORDER BY loam_amount, start_date DESC;
28. Put the interest rate 11.50% for all the loans for which interest rate is NULL.
UPDATE loan_accounts SET int_rate=11.5 WHERE int_rate IS NULL;
29. Increase the interest rate by 0.5% for all the loans for which the loan
amount is more than 400000.
UPDATE loan_accounts SET int_rate = int_rate + 0.5 WHERE loan_amount > 400000;
Assessment
30. For each loan replace Interest with (Loan_Amount * Int_Rate*
Instalments) *12/100.
UPDATE loan_accounts SET interest = Loan_Amount * Int_Rate* Instalments) *12/100;
31. Delete the records of all the loans whose start date is before 2007.
DELETE FROM loan_accounts WHERE start_date < “2007-010-01”34.
32. Add another column Category of type CHAR(1) in the Loan table.
ALTER TABLE loan_accounts ADD category CHAR(1);