SQL Class 12 Notes CBSE Computer Science - Techt6
SQL Class 12 Notes CBSE Computer Science - Techt6
techtipnow
https://techtipnow.in/sql-class-12-notes/
What is SQL?
It also defines key constraints, relationships between tables, and other data
validation constraints.
Example of DDL commands are:
What is MySQL?
Characteristics of MySQL
It is free and Open Source software which does not require to pay for its
usage.
Decimal(3,2): 34.89
Decimal(m,d) Fixed point number. M denotes no of digits stored for
values and d denotes no of decimal points. Decimal(4): 2813
Float Real number 456.23
Creating a Database
Syntax
Example:
Using a database
USE <databasename>;
Example:
Syntax
SELECT DATABASE();
Drop Database
Syntax:
Show tables
Syntax:
SHOW TABLES;
Syntax:
CREATE TABLE<table name>
(
<column 1><data type> [constraint] ,
<column 2><data type>[constraint],
<column 3><data type>[constraint]
);
Example:
Not Null
Sometimes an attribute may not be allowed to leave blank. Hence Not Null
constraints can be associated in this case.
Example:
Default
Primary Key
Example:
Foreign Key
Example:
Here in this example Ino is the foreign key that references Ino of Item table
which is Primary key.
The SQL command for making Ino to foreign key in Order table is as follows:
Create Order
(
Ono int Primary Key,
Odate date,
Itemno int,
Qty int,
Total int,
FOREIGN KEY (Itemno) REFERENCES Item (Itemno)
);
Screen Shot:
Unique Key
Example:
Syntax:
Example:
Drop table
Syntax:
Example:
Alter table
Adding a column
Example:
ALTER TABLE Item ADD date_of_purchase date NOT NULL;
Dropping a column
Example:
A column can also be modified using Alter Table command as given below:
Insert command
Syntax:
Screenshot:
Update command
Update command is used to modify the attribute values of one or more tuples
in a table.
Syntax:
UPDATE <tablename>
SET <expression>
WHERE <criteria>
Example:
UPDATE Item
SET Rate = 30
WHERE itemno = ‘i0001’;
Screenshot:
Delete command
Syntax:
Screenshot:
Select command
Syntax:
Screenshot:
Code:
Screenshot:
Query5: retrieve item details which rate is less than 100 and qty is more
than 100.
Screenshot:
DISTINCT Command
Syntax:
Example
WHERE clause
It can be used with any DML command such as SELECT, DELETE and
UPDATE.
Example:
Arithmetic operators
Relation operators
Logical operators
BETWEEN operators
IN operators
LIKE operators
Arithmetic operators
= Equal to
> Greater than
< Lesser than
>= Greater than equal to
<= Lesser than equal to
!= or <> Not equal to
Logical operators
IN operator
LIKE operator
Wildcard Description
Example:
Example
Aggregate function
Sum(field) Returns sum of all rows of specified field Select sum(qty_sold) from order;
Output: 10230
Avg(field) Returns average or mean value of all rows of Select avg(qty_sold) from order;
specified field Output: 780
Max(field) Returns largest value among all rows of Select max(qty_sold) from order;
specified field Output: 1460
Min(field) Returns smallest value among all rows of Select min(qty_sold) from order;
specified field Output: 470
count(field) Counts and return total rows of specified Select count(qty_sold) from order;
field Output: 72
Group By
Syntax:
Example:
Having clause
Syntax:
Joins
In another way we can say that it is query written to retrieves data from two
or more tables as per given condition.
Cartesian product
Equi join
Natural join
Inner Join