Вы находитесь на странице: 1из 14

What Is the difference between Unique key and primary key.

What is the meaning of Dual Table in RDBMS.


What will be the output of the following MySql Command:-

Write the difference between Round() and truncate function in MySql by taking a query example.
Write the difference between substr() and instr() frunction.
Q1:

(a) Which commands in MySql is used to make any Database current Database and see the
list of table in that data base?

(b) What do you mean by table level constraint and column level constraints? Give example
for that.

(c) How primary key in a table is different from unique key constraint?

(d) What is the purpose of following query statements. Write output.


Select round(4325.7869,2);
Select truncate(4325.7869,2);

(e) What is the difference between Group by and Order by clause?

(f) A table Alpha contains 4 records and table Beta contains 7 records what will be total
number of records in their Cartesian product?

(g) Write SQL statement to add a column regno of data type varchar in a table Candidate
which is primary key?

(h) Mrs Hetal, a database administrator created a table Voters in which she has used
nationality as column name and by mistake she has entered nationality as Gujarat. Now
she wants to change all the values from Gujarati to Indians using single statement. Help
her to do the following by writing statement.

(i) What will be the query output for the following statement. Write in words/sentence.
(a) SELECT * from SCHOOL where sname like “_ _ _ SURAT”;

(b) SELECT * from SCHOOL where sname like “DC%”;

Q2:

(a) What is the difference between drop and delete table command?
(b) What are different commands used in Transaction in MySql?
(c) Consider the tables DOCTORS and PATIENTS given below:
DOCTORS
DOCID DOCNAME DEPARTMENT OPD_DAYS
101 M.Panday ENT TTS
102 G.P.Gupta Paed MWF
201 C.K.Sharma Ortho MWF
PATIENTS
PATNO PATNAME DEPARTMENT DOCID
1 Neeraj ENT 101
2 Mohit Ortho 201
3 Ragni ENT 101
4 Mohit Paed 102
5 Nandani Ortho 201
With reference to these tables, write commands in SQL for(i) and (ii) and output for
(iii) given below:
(i) Display the Patno, PatName and corresponding DocName for each
Patient.
(ii) Display the list of all patients whose OPD_Days are MWF.
(iii) Select OPD_DAYS, count(*) from DOCTORS,PATIENTS
Where Patients.Department=Doctors.Department
Group by OPD_DAYS;
(d) Write SQL statement to create the table Film given below as per specification
Column Name Code Name Price City
Data type Varchar Varchar Number Varchar
Size 6 30 30
Constraint Primary key Default
Delhi

(d) (i) Write SQL statement to add a column Director of data type Varchar(20) in the
above table.
(ii) Write SQL statement to change size of the column City from 40 to 80
(iii) Write SQL statement to display structure of the table.

Q3:

Give the output for the following MySql queries:


a) SELECT ROUND(20009.111,-2);
b) SELECT SQRT(81)+SQRT(49)+SQRT(121);
c) SELECT MID(‘APS Public School’ ,11,8), TRIM(LEADING ‘!’ FROM ‘!!!!!WEL
COME!!!!!’);
d) SELECT SUBSTR( RTRIM(‘INDIA IS GREAT ‘),3,9);
e) SELECT CONCAT(UPPER (‘xiHum’), LOWER(’xiSc’), UPPER(SUBSTR(‘xiCom’,2,3)));

Q4:
a) Which MySql command helps you to see existing databases?
b) Hemant created a table in MySql. Later he found that table is wrongly created and he wants
to remove it. Name the command by which Hemant can do it.
c) Aditi created a table named student, she wants to see those students whose name ending
with p. She wrote a query-
SELECT name.* FROM student WHERE name = ”%p”;
Help her to run the query by removing the errors from the query and rewriting it .
d) Aadhar is not able to set Empid of EMPL table to NULL. Which constraint has he used
while creating table?

Q5:

a) Write a SQL command to create a table LIBRARY with following structure:


Field Type Constraint
Book_no Integer(4) Primary key
Title Varchar(40) Not Null
Author Varchar(30)
Publisher Varchar(30)
Pages Integer(4)
Date_of_pub Date
b) In a database there are two tables ‘BRAND’ and ‘ITEM’ as shown below:
BRAND
ICODE BRAND
101 SONY
202 HP
303 LG
404 IFB
ITEM
CODE INAME PRICE
101 TELEVISION 75000
202 COMPUTER 42000
303 REFRIGERATOR 90000
404 WASHING MACHINE 27000

Write MySql queries for the following:


(i) To display ICode, IName and corresponding Brand of those items, whose price is
between 20000 and 45000(both values inclusive)
(ii) To display ICode, Price and BName of the item which has IName as “Television”
(iii) To increase the Price of all items by Rs. 15%.
c) Given below is a Table: Customer

C_ID Cust_Name City Item_No


01 Dreams Disney New Delhi P002
05 Life time inc. Mumbai P005
12 Happy Travels New Delhi P001
15 Apeksha Pvt. Ltd. Madras P003

(i) Identify Primary key in the table given above.


(ii) Write MySql query to add a column Price with data type Integer and size 6 in the
table Customer.

Chapter 8
What is a database?
A database is an organized collection of raw data that helps in managing it efficiently.

What is a relational database?


A database in which the data is stored in the form of relations (also called tables) is called a Relational
Database. A Relational Database is a collection of one or more tables.

What is a RDBMS?
A database management system is used to manage relational databases is called an RDBMS.

Name some popular DBMS?


MySQL, Oracle, Sybase, Ingress.

Write down the characteristics of a DBMS?


Redundancy can be controlled.
Inconsistency can be controlled.
Data can be shared.
Security restrictions can be applied.

Define MYSQL.
It is an Open source RDBMS software that is available free of cost with a dual licensing system.

Define a relation in context of MySQL?


A relation refers to a two dimensional representation of data arranged in columns(fields or attributes)
and rows(records or tuples).

Define Key?
A column or a combination of columns which can be used to identify one or more rows (tuples) in a
table is called a key of the table.

What is a primary key?


A group of one or more columns that are used to uniquely identify each tuple in a relation.

What is a candidate key?


A column or a group of columns which can be used as the primary key of a relation is called a
candidate key because it is one of the candidates available to be the primary key of the relation.

What is a alternate

Q6. Write the output of the following statements:


a. SELECT TRUNCATE(563.5694,-2);
b. SELECT ROUND(563.5694,-2);
c. SELECT DAYOFYEAR(CURDATE()), DAYOFMONTH(CURDATE()),
DAYNAME(CURDATE());//Assume that query is executed today.
d. SELECT CONCAT("SHEIKH", " HAROON");
e. SELECT POW(2,ROUND(5.46));
Ans.
a. 500
b. 600
c. As per date
d. SHEIKH HAROON
e. 32

Q7. Consider the following table:


Table: Loan Accounts
Acc_no Cust_name Loan_amt Instalments Int_rate Start_date
1 R.K.Gupta 300000 36 12.00 19-07-2009
2 S.P.Sharma 500000 48 10.00 22-03-2008
3 K.P.Jain 300000 36 NULL 08-03-2007
4 M.P. Yadav 800000 60 10.00 6-12-2008
5 S.P. Sinha 200000 36 12.50 03-01-2010
6 P.Sharma 700000 60 12.50 05-06-2008
7 K.S. Dhall 500000 48 NULL 05-03-2008

Write SQL queries for a-d and output for e.


a. Display the details of all the loans with less than 40 instalments.
b. Display the AccNo and Loan_Amount of all the loans started before 01-04-
2009.
c. Display the Int_Rate of all the loans started after 01-04-2009.
d. Display the Cust_Name and Loan_Amount for all the loans for which the
number of instalments are 24, 36, or 48.

Ch-8 : MYSQL Revision Tour

Q1. Define:
a. Database
b. Relational Database
c. RDBMS.
Ans.
a. A database is an organized collection of data.
b. A database in which the data is stored in the form of relations
(also called tables) is called a Relational Database. In other words a Relational
Database is a collection of one or more tables.
c. A DBMS used to manage Relational Databases is called an RDBMS (Relational
Data Base Management System).

Q2. What are the benefits of RDBMS?

Ans.
a. Redundancy can be controlled
b. Inconsistence can be avoided
c. Data can be shared
d. Security restrictions can be applied.

Q3. Define key. Explain the different kinds of keys.

Ans. A key is a column or a combination of columns which can be used to identify


one or more rows in a table.

The different types of keys are:


1. Primary Key: The group of one or more columns used to uniquely identify each
row of a relation is called its Primary Key.

2. Candidate Key: A column or a group of columns which can be used as the


primary key of a relation is called a candidate key because it is one of the
candidates available to be the primary key of the relation.

3. Alternate Key: A candidate key of a table which is not made its primary key is
called its Alternate Key.
Q4. Define SQL. What are the different types of SQL? Explain with examples.

Ans.
SQL is the language used to manipulate and manage databases and tables within
them using an RDBMS.

The different types of SQL are:

1. DDL (Data Definition Language): This is a category of SQL commands. All the
commands which are used to create, destroy, or restructure databases and tables
come under this category. Examples of DDL commands are - CREATE, DROP,
ALTER.

2. DML (Data Manipulation Language): This is a category of SQL commands. All


the commands which are used to manipulate data within tables come under this
category. Examples of DML commands are - INSERT, UPDATE, DELETE.

3. DCL (Data Control Language): This is a category of SQL commands. All the
commands which are used to control the access to databases and tables fall
under this category. Examples of DCL commands are - GRANT, REVOKE.

Q5. Which clause is used for pattern matching in SQL? Give example.

Ans. LIKE clause is used for pattern matching in SQL.

For example, SELECT AccNo, Title, Price from Books WHERE Title LIKE '%History%';