0% found this document useful (0 votes)
2 views12 pages

Chapter - 5 MySQL SQL Revision Tour

The document provides a comprehensive overview of MySQL and database concepts, including definitions of data, databases, and DBMS. It covers relational data models, SQL elements, types of SQL statements, and various MySQL queries for data manipulation and definition. Additionally, it discusses integrity constraints, foreign key relationships, and examples of SQL commands for creating, modifying, and querying databases.

Uploaded by

surajgaintplay16
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)
2 views12 pages

Chapter - 5 MySQL SQL Revision Tour

The document provides a comprehensive overview of MySQL and database concepts, including definitions of data, databases, and DBMS. It covers relational data models, SQL elements, types of SQL statements, and various MySQL queries for data manipulation and definition. Additionally, it discusses integrity constraints, foreign key relationships, and examples of SQL commands for creating, modifying, and querying databases.

Uploaded by

surajgaintplay16
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/ 12

UNIT-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.

Need / Goal / Application / Advantage of databases:-


(i) It reduces data redundancy (i.e. duplication of data).
(ii) It facilitates sharing of data.
(iii)It reduces data inconsistency (by propagating updates etc).
(iv) It ensures data security.
(v) It enforces data standards.

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.

Relational Model Terminology:-

(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.

(3) Tuple :- Rows of a relation/table is called as the tuple.

(4) Attribute :- Columns of a relation/table is called as the attribute.

(5) Degree :- Number of attributes/columns of a relation/table is called as its degree.

(6) Cardinality :- Number of tuples/rows of a relation/table is called as its cardinality.

(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).

Conditions for REFERENTIAL INTEGRITY:-

 Matching attributes of both tables should have same data type.


 Both the tables should belong to the same data base.

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:- >, <, >=, <=, = !=

Logical operators:- AND, OR, NOT

Comments :- Lines or text not executed by the language translator.

Single line comment :- Beginning the comment with a # symbol.


Single line comment:- Beginning the comment with - - two hyphens followed by a space
Multiple line comment:- Enclosing the comment between /*……………*/

Type of SQL statements :-

(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.

QUERY USING SQL

Table :- Student

ROLLNO NAME CLASS FEES


INT(2) CHAR(15) INT(2) INT(5)

ROLLNO NAME CLASS FEES


1 AMIT KUMAR 10 5000
2 SURESH KUMAR 12 6000
3 MANISH 10 5500
4 TANVEER 12 4500

MYSQL QUERIES
MISCELLANEOUS QUERIES

(1) Displaying list of databases:-

SHOW DATABASES;

(2) Using database:-

USE DATABASEname;

(3) Displaying list of tables:-


SHOW TABLES;

(4) Displaying structure of a table:-

Syntax:- DESCRIBE <TableName>;

Example:- DESCRIBE STUDENT;

(5) Displaying name of current database in use:-

SELECT DATABASE( );

(6) Displaying the status of autocommit:-

SELECT @@AUTOCOMMIT; [ output 1 for enabled and 0 for disabled ]

(7) Setting/updating the status of autocommit :-


3
SET AUTOCOMMIT=1; [ 1 for enabled and 0 for disabled ]

DDL QUERIES

(1) Creating a database :-

Syntax:-

CREATE DATABASE <DataBaseName>;

Example:-

CREATE DATABSE SCHOOL;

(2) Creating a table :-

Syntax:-

CREATE TABLE <TableName>


(ColumnName DataType(Size) [,…]);

Example:-

CREATE TABLE STUDENT


( ROLLNO INT(2), NAME CHAR(15), CLASS INT(2), FEES INT(5));

(3) Destroying a table :-

Syntax:-

DROP TABLE <TableName>;

Example:-

DROP TABLE STUDENT;

(4) ALTER:-

(a) Adding a new column into the existing table:-

Syntax:-

ALTER TABLE <TableName>


ADD (ColumnName DataType(Size) [,…]);

Example:-

ALTER TABLE STUDENT


ADD (PHONE BIGINT(10));

(b) Modifying a existing column of the existing table:-

Syntax:-
4
ALTER TABLE <TableName>
MODIFY ColumnName DataType(Size) [,…];;

Example:-

ALTER TABLE STUDENT


MODIFY PHONE BIGINT(13);

(c) Removing Constraint or Column:-

Syntax 1:-

ALTER TABLE <TableName>


DROP Constraint ;

Example:-

ALTER TABLE STUDENT


DROP PRIMARY KEY;

Syntax 2:-

ALTER TABLE <TableName>


DROP (Constraint ConstraintName) ;

(d) Removing Constraint or Column:-

Syntax 3:-

ALTER TABLE <TableName>


DROP ColumnName ;

Example:-

ALTER TABLE STUDENT


DROP ADDRESS;

(e) Modifying a existing columnname / datatype / size of the existing table:-

Syntax:-

ALTER TABLE <TableName>


CHANGE [COLUMN] OldColumnName NewColumnName NewDataType(NewSize) [,…];;

Example:-

ALTER TABLE STUDENT


CHANGE PHONE MOBILENO BIGINT(13);

(5) TRUNCATE:-
To remove all the rows of a table (Note:- Specific rows cannot be removed) :-

Syntax:-
5
TRUNCATE [TABLE] <TableName>;

Example1:-

TRUNCATE TABLE STUDENT;

Example2:-

TRUNCATE STUDENT;

DML QUERIES

(1) Inserting a row into a table:-

Syntax:-

INSERT INTO <TableName>


VALUES (value1, value2);

Example:-

INSERT INTO STUDENT


VALUES (1,AMIT KUMAR’ ,10,5000);

(2) Inserting data into selected columns a row into a table:-

Syntax 1:-

INSERT INTO <TableName>


VALUES (value, null, null, null);

Example 2:-

INSERT INTO STUDENT


VALUES ( NULL, "AMIT KUMAR", NULL, NULL);

Syntax 3:- For known values provide the value and for
unknown values write NULL
INSERT INTO <TableName>
(ColumnName1[,..])
VALUES (value1, value2);
Example 2:-

INSERT INTO STUDENT


(NAME)
VALUES ("AMIT KUMAR");

(3) Updating/Editing data of a table:-

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;

(4) Deleting a row of a table:-

Syntax:-

DELETE FROM <TableName>


WHERE <Search Condition>;

Example:-

DELETE FROM STUDENT


WHERE ROLLNO=1;

(5) Display rows sorted in ascending order according to a column;-

Syntax:-
SELECT * FROM <TableName>
ORDER BY <ColumnName> [ASC];

Example1:-

SELECT * FROM STUDENT


ORDER BY ROLLNO ASC;

(6) Display rows sorted in descending order according to a column;-

Syntax:-

SELECT * FROM <TableName>


ORDER BY <ColumnName> DESC;

Example1:-

SELECT * FROM STUDENT


ORDER BY ROLLNO DESC;

(7) Removing duplicate value of a columns in display list:-

Example :
7
SELECT DISTINCT (CLASS) FROM STUDENT;

(8) Range Searching


(a) Using Relational & Logical Operator
(b) Using Between/Not Between
(a)
Example:-

SELECT * FROM STUDENT


WHERE FEES>=2000 AND FEES<=3000;

(b)
Example1:-

SELECT * FROM STUDENT


WHERE FEES BETWEEN 2000 AND 3000;

Example2:-

SELECT * FROM STUDENT


WHERE FEES NOT BETWEEN 2000 AND 3000;

(9) List Searching


(a) Using Relational & Logical Operator
(b) Using IN/NOT IN
(a)
Example:-

SELECT * FROM STUDENT


WHERE FEES=2000 OR FEES=3000;

(b)
Example1:-

SELECT * FROM STUDENT


WHERE FEES IN(2000, 3000);

Example2:-

SELECT * FROM STUDENT


WHERE NAME IN(’ AMIT KUMAR’ ,’ SURESH KUMAR’ );

Example3:-

SELECT * FROM STUDENT


WHERE FEES NOT IN(2000, 3000);

(10) Pattern Searching with LIKE

% Matches a complete string


_ Matches a single character of a string

Example1:-

8
SELECT * FROM STUDENT
WHERE NAME LIKE ‘ A%’ ;

Example2:-

SELECT * FROM STUDENT


WHERE NAME LIKE ‘ %A%’ ;

Example3:-

SELECT * FROM STUDENT


WHERE NAME LIKE ‘ _ _ _ _ T%’ ;

(11) Search condition with NULL value:-

Example 1:- Details of students whose fees is NULL

SELECT * FROM STUDENT


WHERE FEES IS NULL;

Example 2:- Details of students who does not pays fees

SELECT * FROM STUDENT


WHERE FEES IS NULL OR FEES=0;

Example 3:- Details of students who pays fees

SELECT * FROM STUDENT


WHERE FEES IS NOT NULL;

(12) Column Alias


Example:-
Query to display name and anuual fees of all students.
SELECT NAME, FEES * 12 [AS] “ ANNUAL FEES” FROM STUDENT;

(13) Putting text in the query output


SELECT NAME,’ GETS SALARY’ ,SAL FROM EMP;

TABLES & INTEGRITY CONSTRAINTS


Constraints:- It is check or rule applied to a column of a table so that the column accepts
value according to that constraint.

(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:-

CREATE TABLE <TableName>


(ColumnName DataType(Size) CONSTRAINT NAME [, …] ) ;

Example:-

CREATE TABLE STUDENT


(ROLL INT(2) PRIMARY KEY,
NAME CHAR(13) NOT NULL,
GAMES CHAR(10) UNIQUE,
FEES INT(5) CHECK (FEES > 2000),
GENDER CHAR(1) DEFAULT ‘F’ );

Table Level Constraints:-

Syntax:-

CREATE TABLE <TableName>


(ColumnName DataType(Size) [, …]
ConstraintName (ColumnName)) ;

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)) ;

REFERENCING FOREIGN TABLE WITH PRIMARY TABLE

Primary Table:- STUDENTP with columns ROLL, NAME


Foreign Table:- STUDENTF with columns ROLL, ADDRESS, FNAME

Creating STUDENTP (Primary Table):-

CREATE TABLE STUDENTP


( ROLL INTEGER(2) PRIMARY KEY,
NAME CHAR(20));

Creating STUDENTF (Foreign Table):-

CREATE TABLE STUDENTF


( ROLL INTEGER(2),
10
ADDRESS CHAR(20),
FNAME CHAR(15),
FOREIGN KEY(ROLL) REFERENCES STUDENTP(ROLL));

VIEWING/DISPLAYING TABLE STRUCTURE WITH CONSTRAINTS:-

SYNTAX:-

SHOW CREATE TABLE <TABLENAME>;

EXAMPLE:-

SHOW CREATE TABLE STUDENTP;

ADDING A PRIMARY KEY CONSTRAINT TO AN EXISTING TABLE:-

SYNTAX:-

ALTER TABLE <PRIMARY TABLENAME>


ADD [CONSTRAINT [CONSTRAINT NAME] PRIMARY KEY <(PRIMARY KEY COLUMNNAME)>;

EXAMPLE:-

ALTER TABLE STUDENTP


ADD CONSTRAINT PK1 PRIMARY KEY (ROLL);

ADDING A FOREIGN KEY CONSTRAINT TO AN EXISTING TABLE:-

SYNTAX:-

ALTER TABLE <FOREIGN TABLENAME>


ADD [CONSTRAINT [CONSTRAINT NAME] FOREIGN KEY <(FOREIGN KEY COLUMNNAME)>
REFERENCES <PRIMARY TABLE NAME> <(PRIMARY KEY COLUMNNAME)>;

EXAMPLE:-

ALTER TABLE STUDENTF


ADD CONSTRAINT FK1 FOREIGN KEY (ROLL) REFERENCES STUDENTP (ROLL);

DELETING PRIMARY KEY CONSTRAINT:-

SYNTAX:-

ALTER TABLE <PRIMARY TABLENAME>


DROP PRIMARY KEY;

EXAMPLE:-

11
ALTER TABLE STUDENTP
DROP PRIMARY KEY;

DELETING FOREIGN KEY CONSTRAINT:-

SYNTAX:-

ALTER TABLE <FOREIGN TABLENAME>


DROP FOREIGN KEY <(CONSTRAINT NAME)>;

EXAMPLE:-

ALTER TABLE STUDENTF


DROP FOREIGN KEY (FK1);

12

You might also like