SQL Project With Codings
SQL Project With Codings
ON
“DATABASE MANAGEMENT SYSTEM”
2 DATABASE 5
3 WHAT IS DBMS? 6
4 CHARACTERISTICS OF DBMS 6
6 DATATYPE 7-8
8 CREATE DATABASE-DDL 12
9 USE DATABASE-DDL 12
10 12
CREATE TABLE-DDL
11 13
ALTER TABLE-DDL
12 13
DROP TABLE-DDL
13 14
DROP COLUMN-DDL
14 14-15
INSERT-DML
15 15-16
UPDATE-DML
16 16
DELETE-DML
17 17
SELECT-DML
18 17-19
DIFFERENCE BETWEEN DESCRIBE AND
SELECT
19 19-21
DIFFERENCE BETWEEN ALTER AND UPDATE
20 21-22
DIFFERENCE BETWEEN DROP AND DELETE
21 23
COUNT
22 SUM 23
23 AVERAGE 24
24 MIN 24
25 MAX 24-25
26 FIRST 25-26
27 LAST 26-27
• Queries are performed using the ubiquitous yet familiar SELECT statement,
which is further divided into clauses, including SELECT, FROM, WHERE and
ORDER BY.
• Data Manipulation Language (DML) is used to add, update or delete data and
is actually a SELECT statement subset and is comprised of the INSERT, DELETE
and UPDATE statements, as well as control statements, e.g., BEGIN
TRANSACTION, SAVEPOINT, COMMIT and ROLLBACK.
• Data Definition Language (DDL) is used for managing tables and index
structures. Examples of DDL statements include CREATE, ALTER, TRUNCATE and
DROP.
• Data Control Language (DCL) is used to assign and revoke database rights and
permissions. Its main statements are GRANT and REVOKE.
Why SQL?
USE OF SQL:
EXPLAIN DATABASE?
DBMS allows users to create their own databases as per their requirement.
The term “DBMS” includes the user of the database and other application
programs. It provides an interface between the data and the software
application.
Types of DBMS
Hierarchical DBMS
Network Model
The network database model allows each child to have multiple parents. It
helps you to address the need to model more complex relationships like as
the orders/parts many-to-many relationship. In this model, entities are
organized in a graph which can be accessed through several paths.
Relational model
Relational DBMS is the most widely used DBMS model because it is one of
the easiest. This model is based on normalizing data in the rows and
columns of the tables. Relational model stored in fixed structures and
manipulated using SQL.
Object-Oriented Model
Float (floating point) Number with a decimal point 3.15, 9.06, 00.13
The data type defines which operations can safely be performed to create, transform
and use the variable in another computation. When a program language requires a
variable to only be used in ways that respect its data type, that language is said to
be strongly typed. This prevents errors, because while it is logical to ask the computer
to multiply a float by an integer (1.5 x 5), it is illogical to ask the computer to multiply a
float by a string (1.5 x Alice). When a programming language allows a variable of one
data type to be used as if it were a value of another data type, the language is said to
be weakly typed.
DML statements are used for performing queries on the data within
schema objects. The purpose of DQL Command is to get some
schema relation based on the query passed to it.
Example of DQL:
Examples of DML:
CREATE QUERY
DATABASE:
SYNTAX:
(create database db1;)
TABLE:
SYNTAX:
(CREATE TABLE table_name (column_name column_type...));
ALTER:
SYNTAX:
(ALTER TABLE table_name, ADD
new_column_namecolumn_definitions);
Result:
DROP:
SYNTAX:
(DROP TABLE table_name);
DROP COLUMN:
DML (DATA MANIPULATION LANGUAGE):
INSERT:
SYNTAX:
(INSERT INTO table_name VALUES (value1, value2,... valueN ); )
TABLE EMP
TABLE EMP_DEPT
UPDATE:
SYNTAX:
{(UP DATE table_name SET field1=new-value1, field2=new-
value2, ...
[W HERE Clause]}
Result:
DELETE:
SYNTAX:
: {(UP DATE table_name SET field1=new-value1, field2=new-
value2, ...
[W HERE Clause]}
DQL (DATA QUERY LANGUAGE):
SELECT:
SYNTAX:
(SELECT * FROM tables [W HERE conditions]; )
DIFFERENCE BETWEEN DESCRIBE AND SELECT
DESCRIBE:
§ Column Name
§ Column allow NULL or NOT NULL
§ Datatype of the Column
§ With database size precision and If NUMERIC datatype scale.
SYNTAX:
(DESC table name;)
SELECT:
SYNTAX:
(SELECT * FROM table name;)
DIFFERENCE BETWEEN ALTER AND UPDATE
SYNTAX:
DROP DATABASE:
DELETE:
SYNTAX:(DELETE FROM table name, where column name =
value;)
MySQL DELETE statement is used to delete data from the
MySQL table within the database. By using delete statement, we
can delete records on the basis of conditions.
AGGREGATE FUNCTIONS
In database management an aggregate function is a function
where the values of multiple rows are grouped together as input
on certain criteria to form a single value of more significant
meaning.
Various Aggregate Functions:
1) Count( )
2) Sum( )
3) Avg( )
4) Min( )
5) Max( )
6) First( )
7) Last( )
COUNT:
SYNTAX: (SELECT COUNT (column name) from table name;)
Returns total number of records.
SUM:
SYNTAX: (SELECT SUM (column name) AS ‘expression’ from
table name;) Sum all Not Null values of Column salary.
AVG:
SYNTAX:
(SELECT AVG (column name) AS ‘expression’ from table name;)
Function is an aggregate function that returns the average
value of a group.
MIN:
SYNTAX: (SELECT MIN(column name) from table name;)
Function returns the smallest value of the selected column
MAX:
SYNTAX:
(SELECT MAX(column name ) from table name; )
Function returns the largest value of the selected column
FIRST:
SYNTAX: SELECT column_name FROM table_name LIMIT 1;
The MySQL first function is used to return the first value of the
selected column. Here, we use limit clause to select first record
or more.
ORIGINAL EMP TABLE:
RESULT = LIMIT 1
RESULT = LIMIT 2
LAST:
SYNTAX:
(SELECT column_name FROM table_name ORDER BY
column_name DESC LIMIT 1;) MySQL last function is used to
return the last value of the selected column.
ORIGINAL EMP TABLE:
RESULT = LIMIT 1
RESULT = LIMIT 2
GROUP BY CLAUSE
The GROUP BY statement groups rows that have the same
values into summary rows. The GROUP BY clause is a SQL
command that is used to group rows that have the same values.
The GROUP BY clause is used in the SELECT statement.
Optionally it is used in conjunction with aggregate functions to
produce summary reports from the database.
SYNTAX:
{(SELECT expression1, expression2, ...expression n, Aggregate
function (expression) FROM tables [W HERE conditions] GROUP
BY expression1, expression2, ... expression n;}
ORIGINAL EMP TABLE:
RESULT:
ORIGINAL EMP_DEPT TABLE:
RESULT:
ORDER BY CLAUSE
The ORDER BY statement in sql is used to sort the fetched data
in either ascending or descending according to one or more
columns. By default ORDER BY sorts the data in ascending
order.We can use the keyword DESC to sort the data in
descending order and the keyword ASC to sort in ascending
order.
ORDER BY:
SYNTAX:
{(SELECT expressions
FROM tables [W HERE conditions]
ORDER BY expression [ ASC | DESC];}
ORIGINAL EMP TABLE:
RESULT:
ASCENDING ORDER:
SYNTAX:
{(SELECT expressions FROM tables [W HERE conditions] ORDER
BY expression [ ASC];}
ORIGINAL EMP TABLE:
RESULT:
DESCENDING ORDER:
SYNTAX:
{(SELECT expressions FROM tables [W HERE conditions] ORDER
BY expression [ DESC];}
ORIGINAL EMP TABLE:
RESULT:
WHERE CLAUSE
The SQL WHERE clause is used to specify a condition while
fetching the data from a single table or by joining with multiple
tables. If the given condition is satisfied, then only it returns a
specific value from the table. You should use the WHERE clause
to filter the records and fetching only the necessary records.
The WHERE clause is used to filter records. The WHERE clause is
used to extract only those records that fulfill a specified
condition.
SYNTAX:
(WHERE conditions;)
ORIGINAL EMP TABLE:
RESULT:
ORIGINAL EMP_DEPT TABLE
RESULT:
HAVING CLAUSE
SQL - Having Clause. The HAVING Clause enables you to specify
conditions that filter which group results appear in the results.
The WHERE clause places conditions on the selected columns,
whereas the HAVING clause places conditions on groups created
by the GROUP BY clause.
SYNTAX:
{SELECT expression1, expression2, ... expression n, Aggregate
Function (expression) FROM tables GROUP BY expression1,
expression2, ... expression n HAVING condition;}
DIFFERENCE BETWEEN HAVING AND WHERE CLAUSE
“The difference between the having and where clause in SQL is
that the where clause cannot be used with aggregates, but the
having clause can.”
1) WHERE clause is processed right after FROM clause in the
logical order of query processing, which means it is processed
before GROUP BY clause while HAVING clause is executed after
groups are created.
2) If used in GROUP BY, You can refer any column from a table
in WHERE clause but you can only use columns which are not
grouped or aggregated.
3) If you use HAVING clause without group by, it can also refer
any column but the index will not be used as opposed to WHERE
clause. For example, the following have the same result set,
however "where" will use the id index and having will do a table
scan s e l e ct * f r om t a bl e wher e i d = 1 s e l e ct * f r om t a
bl e ha vi ng i d = 1
4) You can use an aggregate function to filter rows with HAVING
clause. Because HAVING clause is processed after the rows have
been grouped, you can refer to an aggregate function in the
logical expression. For example, the following query will display
only courses which have more than 10 students : S E LE CT C o
ur s e , C OUNT( C ou rs e) a s NumOf S t uden t f r om T r ai ni n
g GROUP BY C o ur s e HAVI NG C OUNT( C OURS E) > 10
5) Another key difference between WHERE and HAVING clause
is that WHERE will use Index and HAVING will not, for example
following two queries will produce an identical result but WHERE
will use Index and HAVING will do a table scan
S E LE CT * F ROM C o ur s e WHERE I d = 1 0 1;
S E LE CT * F ROM C o ur s e HAVI NG I d = 102;
PRIMARY KEY
SQL PRIMARY KEY Constraint apply on column(s) for a uniquely
identifies each record (row) in the table.
SQL Primary Key constraint has been specified for certain
column. we can not enter duplicate data in this column.
SQL Primary Key in a table have following three special
attributes,
§ The NOT NULL attribute is automatic active.
§ The data across the column must be unique.
§ Defines column as a mandatory column.
FOREIGN KEY
SQL FOREIGN KEY Constraint apply on column(s) for whose
value must have reference in another table column (that
existing column must be primary key or unique key
constraint).SQL FOREIGN KEY constraints also known as
relationship (referential) constraints.
SYNTAX:
FOREIGN KEY (table2. column name) REF ERENC ES table1
(column name)
TABLES:
NOT NULL
SQL NOT NULL Constraint enforces to a column is always
contain a value. This means that you can not insert NULL (blank)
value in this field. SQL NOT NULL Constraint applied only at
column level . You should manually define NOT NULL constraint
because table column set NULL value already.
SYNTAX: (column name data type data size NOT NULL,)
(Create table table name (column name data size Not Null)
LOGICAL OPERATOR
LIKE
SQL LIKE operator is used with WHERE clause to matches
specific pattern in a column. SQL LIKE condition apply on table
column data.
Following two wildcards are often used with the LIKE operator
§ % - Represents zero, or any number of characters
§ _ - Represents a single character
SYNTAX:
(SELECT * FROM table name W HERE column name LIKE 'value';
ORIGINAL EMP TABLE:
RESULT:
BETWEEN
SQL BETWEEN operator used for fetching within range data.
SQL BETWEEN query simply a shorthand way of expressing an
inclusive range comparison.
SYNTAX:
(SELECT * FROM table name W HERE column name BETWEEN
value AND value;)
ORIGINAL EMP TABLE:
RESULT:
AND
SQL AND condition use to test more then one conditions in
INSERT, UPDATE, DELETE, SELECT statement. AND operator
work is test first condition if true come to a second and so forth,
otherwise not check next condition.
SYNTAX:
(SELECT * FROM table name W HERE Column name conditon
'value' AND Column name condition ‘value’;)
ORIGINAL EMP TABLE:
RESULT:
OR
SQL OR Condition use to test more then one conditions in
INSERT, UPDATE, DELETE, SELECT statement. OR operator test
all condition even if condition TRUE or FALSE. And return data
when any one of the condition TRUE. SQL OR Operator same as
AND operator, return the record base filtered data. INSERT,
UPDATE, DELETE, SELECT statement perform only one of the
specified condition TRUE.
SYNTAX:
(SELECT * FROM table name W HERE Column name conditon
'value' OR Column name condition ‘value’; )
RESULT:
IN
The MySQL IN condition is used to reduce the use of multiple OR
conditions in a SELECT, INSERT, UPDATE and DELETE statement.
SYNTAX:
SELECT * FROM table name W HERE Column name IN ('values’);
ORIGINAL EMP TABLE:
RESULT
COMPARISON OPERATOR
A comparison (or relational) operator is a mathematical symbol
which is used to compare two values.
· GREATER THAN
· LESS THAN
· GREATER THAN EQUALS TO
· LESS THAN EQUALS TO
· EQUALS TO
· NOT EQUAL TO
EQUALS TO
SYNTAX: (SELECT * FROM table name WHERE column name =
value;)
GREATER THAN
SYNTAX: (SELECT * FROM table name WHERE column name >
value;)
LESS THAN
SYNTAX: (SELECT * FROM table name WHERE column name <
value;)
JOIN
MySQL JOINS are used with SELECT statement. It is used to
retrieve data from multiple tables. It is performed whenever you
need to fetch records from two or more tables.
There are three types of MySQL joins:
o MySQL INNER JOIN (or sometimes called simple join)
o MySQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
o MySQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
INNER JOIN:
SYNTAX:
(SELECT table1.column name, table1.column name,
table2.column name FROM table1 INNER JOIN table2
ON table1.column name = table2.column name;)
RESULT:
RESULT: