STRUCTURED QUERY LANGUAGE
DATABASE CONCEPTS
SQL ENABLES THE FOLLOWING:
MAIN PURPOSE OF DATABASE
Duplication of data is known as data redundancy. That means multiple copies of the same data is available in many places.
Suppose multiple copies of the same data is available in many places, but only one copy of the data is updated, and the other copies has the old data. This situation is referred to as inconsistency of data.
Multiple mismatching of the same data is known as data inconsistency.
Since data is available in centralized form, data may be shared among several users, in the sense that each users may use the same data for different purpose.
MAIN PURPOSE OF DATABASE
Refers to protection of data against accidental or intentional disclosure unauthorized persons for modification or destruction.
DBMS can ensure that all the data follow a certain standard.(refers to the format in which the data is stored or accessed).
refers to the rights of individuals and organizations to determine when, how and to what extend information about them can be transmitted to others.
LEVEL OF DATABASE IMPLEMENTATION
Internal level
Stored item length :40B
Name type(Where it is stored)
Price type
Qty type
Conceptual level
Name char(40)
Price Numeric
Qty Numeric
View 1
Name + Price
View 2
Price+Qty
DATABASE TERMINOLOGY
A relation is a table in database i.e data is arranged in rows and columns.
A relationship has the following property
SpNo | ship |
1 | abc |
2 | xyz |
3 | def |
DATABASE TERMINOLOGY
A domain is a pool of values from which the actual values are drawn.
S1 S2 S3 S4 S5 |
SpNo | ship |
S1 | abc |
S3 | def |
SpNo | city |
S1 | Jaipur |
S3 | chennai |
S4 | Delhi |
Domain
DATABASE TERMINOLOGY
RNO | NAME | MARKS | DOB | GRADE |
1 | AAAA | 89 | 1/2/2000 | B |
2 | BBBB | 75 | 5/12/2000 | C |
3 | CCCC | 96 | 4/5/1999 | A |
4 | DDDD | 69 | 8/9/2000 | D |
ATTRIBUTE
TUPLE
DEGREE : 5
CARDINALITY : 4
KEYS
A Primary key is a unique attribute in a relation. It can be a set of one or more attribute that can uniquely identify the tuples within the relation.
All the attributes in a relation that can serve as a primary key is known as candidate key.
A candidate key that is not the primary key is called an alternate key.
KEYS
An attribute whose values are derived from the primary key of some other table is known as foreign key in its current table.
REFERENTIAL INTEGRITY
KEY FEATURES OF MYSQL
RNO,AdminNo,NAME
AdminNo
RNO,NAME
RNO | AdminNo | NAME | MARKS | DOB | GRADE |
1 | 121 | AAAA | 89 | 1/2/2000 | B |
2 | 122 | BBBB | 75 | 5/12/2000 | C |
3 | 123 | CCCC | 96 | 4/5/1999 | A |
4 | 124 | DDDD | 69 | 8/9/2000 | D |
5 | 125 | EEEEE | 92 | 23/6/2000 | A |
RULES FOR SQL COMMANDS
MYSQL SQL ELEMENTS
LITERALS
DATA TYPES
🡪 Numeric
🡪 Date and Time
🡪 String types
DATA TYPES IN SQL
DIFFERENCE BETWEEN CHAR AND VARCHAR
Each value that is stored in this column stores exactly as you specify it, i.e no blanks are added if the length is shorter than the maximum length n.
COMMENTS IN SQL
ACCESSING DATABASE IN MYSQL
mysql>create database Name;
mysql>USE Name;
Database changed
CREATING A TABLE
( <attribute name> <data type> <size><constraint>,
<attribute name> <data type> <size><constraint>
);
TO CREATE A TABLE CALLED STUDENT WITH THE FOLLOWING ATTRIBUTE
( Rno int(3) PRIMARY KEY,
AdminNo int(3) NOT NULL UNIQUE,
Name char(20) NOT NULL,
Marks decimal,
School char(20) DEFAULT ‘BGS NPS’,
Grade char(2) );
RNO | AdminNo | NAME | MARKS | GRADE |
TO CREATE A TABLE CALLED STUDENT WITH THE FOLLOWING ATTRIBUTE
( Rno int PRIMARY KEY,
AdminNo int NOT NULL,
Name char(20),
Marks decimal,
Grade char(2) );
RNO | AdminNo | NAME | MARKS | GRADE |
ADD VALUES INTO THE TABLE
INSERT INTO STUDENT
VALUES (1,121,’AAA’,89,’B’);
INSERT INTO STUDENT(RNO,AdminNo,NAME)
VALUES (1,121,’AAA’);
RNO | AdminNo | NAME | MARKS | GRADE |
1 | 121 | AAAA | 89 | B |
2 | 122 | BBBB | 75 | C |
3 | 123 | CCCC | 96 | A |
INSERTING NULL VALUES
INSERT INTO STUDENT
VALUES (4,124,’DDD’,23,NULL)
Here, the Grade column for the forth record will be NULL
RNO | AdminNo | NAME | MARKS | GRADE |
1 | 121 | AAAA | 89 | B |
2 | 122 | BBBB | 75 | C |
3 | 123 | CCCC | 96 | A |
VIEWING STRUCTURE OF A TABLE STUDENT
If you want to know the structure of a table, you can use Describe or Desc , the syntax is
mysql>DESC <TABLE NAME>;
mysql>DESC STUDENT;
RNO | AdminNo | NAME | MARKS | GRADE |
1 | 121 | AAAA | 89 | B |
2 | 122 | BBBB | 75 | C |
3 | 123 | CCCC | 96 | A |
WRITE THE SQL QUERY TO CREATE A TABLE AND ADD THE FOLLOWING VALUES INTO THE TABLE.�
Sno | Item | Qty | Price | Company | Supcode | City |
1 | Pen Drive | 70 | 300 | SanDisk | S109 | Delhi |
2 | Camera | 50 | 4500 | CoolPix | S102 | GOA |
3 | Laptop | 100 | 35000 | Lenovo | S112 | Delhi |
4 | Car GPS | 30 | 12000 | Dell | S103 | GOA |
TABLE: SONY
SELECT- TO VIEW A TABLE
To select the column Sno particular column
To display Sno, Item Price from the same table
WHERE
1. Query to display from table sony where price is 300.
WHERE Price=300;
2. Query to display the item name from table sony where price more than 5000.
WHERE Price > 5000;
3. Query to display the item name and price from table sony where price more than 10000 and less than 40000.
WHERE Price > 10000 AND Price <40000;
CREATING A VIEW
SELECT * FROM SONY
WHERE PRICE > 5000;
5. .Query to display from table sony where Supcode is S103.
WHERE Supcode=‘S103’;
6 .Query to display Item,Company from table sony where Supcode is S109.
WHERE Supcode=‘S109’;
7 .Query to display Item,Price,Company from table sony where Company is LENOVO or DELL.
WHERE Company=‘LENOVO’ OR Company=‘DELL’;
MAX AND MIN
8. Query to display the maximum Price from table sony.
9. Query to display the maximum Price from table sony from company Dell.
WHERE Compay=‘Dell’;
MAX AND MIN
12 Query to display the minimum Price from table sony.
13. Query to display the minimum Price from table sony from company Dell.
WHERE Compay=‘Dell’;
AVERAGE
15. Display the average Price from the table SONY
16 Display the average Price from the table SONY from DELL or LENOVO company.
WHERE Company=‘Dell’ OR Company=‘Lenovo’;
ORDER BY- TO ARRANGE IN ASC OR DESC ORDER ACCORDING TO AN ATTRIBUTE
17. Query to display according to the ascending order of Price from table sony.
ORDER BY Price ASC;
18. Query to display according to the Descending order of the Qty from table sony.
ORDER BY Qty DESC;
DISTINCT
SELECT DISTINCT City FROM SONY;
SELECT ALL City FROM SONY;
City |
Delhi |
City |
Delhi |
Delhi |
Delhi |
Delhi |
LOGICAL OPERATORS
SELECT name, marks FROM student
WHERE grade=‘A’ OR grade=‘B’;
SELECT name, marks FROM student
WHERE grade=‘A’ AND marks>90;
SELECT name, marks FROM student
WHERE NOT grade=‘C’;
BETWEEN
SELECT name, marks FROM student
WHERE marks>=55 AND marks<=75;
SELECT name, marks FROM student
WHERE marks between 55 AND 75;
//INCLUDING 55 AND 75
SELECT name FROM student
WHERE RNo>=40 AND Rno <=30;
SELECT name, marks FROM student
WHERE NOT RNo between 30 AND 40;
CONDITION BASED ON LIST
SELECT name FROM student
WHERE Class IN (5,8,11);
SELECT name FROM student
WHERE Class NOT IN (5,8,11);
COUNT
SELECT COUNT(*) FROM SONY;
SELECT COUNT(*) FROM SONY
WHERE Company=‘DELL’;
SELECT count (*) FROM student
WHERE Class IN (5,8,11);
SELECT COUNT(DISTINCT Company) FROM SONY;
USE OF % AND _
select * from student
where name like ‘A%’;
select * from student
where address like ’13#%’;
USE OF % AND _
select * from student
where name like ‘A _ _’;
select * from student
where name like ’_ _ _ D’;
SEARCH FOR NULL
WHERE department IS NULL;
HANDLING NULL
SELECT NAME, IFNULL(DESIGN,”BENCHED”) FROM EMPLOYEE;
Note: The value being substituted for NULL values through IFNULL() should be of the same data type as that of the column.
SQL CONSTRAINTS
Constraint is a condition or check applicable on a field or set of fields.
If you do not want to allow NULL value in a column, you will want to place a NOT NULL constraint.
CREATE TABLE Customer
( CID int PRIMARY KEY,
NAME char(10) NOT NULL,
Amount float NOT NULL,
Discount float );
SQL CONSTRAINTS
The DEFAULT constraint provides default value to a
column when the INSERT INTO statement does not provide a specific value
CREATE TABLE Customer
( CID int PRIMARY KEY,
NAME char(10) NOT NULL,
Amount float NOT NULL,
Discount float DEFAULT 0.0);
SQL CONSTRAINTS
INSERT INTO CUSTOMER(CID,NAME, Amount)
VALUES (1,”Amy”,1234)
SQL CONSTRAINTS
3. UNIQUE:
The UNIQUE constraint ensures that all the values in a column are distinct.
CREATE TABLE Customer
( CID int PRIMARY KEY,
NAME char(10) ,
Phone int UNIQUE,
Amount float NOT NULL,
CITY CHAR(10) DEFAULT ‘DELHI’,
Discount FLOAT DEFAULT 0.0);
SQL CONSTRAINTS
4. CHECK:
The CHECK constraint ensures that all the values in a column satisfy certain conditions
CREATE TABLE Customer
( CID int PRIMARY KEY ,
NAME char(10) NOT NULL ,
Phone int UNIQUE ,
Amount float CHECK(Amount > 100),
COMPANY VARCHAR(10) DEFAULT ‘BGSNPS’);
SQL CONSTRAINTS
5. PRIMARY KEY:
Used to uniquely identify each row in a table
CREATE TABLE Customer
( CID int PRIMARY KEY ,
NAME char(10) ,
Phone int UNIQUE,
Amount float CHECK(Amount > 100) );
SQL CONSTRAINTS
5. PRIMARY KEY:
Used to uniquely identify each row in a table
CREATE TABLE Customer
( CID int,
NAME char(10) ,
Phone int UNIQUE,
Amount float,
PRIMARY KEY (CID) );
SQL CONSTRAINTS
5. PRIMARY KEY:
Used to uniquely identify each row in a table
CREATE TABLE Customer
( CID int,
NAME char(10) ,
Phone int UNIQUE,
Amount float,
PRIMARY KEY (CID,NAME) );
6. FOREIGN KEY:
When 2 tables are related by a common column , then the related column of the parent table should be declared as the primary or Unique field and the related column in child table should have the FOREIGN Key constraint
COLUMN NAME | CHARACTERISTICS |
CID | Primary key |
Name | |
Amount | |
TABLE : CUSTOMER
TABLE : ORDER
COLUMN NAME | CHARACTERISTICS |
OID | Primary key |
CID | Foreign key |
DATE | |
FOOD | |
CREATE TABLE ORDER1
(OID int Primary key,
CID INT,
DATE date,
FOOD char(10),
FOREIGN KEY (CID) REFERENCES CUSTOMER(CID));
What does the following statement mean?
CREATE TABLE Item
( Icode char(5) NOT NULL,
Desc char(5) NOT NULL,
ROL int,
QOH int,
CHECK( ROL < QOH ),
UNIQUE (Icode, Desc) );
MYSQL FUNCTIONS
GROUP BY
GROUP BY Country;
GROUP BY
eg
SELECT COMPANY, count(*) FROM SONY
GROUP BY Company;
SELECT Company, max(Price),Min(Price) FROM SONY
GROUP BY Company;
Sno | Item | Qty | Price | Company | Supcode | City |
1 | Pen Drive | 70 | 300 | Dell | S109 | Delhi |
2 | Camera | 50 | 4500 | Lenovo | S102 | Delhi |
3 | Laptop | 100 | 35000 | Lenovo | S112 | Delhi |
4 | Car GPS | 30 | 12000 | Dell | S103 | Delhi |
HAVING
SELECT AVG(marks) FROM STUDENT
GROUP BY Grade
HAVING GRADE=‘E’;
SELECT JOB,COUNT(*) as EMPLOYEE
FROM EMPLOYEE
GROUP BY JOB
HAVING COUNT(*) < 3;
EMPLOYEE | JOB |
5 | DOCTOR |
2 | ENGINEER |
4 | CA |
1 | ARCHITECT |
7 | TEACHER |
SELECT JOB,COUNT(*) FROM EMPLOYEE
GROUP BY JOB
HAVING COUNT(*) < 3;
EMPLOYEE | JOB |
5 | DOCTOR |
2 | ENGINEER |
4 | CA |
1 | ARCHITECT |
7 | TEACHER |
SELECT Item, 2*Price FROM SONY;
Sno | Item | Qty | Price | Company | Supcode | City |
1 | Pen Drive | 70 | 300 | Dell | S109 | Delhi |
2 | Camera | 50 | 4500 | Lenovo | S102 | Delhi |
3 | Laptop | 100 | 35000 | Lenovo | S112 | Delhi |
4 | Car GPS | 30 | 12000 | Dell | S103 | Delhi |
DELETE
DELETE FROM SONY
WHERE Id=121;
To remove all the items from the table
DELETE FROM SONY;
To delete the table
DROP TABLE SONY;
REVISE
UPDATE TABLE
syntax
UPDATE <TABLE NAME>
SET <COL NAME>= VALUE;
For eg change all the price from table sony to 2000
UPDATE SONY
SET Price=2000;
update sony
set Price=Price*2
where qty>30;
update sony
set Price=5000,Qty=100
where Company=‘Dell’;
syntax is
ALTER TABLE <table name>
MODIFY (<COL> new data type(new size));
eg
ALTER TABLE EMP
MODIFY (Job char(30));
ALTER TABLE
ALTER TABLE <table name>
ADD <COL><DATATYPE><SIZE>;
ALTER TABLE <table name>
DROP <COL>; //Deleted column
EG
ALTER TABLE SONY
ADD Discount int(3);
DATE DATA TYPE
select curdate();
USING DATE DATA TYPE
CREATE TABLE Trial
( sno int PRIMARY KEY,
DOB date);
INSERT INTO Trial VALUES
(1,’2000-12-1’),
(2,’2004-3-2’);
`
SELECT name, Date_format(DOB,’%d-%b-%y’) AS DOB from Trial;
DATA DEFINITION LANGUAGE
DATA MANIPULATION LANGUAGE
TCL
THE RELATIONAL ALGEBRA
The relational algebra include
SELECT OPERATION
select * from student
where roll<12
PROJECT OPERATION
eg:
select name from student;
THERE CAN BE MANY TYPES OF JOIN
or
CARTESIAN PRODUCT �
CARTESIAN PRODUCT
SELECT * FROM T,S;
EQUI- JOIN
NATURAL JOIN
UNION-MULTIPLE QUERIES CAN BE COMBINED TO ONE AS�
Ans: Select ConsumerName from Consumer
where Address = “Delhi”;
2. To display the details of stationary whose Price is in the range of 8 to 15(Both value included).
select * from stationary
where price between 8 AND 15;
4.To display the ConsumerName and StationaryName in ascending order of ConsumerName
select ConsumerName, Stationary Name
From Stationary S, Consumer C
where S.S_ID=C.S_ID
ORDER BY ConsumerName;
5. To display the ConsumerName,Address from Table Consumer and Company and Price from table Stationary
SELECT ConsumerName, Address,Company, Price from Stationary, Consumer
where Stationary.S_ID=Consumer.S_ID;�
DRAW THE OUTPUT OF THE SQL QUERIES
Stationary.StationaryName,
Stationary.Price from Stationary,Consumer
WHERE Consumer.S_ID=Stationary.S_ID;
4. SELECT StationaryName, Price*3 FROM Stationary;
VIEW
CREATE VIEW
AS SELECT *
FROM employee
WHERE GROSS >8000;
Now this view will be having all the details of the employees that have gross more than 8000.
Now this view can be used just like any other table.
SELECT * FROM tax;
DROP VIEW tax
DUAL TABLE
SOME BUILT IN FUNCTIONS
FUNCTION | COMMAND | OUTPUT |
LOWER | SELECT LOWER(“HELLO”) FROM DUAL;
| hello |
UPPER | SELECT UPPER(“hello”) FROM DUAL; | HELLO |
REPLICATE | SELECT REPLICATE(“&*”,4) FROM DUAL; | &*&*&*&* |
SUBSTR | SELECT SUBSTR(“COMPUTER”,4,3) FROM DUAL; | PUT |
GETDATE(), SYSDATE() | | |
QUESTIONS�
a) relation b) tuple c) Attribute
QUESTIONS BASED ON THE TABLE