Chapter - 5 MySQL SQL Revision Tour
Chapter - 5 MySQL SQL Revision Tour
DATABASE CONCEPTS
DATA Raw facts and figures are called as data.
DATABASE It is organised collection of interrelated data.
DBMS (Data Base Management System) It is collection of data base and set of
application programs to access those data.
RDBMS --> Relational Data Base Management System
DBMS that follows 12 Codd rules. The DBMS uses more than one tables and tables
are linked using common field. It uses client server architecture. It is multiuser
Examples of RDBMS – ORACLE, MS Access, OpenOffice Base, MySQL,
Microsoft SQL Server, DB2, PostgreSQL, Ingres.
Relational Data Model:- In relational data model, the data is organized into tables (i.e. rows &
columns). These tables are called as relations, rows as tuples and columns as attributes.
(1) Relation :- Tables are called as the relations. In which data are organized in rows &
columns. These rows are called as tuples and columns as attributes.
(2) Domain :- It is set of all possible & permitted values of an attribute / column.
(7) Primary Key :- A set of one or more attribute(s) that uniquely identifies rows / tuples
within the relation/table.
(8) Candidate Key :- All attribute/column combinations inside a relation / table that can
serve as primary key.
(9) Alternate Key :- A candidate key that is not the primary key.
(10) Foreign Key :- A non-key attribute whose values are derived from the
primary key of some other table(i.e. primary table).
1
REFERENTIAL INTEGRITY:-
You can’ t enter values in foreign table if related values does not exist in primary table.
You can’ t delete a tuple from primary table if related record exists in foreign table.
MySQL
MySQL is free and open source RDBMS that uses SQL, developed by MySQL AB now subsidiary
of Sun Microsystems. Logo of MySQL is dolphin, named as “ Sakila” .
It works on client/server architecture.
Client:- It is front end which is responsible for sending users query/request to the server.
Server:- It is back end which is responsible for receiving users query, processing the query and
sending back the result of the query to the client.
SQL Elements :-
Data Types:- These are the means to identify the type of data and associated operations with it.
(a) Numeric :-
int / integer 11 digits
bigint
float 10,2
double 16,4
decimal
(b) Date and time :-
date YYYY-MM-DD
datetime YYYY-MM-DD HH :MM :SS
(c) String/Text :-
char 255characters (fixed length) space padding
varchar 255characters (variable length)
Operators :- These are the words or symbols that trigger an action on some data.
Examples:
Arithmetical operators :- *,/,%,+,
Relational operators:- >, <, >=, <=, = !=
(1) DDL (Data Definition Language) Statements/commands that are used to create, destroy or
change a data base object such as table.
Commands:- CREATE, DROP, ALTER, TRUNCATE.
(2) DML (Data Manipulation Language) Statements/commands that are used to manipulate
(insert, edit, delete) data of a data base such as table.
2
Commands:- INSERT, UPDATE, DELETE and all SELECT queries.
(3) DCL (Data Control Language) Statements/commands that are used to grant or revoke
privileges/permissions on a data base object such as table.
Commands:- GRANT, REVOKE
(4) TCL (Transaction Control Language) Statements/commands that are used to control
transactions.
Commands:- COMMIT, ROLLBACK, SAVE POINT.
Table :- Student
MYSQL QUERIES
MISCELLANEOUS QUERIES
SHOW DATABASES;
USE DATABASEname;
SELECT DATABASE( );
DDL QUERIES
Syntax:-
Example:-
Syntax:-
Example:-
Syntax:-
Example:-
(4) ALTER:-
Syntax:-
Example:-
Syntax:-
4
ALTER TABLE <TableName>
MODIFY ColumnName DataType(Size) [,…];;
Example:-
Syntax 1:-
Example:-
Syntax 2:-
Syntax 3:-
Example:-
Syntax:-
Example:-
(5) TRUNCATE:-
To remove all the rows of a table (Note:- Specific rows cannot be removed) :-
Syntax:-
5
TRUNCATE [TABLE] <TableName>;
Example1:-
Example2:-
TRUNCATE STUDENT;
DML QUERIES
Syntax:-
Example:-
Syntax 1:-
Example 2:-
Syntax 3:- For known values provide the value and for
unknown values write NULL
INSERT INTO <TableName>
(ColumnName1[,..])
VALUES (value1, value2);
Example 2:-
Syntax:-
UPDATE <TableName>
6
SET <ColumnName> = Values [,…]
WHERE <Search Condition>;
Example1:-
UPDATE STUDENT
SET FEES=5650
WHERE ROLLNO=1;
Example2:-
UPDATE STUDENT
SET FEES=5650, NAME=’ ROHIT’
WHERE ROLLNO=1;
Syntax:-
Example:-
Syntax:-
SELECT * FROM <TableName>
ORDER BY <ColumnName> [ASC];
Example1:-
Syntax:-
Example1:-
Example :
7
SELECT DISTINCT (CLASS) FROM STUDENT;
(b)
Example1:-
Example2:-
(b)
Example1:-
Example2:-
Example3:-
Example1:-
8
SELECT * FROM STUDENT
WHERE NAME LIKE ‘ A%’ ;
Example2:-
Example3:-
(i) Unique It ensures that the column accepts unique values i.e. it does not
accepts duplicate values but it can accept NULL values.
(ii) Not Null It ensures that the column does not accepts NULL values.
(iii) Primary Key It ensures that the column accepts unique values i.e. it does not
accepts duplicate values and it cannot accept NULL values. (The column can be
referenced to a foreign key)
(iv) Check It ensures that the column accepts values according to range of
values provided in the check condition.
(v) Default It ensures that if an user does not provides value to the column
it accepts value provided as default.
9
Column Level Constraints:-
Syntax:-
Example:-
Syntax:-
Example:-
CREATE TABLE STUDENT
(ROLL INT(2) ,
NAME CHAR(13) ,
GAMES CHAR(10) ,
FEES INT(5) ,
GENDER CHAR(1) ,
Primary Key (ROLL),
Not Null (NAME),
Unique (GAMES)) ;
SYNTAX:-
EXAMPLE:-
SYNTAX:-
EXAMPLE:-
SYNTAX:-
EXAMPLE:-
SYNTAX:-
EXAMPLE:-
11
ALTER TABLE STUDENTP
DROP PRIMARY KEY;
SYNTAX:-
EXAMPLE:-
12