Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views37 pages

SQL Database For Xii It

The document provides an overview of database concepts, including definitions, properties, and types of data in MySQL. It explains SQL commands for Data Definition Language (DDL) and Data Manipulation Language (DML), highlighting the differences between commands like DELETE and TRUNCATE. Additionally, it covers SQL functions, constraints, and the use of GROUP BY and HAVING clauses for data aggregation.

Uploaded by

shrutipate123409
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views37 pages

SQL Database For Xii It

The document provides an overview of database concepts, including definitions, properties, and types of data in MySQL. It explains SQL commands for Data Definition Language (DDL) and Data Manipulation Language (DML), highlighting the differences between commands like DELETE and TRUNCATE. Additionally, it covers SQL functions, constraints, and the use of GROUP BY and HAVING clauses for data aggregation.

Uploaded by

shrutipate123409
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit-1

Database Concept

Database:
collection of related data that has been recorded, organized and made
available for searching is called a Database.

A database has the following properties:


1) A database is a representation of some aspect of the real world also called

miniworld. Whenever there are changes in this miniworld they are also
reflected in the database.
2) It is designed, built and populated with data for specific purpose.
3) It can be of any size and complexity.
4) It can be maintained manually or it may be computerized.
DATA TYPE OF MYSQL
1. Integer Type
Type Storage (Bytes) Range (Signed) Range(Unsigne
d)
TINYINT(Length) 1 -128 to 127 0 to 255
SMALLINT(Length) 2 -32768 to 32767 0 to 65535
MEDIUMINT(Length) 3 -8388608 to 8388607 0 to 16777215
INT(Length) 4 -2147483648 to 0 to
2147483647 4294967295
BIGINT(Length) 8 -263 to 263-1 0 to 264-1

2. Floating-Point
Type
Type Length RANGE RANGE
in Bytes (Signed) (Unsigned)

FLOAT 4 -3.402823466E+38 TO - 1.175494351E-38 TO


1.175494351E-38 3.402823466E+38
DOUBLE 8 -1.7976931348623 0, and
(Length, 157E+ 308 TO - 2.2250738585072014E- 308
Decimal) 2.22507385850720 TO 1.797693134862315
14E- 308 7E+ 308
DATA TYPE OF MYSQL
3. Fixed-Point type

Type Leftover Number RANGE


Digits of Bytes (Unsigned)

DECIMAL(M,D) 0 0 M is the maximum number of digits (the


precision). It has a range of 1 to 65(Digit).
1–2 1 D is the number of digits to the right of the
decimal point (the scale). It has a range of 0 to 30
and must be no larger than M.
3–4 2
5–6 3 Ex: DECIMAL(3,0) column supports a range of -
999 to 999.
7–9 4
DATA TYPE OF MYSQL

4. Text/String type

Type Storage Range Ex


(Bytes)
CHAR(Length) Length 0-255 Char(4)=‘abcd’
bytes Size=4 byte
VARCHAR(Length) String 0-65535 varchar(4)=‘abc
length+1 d’
Size=5 byte
TINYTEXT String A string with a maximum length of
length+1 255 characters
TEXT String A string with a maximum length of
length+2 65,535 characters
MEDIUMTEXT String A string with a maximum length of
length+3 16,777,215 characters
LONGTEXT String A string with a maximum length of
length+4 4,294,967,295 characters
DATA TYPE OF MYSQL

5. Date type

Data Type Storage Value Range


(Bytes)

DATE 3 ‘YYYY-MM-DD' '1000-01-01' to '9999-12-31'


TIME 3 ‘HH:MM:SS' ‘23:59:59’
DATETIME 8 ‘YYYY-MM-DD '1000-01-01 00:00:00' to '9999-
HH:MM:SS' 12-31 23:59:59'
TIMESTAM 4 ‘YYYY-MM-DD '1970-01-01 00:00:01' UTC to
P HH:MM:SS' '2038-01-19 03:14:07' UTC.
(Universal Time Coordinated)
YEAR 1 ‘YYYY’ 1901 to 2155 , or 0000
TYPES OF SQL COMMAND
DDL vs DML

DDL - DDL will be dealing with the structure of DB objects


DDL's
Auto commit. You can not rollback the changes if you
have
fired any DDL statement.

DML - DML statements deals with the data. You will be fire
commit
statement explicitly.

You can rollback changes because Oracle keeps record of


changed rows in UNDO/Rollback segment.
What is the difference between Delete and TRUNCATE?

1. Delete is a DML command I DDL is a DDL command


2. In case of TRUNCATE, you can not rollback your changes I

Delete will allow to rollback your changes


3. TRUNCATE is faster than Delete because it doesn't keep
records of the changes been done.
4. TRUNCATE doesn’t allow to delete specific rows I Delete
allows us to delete specific rows based on the filters.
5. We can not create TRIGGERS on TRUNCATE statement I
We
can create before/after Triggers on Delete statement.
Why TRUNCATE is a DDL command?

1. TRUNCATE command on a table is going remove all the data


from the table.

2. Data Dictionary will be updated - De- allocate data pages


and
will make them empty so that it can be used by other DB
objects.

3. It changes the storage definition and change the structure


of
the table by resetting the water high mark back to zero.

4. TRUCATE doesn't allow us to have where clause that is one


of
the reason why it is DDL.
SQL for DDL
1.Create Database
CREATE DATABASE
DataBaseName;
Note : A DBMS can manage multiple databases on one
computer. Therefore, we need to select the database that we
want to use. Once the database is selected, we can proceed
with creating tables or querying data.
USE DataBaseName;
Note : To check list of Databases present in Computer use
following command-
Show databases;
Note : To check list of Tables present in Database use
following command- Show tables;
SQL for DDL
2. Create Table
Syntax:
CREATE TABLE TableName
(
attributename1 datatype constraint,
attributename2 datatype constraint,
.
.
.
attributenameN datatype constraint
);

We can view the structure of an already created


table using the describe statement.

Syntax:
DESCRIBE TableName;
OR
Desc TableName;
SQL for DDL
3. Alter Table
After creating a table we may realize that we need to add/remove an
attribute or to modify the datatype of an existing attribute or to add
constraint in attribute. In all such cases, we need to change or alter the
structure of the table by using the alter statement.
Syntax:
ALTER TABLE tablename ADD/Modify/Change/DROP
attribute1, attribute2, ……
(A) Add primary key to a relation
(B) Add foreign key to a relation
(C) Add constraint UNIQUE to an existing attribute
(D) Add an attribute to an existing table
(E) Modify datatype of an attribute
(F) Modify constraint of an attribute
(G) Add default value to an attribute
(H) Remove an attribute
(I) Remove primary key from the table
(J) Rename Table name
(K) Rename Column name
SQL for DDL
4. Drop Statement
Syntax to drop a table:
DROP TABLE Table_Name;

Syntax to drop a database:


DROP DATABASE Database_Name;

5. Truncate Statement
Syntax to drop a table:
Truncate TABLE Table_Name;
Introduction to
Structured Query
Language (SQL)
SQL for Data Definition Language

ALTER Table

Syntax:
ALTER TABLE tablename
ADD/Modify/ChangeDROP attribute1,
attribute2,…..;
SQL for Data Definition Language
ALTER Table
(A) Add primary key to a relation
syntax:
ALTER TABLE Table_Name ADD PRIMARY KEY
(attribute);

(B) Add foreign key to a relation


syntax:
ALTER TABLE table_name ADD FOREIGN
KEY(attribute)
REFERENCES referenced_table_name
(attribute);

(C) Add constraint UNIQUE to an existing attribute


syntax:
ALTER TABLE table_name ADD UNIQUE (attribute);

(D) Add constraint CHECK to an existing attribute


SQL for Data Definition Language
ALTER Table

(E) Add an attribute to an existing table


syntax:
ALTER TABLE table_name ADD new_attribute data_type;
ALTER TABLE table_name ADD new_attribute data_type
after
existing_attribute;
ALTER TABLE table_name ADD new_attribute data_type
first;

(F) Modify datatype of an attribute


syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE;

(G) Modify constraint of an attribute


syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE
SQL for Data Definition Language
ALTER Table
(H) Add default value to an attribute
syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE
DEFAULT
default_value;

(I) Remove an attribute


syntax:
ALTER TABLE table_name DROP attribute;

(J) Remove primary key from the table


syntax:
ALTER TABLE table_name DROP PRIMARY KEY;

(K) Remove foreign key from table


syntax:
ALTER TABLE table_name DROP FOREIGN KEY attribute;
SQL for Data Definition Language
ALTER Table
(L) Remove Unique Key from table
syntax:
ALTER TABLE table_name DROP INDEX attribute;

(M) Remove CHECK Constraint from table


syntax:
ALTER TABLE table_name DROP Check attribute;

(N) Rename Column Name


syntax:
Alter table table_name change old_col new_col
datatype;

(O) Rename Table Name


syntax:
Alter table table_name rename new_table_name;
SQL for Data Definition Language
DROP Statement

Syntax to drop a table:


DROP TABLE table_name;

Syntax to drop a database:


DROP DATABASE database_name;
SQL for Data Definition Language

Truncate Statement

It is used to delete all the rows of a relation (table)


in one go.

Truncate table table_name;


SQL for Data Manipulation Language

INSERTION of Records
1. Insert Record to all the columns
insert into table_name
values(value1,value2,.........);
2. Insert Record to specific columns
insert into table_name(Field1,Field2,.....)

values(value1,value2,.........);
3. Insert Multiple records in one Line:
insert into table_name
values(value1,value2,.........),
(value1,value2,.........),
(value1,value2,.........);
SQL for Data Manipulation Language
SELECT Statement
1. Projection
Return rows from a table with selective
attributes.
select col,col2,col3 from table_name;
2. Selection
Return specific Rows .
select * from table_name;
i. DISTINCT Clause
SELECT DISTINCT Attribute FROM EMPLOYEE;
ii. WHERE Clause
SELECT * FROM EMPLOYEE WHERE Attribute=value;
iii. MEMBERSHIP OPERATOR IN
SELECT * FROM EMPLOYEE WHERE Attribute IN
(Value1,Value2,....);
iv. Between OPERATOR
SELECT * FROM EMPLOYEE WHERE Attribute Between
val1 and
Val_n;
v. ORDER BY Clause
vi. Handling NULL Values
SQL for Data Manipulation Language
SELECT Statement
vii. Substring pattern matching
The LIKE operator makes use of the following two wild card
characters:
• % (percent) — used to represent zero, one, or multiple
characters.
• _ (underscore)— used to represent a single character.
SQL for Data Manipulation Language

Data Updation
Syntax:
UPDATE table_name
SET attribute1 = value1, attribute2 = value2, ...
WHERE condition;

Data Deletion
Syntax:
DELETE FROM table_name WHERE condition;
Operators in SQL

Arithmet
ic Comparison
Operator Operator Special Operator
+ BETWEEN/
- < Logical NOT BETWEEN

* > Operat IS NULL/


IS NOT NULL
/ <= or
LIKE/ NOT LIKE
% >= AND IN/ NOT IN
= OR
!= or DISTINCT
NOT
<>
Constraint in MySQL

1.Primary Key Constraint


2.Foreign Key Constraint
3.Unique Key Constraint
4.Not Null Constraint
5.Check Constraint
6.Default Constraint
Functions in SQL
Multiple Row Function (Aggregate Functions)

Aggregate functions are also called multiple row functions. These functions
work on a set of records as a whole, and return a single value for each
column of the records on which the function is applied.

Function Description
MAX(column) Returns the largest value from
the specified column.
MIN(column) Returns the smallest value from
the specified column.
AVG(column) Returns the average of the values
in the specified column.
SUM(column) Returns the sum of the values
for the specified column.
COUNT(column) Returns the number of values
in the specified column ignoring
the NULL values.
COUNT(*) Returns the number of records
in a table.
GroupBy and Having Clause in SQL
Group By Clause
At times we need to fetch a group of rows on the basis of common values in
a column. This can be done using a GROUP BY clause. It groups the rows
together that contain the same values in a specified column. We can use the
aggregate functions (COUNT, MAX, MIN, AVG and SUM) to work on the
grouped values.

HAVING Clause
HAVING Clause in SQL is used to specify conditions on the rows with GROUP
BY clause.

Consider the SALE table from the CARSHOWROOM database:


GROUP BY Clause in SQL
d) Display the PaymentMode and number of payments made using
that mode more than once.
mysql> SELECT PaymentMode, Count(PaymentMode) FROM SALE
GROUP BY Paymentmode HAVING COUNT(*)>1 ORDER BY
Paymentmode;
SALE table from the CARSHOWROOM database

a) List the total number of cars sold by each employee.


b) List the maximum sale made by each employee.
Difference between Where and Having Clause in
MYSQL :
Sno Where Clause Having Clause
1. WHERE Clause is used to filter HAVING Clause is used to filter
the records from the table based record from the groups based on
on the specified condition. the specified condition.

2. WHERE Clause can be used HAVING Clause cannot be used


without GROUP BY Clause without GROUP BY Clause

3. WHERE Clause cannot contain HAVING Clause can contain


aggregate function aggregate function

4. WHERE Clause can be used with


HAVING Clause can only be used
SELECT, UPDATE, DELETE
with SELECT statement.
statement.

5. WHERE Clause is used before HAVING Clause is used after


GROUP BY Clause GROUP BY Clause

6. WHERE Clause is used with HAVING Clause is used with


single row function like UPPER, multiple row function like SUM,
LOWER etc. COUNT etc.
Difference between Having clause and Group by
clause
S.No
Having Clause GroupBy Clause
.
The groupby clause is used to
It is used for applying some
1. group the data according to
extra condition to the query.
particular column or row.

Having can be used without


groupby can be used without
groupby clause,in aggregate
2. having clause with the select
function,in that case it
statement.
behaves like where clause.

The having clause can It cannot contain aggregate


3.
contain aggregate functions. functions.

It restrict the query output by It groups the output on basis


4.
using some conditions of some rows or columns.
Q.
Q.

Write SQL queries using function to perform the


following operation:
a. Display maximum sales for each area.
b. Display those addresses which anywhere contain
‘i’;

You might also like