Class 12 Mysql
Class 12 Mysql
TOPIC: MySQL
Teacher: PRASAD N My SQL
PGT COMPTUER SCIENCE
dATABASE cONCEPTS
A Database is a collection of DATA/INFORMATION that is
organized so that it can be easily accessed, managed and
updated. In Database ,Data is organized into rows, columns
and tables, and it is indexed to make it easier to find
relevant information.
When we have more than one key which can identify record uniquely then all the unique keys
Alternate key is the candidate key which is not selected as primary key
KEYS IN DATABASE
Key plays an important role in relational database; it is used for identifying unique
rows from table & establishes relationship among tables on need.
Types of keys in DBMS
Primary Key – A primary is a column or set of columns in a table that uniquely
identifies tuples (rows) in that table.
Candidate Key –It is an attribute or a set of attributes or keys participating for Primary
Key, to uniquely identify each record in that table.
Alternate Key – Out of all candidate keys, only one gets selected as primary key,
remaining keys are known as alternate or secondary keys.
Foreign Key – Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
KEYS IN DATABASE
SQL
SQL is an acronym of Structured Query Language.It is a standard
language developed and used for accessing and modifying relational
databases.
The SQL language was originally developed at the IBM research
laboratory in San José, in connection with a project developing a
prototype for a relational database management system called System R
in the early 70s.
SQL is being used by many database management systems. Some of
them are:
MySQL
PostgreSQL
Oracle
SQLite
Microsoft SQL Server
MYSQL
MySQL is currently the most popular open
source database software. It is a multi-user,
multithreaded database management system.
MySQL is especially popular on the web. It is
one of the parts of the very popular LAMP
platform. Linux, Apache, MySQL and PHP or
WIMP platform Windows,Apache,MySQL and
PHP.
MYSQL
MySQL Features:
➔ Open Source & Free of Cost:
It is Open Source and available at free of cost.
➔ Portability:
Small enough in size to instal and run it on any types of Hardware and
OS like Linux,MS Windows or Mac etc.
➔ Security :
Its Databases are secured & protected with password.
➔ Connectivity
Various APIs are developed to connect it with many programming
languages.
➔ Query Language
It supports SQL (Structured Query Language) for handling
database.
Types of SQL Commands
❑ DDL (Data Definition Language)
To create database and table structure-commands like CREATE , ALTER ,
DROP, TRUNCATE etc.
❑ DML (Data Manipulation Language) Record/rows related
operations.commands like SELECT...., INSERT..., DELETE..., UPDATE.... etc.
❑ Transactional control Language
Used to control the transactions.commands like COMMIT, ROLLBACK,
SAVEPOINT etc.
❑ DCL (Data Control Language)
used to manipulate permissions or access rights to the tables.commands like
GRANT , REVOKE etc.
MySql datatypes
numeric
decimal -decimal(<precision>, [<scale>]) [zerofill] For storing
floating-point numbers where precision is critical.
Int - int(<size>) [auto_increment] [unsigned] [zerofill]
A whole number, 4 bytes, with a maximum range of -2,147,483,648 to
2,147,483,647 (unsigned: 0 to 4,294,967, 295)
string
char-char(<size>) [binary]
Fixed length – for storing strings that won't vary much in size.
Range of 0 to 255, stores that amount in bytes
Varchar-varchar(<size>) [binary]
Variable length – for storing strings that will vary in size.
Range of 0 to 255, stores that amount in bytes, plus 1 byte
date
Date-Format: YYYY-MM-DD ,Example: 2006-09-23,Range of years
1000 to 9999
Database Commands in MySql
Getting listings of database and tables
mysql> SHOW DATABASES;
Creating a database-
command:
SELECT what to select(field name) FROM table(s) WHERE condition that the data
must satisfy;
IN- operator allows us to easily test if the expression in the list of values.
Modify column
● alter table <table_name> modify (<column_name> newdatatype
<new size>)
Ex: alter table student add city varchar(20)
alter table student add (city varchar(20), gender varchar(5))
Drop column:
Syntax: alter table <table_name> drop <column>
Ex:
alter table student drop <column> city;
★ Primary key
★ Unique
★ Not null
★ Default
★ between
Primary key:
➔ It is used to uniquely identifies each record in
a table
➔ Primary key values must not be NULL
UNIQUE:
➔ Information in the column for each row it
must be unique
➔ Unique key constraint can have NULL values
NOT NULL:
BY DEFAULT, A COLUMN CAN HOLD NULL VALUES.
THE NOT NULL CONSTRAINT ENFORCES A COLUMN
TO NOT ACCEPT NULL VALUES.
DEFAULT constraint:
It is used to assign the default value to a column
CHECK constraint:
It ensures that all the values in a column
satisfies that certain condition
Unique Constraint
mysql>
mysql>
Cartesian product:
select * from emp, dept;
Types of Joins:
Natural Join:
join=cross product + condition
DEPARTMENT TABLE:
FIND THE STUDENT NAME WHO WORKED IN A WING HAVING ADDRESS SAME AS
THEIR ADDRESS
select sname from student,wing where student.sno=wing.sno and
student.address=wing.place
1. Name the class in SQL for fetching unique values only
2. What is the use of BETWEEN operator
3. Categorize the DDL and DML commands from the following.
SELECT, UPDATE, DROP, DELETE, CREATE, ALTER, INSERT
4. Use of WHERE clause in SQL
5. FUll form of SQL
6. Give example of any two aggregate functions in MySQL.
7. What is the difference between primary key and unique constraint.
8. Which command is used to display list of all databases.
9. Which command is used to display a list of already existing tables?
10. Which command is used to change the data of the table?
11. Which clause is used with aggregate functions?(Group by/ Where)
12. What do you mean by candidate key?
13. Correct the error in the following query.
Select * from book where bname = %math%;
14. Select count(*) from book; return 10 and select count(bname) from book; return 9. What is the reason
for this?
15. Write a query to display details of all employees whose name(e_name) start with alphabet ‘a’ from table
emp.
16. Duplication of record is called ____________
1. To display CNO, CNAME, TRAVEL DATE from the table TRAVEL in descending
order of CNO.
>>> select CNO, CNAME, TRAVEL DATE from travel order by CNO desc
2. To display the CNAME of all customers from the table TRAVEL who are travelling
by vehicle with code Vo1 or Vo2
>>> select CNAME from travel where VCODE=’V01’ or VCODE=’V02’
3. To display the CNO and CNAME of those customers from the table TRAVEL who
travelled between ‘2015-1231’ and ‘2015-05-01’.
>>> select CNO, CNAME from TRAVEL where TRAVELDATE between ‘2015-1231’ and
‘2015-05-01’
4. To display all the details from table TRAVEL for the customers, who have travel
distance more than 120 KM in ascending order of NOP
>>> select * from TRAVEL where KM>120 order by NOP
5. SELECT COUNT (*), VCODE FROM TRAVEL GROUP BY VCODE HAVING
COUNT (*) > 1;
DISTINCT KEYWORD
SIMPLE CALCULATIONS
IN ,NOT IN COMMAND
LIKE (%, _ )
CONSTRAINTS
SQL NOT NULL CONSTRAINT
◼ The UNIQUE constraint ensures that all values in a column are different.
◼ A PRIMARY KEY constraint automatically has a UNIQUE constraint.
◼ In an RDBMS, whenever two tables are related by a common column, then the related column in the
parent table should be either declared a primary key and the related column in the child table should
have foreign key constrint.
◼ CUSTOMER ORDERS
Column characteristic Column name characteristi
name c
CID Primary key OrderId Primary key
Marks CUSTOMER_CID
Create table orders ( orderId integer, orderdate date, customer_cid integer, primary key(OrderId), foreign
key(customer_cid) references customer(cid)
INSERT AND ALTER TABLE COMMANDS