0% found this document useful (0 votes)
17 views

Unit 2 - DATABASE CONCEPTS AND SQL 2

Uploaded by

josefbreh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Unit 2 - DATABASE CONCEPTS AND SQL 2

Uploaded by

josefbreh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Unit 2

Database concepts
and the
SQL
Data - it can be of any form (numbers, text, graphs, pictures etc)
A file can be understood as a container to store data in a computer.

Limitations of File System


● Data Redundancy - Repetition of Data, Memory wastage
● Data Inconsistency - Multiple File formats, Duplication of Data
● Data Isolation - Difficulty in accessing data from various files
● Data Integrity - Difficulty in maintaining Data Accuracy
● Data Security - Security and privacy cannot be maintained. Authorization
levels cannot be restricted.

Database - It is an organised collection of data which is interrelated


and stored together to serve many applications.
DBMS - Database Management System
A database management system (DBMS) or database
system in short, is a software that can be used to create
and manage databases

Database - It is an organised collection of data which is interrelated


and stored together to serve many applications.
GUI - Graphical User Interface
CLIENT —>SERVER
Properties of a Relation
● All the rows of a relation must be distinct (No duplicates)

● Ordering of rows and columns can be different

● For a row a column cannot have more than one value


● Columns must be unique
● A special value NULL is used to represent values that are
unknown or non-applicable to certain attributes
Referential Integrity
Is a system of rules that a DBMS uses to ensure that
relationships between records in related tables are valid and
the users don't accidentally delete or change related data.
Advantages of RDBMS

● Controls Data Redundancy


● Data Consistency - changes are reflected
● Sharing of Data
● Improved Data Integrity - validity of data
● Privacy and Security - protects data
● Backup and Recovery
Database Keys
Primary Key - The PRIMARY KEY uniquely identifies each
record in a table.

Candidate Key - The CANDIDATE KEY is one that is capable


of becoming the primary key.

Alternate Key - The Candidate Key that is not the primary key

Foreign Key - The PRIMARY KEY of one table becomes a


foreign key when it is referenced in another table
Properties of Primary Key
● It should be unique - cannot have a duplicate
● It should not be NULL
● A table can have only ONE primary key

Properties of Candidate Key


● A attribute that is eligible to become a primary key
● A table can have ONE/MORE Candidate Key

Properties of Alternate Key


● Also called the Secondary Key
● It is a column or set of columns that can act as a primary key but
is not selected as a primary key
Properties of Foreign Key
● A primary key of one table when is accessed in another table it
becomes a foreign key
● A foreign key can accept NULL value
SQL
Structured Query Language
Structured Query Language (SQL) is the most
popular query language used by major Relational
Database Management Systems such as MySQL,
ORACLE, SQL Server

● MySQL is an open source RDBMS software


● SQL is case insensitive.
● Always end SQL statements with a semicolon (;).
● Developed by Edgar Frank Codd - 1970 (IBM)
Advantages of using SQL

● Easy Data Retrieval and Manipulation - SQL commands


used
● Data Security - unauthorised access.
● Scalability - can handle huge about of data
● Data integrity - precise and consistent data
● Flexibility in Querying - retrieve data by different queries
● Cost-effective - it’s a free and open source software
Data type of the Attribute/column
classified into three categories.
● Numeric - INT, TINYINT, BIGINT, FLOAT
● Date and Time - DATE, TIME, DATETIME, TIMESTAMP
● String - CHAR, VARCHAR, TEXT
Numeric
● Each INT(Integer) value occupies 4 bytes of storage. The range of
values allowed in integer type are -2147483648 to 2147483647.
E.g: age int(2)
● For values larger than that, we have to use BIGINT, which occupies
8 bytes
● FLOAT – Holds numbers with decimal points. Each FLOAT
value occupies 4 bytes..

E.g: Marks float(4,2)


78.25

Date and Time


DATE - YYYY-MM-DD
TIME - HH:MM:SS
DATETIME - YYYY-MM-DDHH:MM:SS
String

CHAR VARCHAR
stores characters of fixed length. stores characters of variable size.

is used when the length of data is is used when the length of data is
known so that we declare the field unknown.
with the same length.
could be any value from 0 to 255 could be any value from 0 to 65535

Padding unused spaces creates No memory wasted


memory wastage
DDL - Data Definition Language
Performs task related to the structure of the table
CREATE, ALTER, RENAME, DROP

DML - Data Manipulation Language


Used to manipulate data in the table
SELECT, INSERT, UPDATE, DELETE

DCL - Data Control Language


Used for security purpose
GRANT, REVOKE

TCL - Transaction Control Language


Used for manage transactions within the database
ROLL BACK, COMMIT, SAVE POINT
DDL Commands
CREATE

To create a Database
CREATE DATABASE db_name;
To show all existing Databases
SHOW DATABASES;
To use a Database / open an existing database
USE db_name;
To show all existing Tables
SHOW TABLES;

To create a Table
CREATE TABLE table_name
(
Col1 datatype(size) constraint,
Col2 datatype(size) constraint,
:
Coln datatype(size) constraint
);
E.g. CREATE TABLE Student
(
RNo int Primary Key,
Name varchar(12),
Class varchar(5),
House varchar(10),
DOB Date
);

To view the structure of the Table created


DESC table_name; (OR)
DESCRIBE table_name;
Constraints
Constraints are certain types of restrictions on the data values that
an attribute can have. They are used to ensure the accuracy and
reliability of data.

● PRIMARY KEY - The column which can uniquely identify each


row or record in a table
● UNIQUE - Ensures that all the values in a column are
distinct/unique.
● DEFAULT - A default value is specified for the column if no
value is provided
● NOT NULL - Ensures that a column cannot have NULL values.
● CHECK - The CHECK constraint is used to limit the value
range that can be placed in a column..
CREATE TABLE Student
(
RNo int Primary Key,
Reg_no int Unique,
Name varchar(12) Not Null,
Class varchar(5),
House varchar(10),
Marks float(4,2) default 15.0,
DOB Date check (DOB between ‘2012-01–01’ and ‘2018-01-01’)
);
ALTER

we can change or alter the structure of the table by using


the ALTER statement.

ALTER TABLE tablename ADD/Modify/DROP Col1,


Col2,..
Add a primary key to a table
ALTER TABLE Student ADD PRIMARY KEY SID;

Add a column to a table


ALTER TABLE Student ADD ADDRESS VARCHAR(20);

Modify the datatype of a column in a table


ALTER TABLE Student MODIFY ADDRESS VARCHAR(15);
Change a column name of the table
ALTER TABLE Student Name - Old
CHANGE Name SName varchar(20); SName - New

Delete a column from the table


ALTER TABLE Student DROP ADDRESS;

Remove Primary Key from the table


ALTER TABLE Student DROP PRIMARY KEY;
DROP
We can use DROP statement to remove a database or a
table permanently from the system. Once deleted it cannot
be undone.

Delete a table permanently


DROP TABLE table_name;

Delete a database permanently


DROP DATABASE db_name;
DML Commands
INSERT
INSERT INTO statement is used to insert new records in a
table.

INSERT INTO tablename VALUES(value 1, value 2,....);

we need not to specify attribute names in insert statement if there


are exactly same number of values in the INSERT statement as the
total number of attributes in the table.
E.g:
INSERT INTO Student(RNo, NAME) VALUES(103, ’Sania’);

CREATE TABLE Student


(
RNo int Primary Key,
Name varchar(12),
Class varchar(5),
House varchar(10),
DOB Date
);

INSERT INTO Student VALUES(103, ’Sania’, ‘XI-M’, ‘Mars’,


‘2012-07-21’);
To INSERT multiple records at the same time we can use
single INSERT statement as follows:

INSERT INTO Student VALUES


(103, ’Sania’, ‘XI-M’, ‘Mars’, ‘2012-07-21’),
(104, ’Robin’, ‘XI-B’, ‘Saturn’, ‘2013-08-21’),
(107, ’John’, ‘XI-C’, ‘Jupiter’, ‘2011-05-27’);

Note:

Varchar, Char and Date should be in quotes.


SELECT
The SQL statement SELECT is used to retrieve data from the
tables in a database and is also called query statement. The
output is displayed in a tabular form.

SELECT Col1, Col2, ...


FROM table_name
WHERE condition;
E.g:
SELECT Name, House FROM Student
WHERE RNo = 104 ;
To display all the columns
SELECT * FROM Student
WHERE RNo = 104 ;

To display selected columns


SELECT Name, House FROM Student
WHERE RNo = 104 ;

To display entire table


SELECT * FROM Student;
To rename a column
If we want to rename any column while displaying the
output, we can do so by using alias 'AS'

SELECT Name AS Stud_name FROM Student;


Display names of all students along with their bonus mark (mark + 15)

SELECT Name, mark+15 FROM Student;


Name mark+15
Sania 78

Distinct Clause Donald 86

The SELECT statement when combined with DISTINCT


House
clause, returns records without repetition/duplicates
Mars

Jupiter
SELECT DISTINCT House FROM Student;
Saturn
SELECT ALL House FROM Student;
(Includes duplicates)
Where Clause
The WHERE clause is used to retrieve data that meet some
specified conditions
SELECT * FROM EMPLOYEE WHERE Salary > 5000 AND
DeptId = 'D04';

Q. display name and department number of all those employees who are
earning salary between 20000 and 50000 (both values inclusive).

SELECT Ename, DeptId FROM EMPLOYEE WHERE


Salary>=20000 AND Salary<=50000;
Between operator
SELECT Ename, DeptId FROM EMPLOYEE WHERE
Salary BETWEEN 20000 AND 50000;
(It includes 20000 and 50000)
OR operator
Display details of all the students who belong to either Mars,
Jupiter or Saturn

SELECT * FROM Student WHERE House = ‘Mars’ OR


House = ‘Jupiter’ OR House = ‘Saturn’ ;
Membership operator - IN
Displays details of all the students who belong to either Mars,
Jupiter or Saturn

SELECT * FROM Student WHERE House IN (‘Mars’, ‘Jupiter’,‘Saturn’) ;

Membership operator - NOT IN


Displays details of all the students who do not belong to either Mars,
Jupiter or Saturn

SELECT * FROM Student WHERE House NOT IN (‘Mars’, ‘Jupiter’,‘Saturn’) ;


Order by Clause
Sorts the order of a specified column either in ascending or descending
order. Ascending order by default

SELECT * FROM Student Order by Name ASC;


OR
SELECT * FROM Student Order by Name;

SELECT * FROM Student Order by Marks DESC;


NULL values
SQL supports a special value called NULL to represent a
missing or unknown value. It is important to note that NULL
is different from 0. [ 5 + NULL = NULL not 5]

SELECT * FROM EMPLOYEE WHERE Bonus IS NULL;


(Who did not get bonus)
SELECT * FROM EMPLOYEE WHERE Bonus IS NOT NULL;

(Who got bonus)


Note: It is not possible to test for NULL values
with comparison operators (=, < or <>)
Substring Pattern Matching

SQL provides LIKE operator that can be used with WHERE


clause to search for a specified pattern in a column. 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
Q. Display details of all those Students whose name starts
with 'K'.
SELECT * FROM Student WHERE Name LIKE 'K%';

Q. Display details of all those Students whose name ends


with 'K'.
SELECT * FROM Student WHERE Name LIKE '%K';
Q. Display details of all those Students whose name has 'K'
as substring.
SELECT * FROM Student WHERE Name LIKE '%K%';
Q. Display details of all those employees whose name consists
of exactly 5 letters and starts with any letter but has ‘ANYA’
after that.

SELECT * FROM EMPLOYEE WHERE Name LIKE


'_ANYA';
Q. Displays names of all employees containing 'a' as the
second character

SELECT * FROM EMPLOYEE WHERE Name LIKE '_a%';


Q. Display details of all those employees whose name consists
of exactly 5 letters

SELECT * FROM EMPLOYEE WHERE Name LIKE '_ _ _ _ _';

DUAL
DUAL is 1 row, 1 column table present by default in all Oracle
databases.
5*10
SELECT 5*10;
50
UPDATE
The UPDATE statement is used to make such modifications in the
existing data.

UPDATE table_name
SET Col1 = val1, Col2 = val2, …
WHERE condition;

UPDATE Student UPDATE Student


SET House = ‘Mars’ SET Marks = Marks+10
WHERE RNo = 104; WHERE Marks < 50;
DELETE

The DELETE statement is used to delete one or more record(s)


from a table.
DELETE FROM table_name
WHERE condition;

DELETE FROM Student DELETE FROM Student


WHERE Rno = 104; WHERE Name = ‘Sanya’;
MySQL
Functions
MATH FUNCTIONS
MOD( )
This function returns modulus (remainder) of given two numbers.

Q. Find out the remainder of 11 divided by 4. Modulus

3
SELECT MOD(11,4) Modulus;

SELECT MOD(10.5,3) Modulus; Modulus

1.5
POWER( ) / POW( )
n th
This function returns m . i.e. a number m raised to the n power.

SELECT POWER(3,2) Result; Result

9
SELECT POW(3,2) Result;

ROUND( )
This function returns a number rounded off as per given instructions

Q. Round off value 15.193 to one decimal place Result

SELECT ROUND(15.193,1) Result; 15.2


Q. Round off value 15.193 to nearest tens
Result Result
SELECT ROUND(15.193,-1) Result;
SELECT ROUND(12.193,-1) Result; 20 10

Q. Round off value 15.193 to nearest hundreds


Result
SELECT ROUND(375.415, -2) Result;
400

Result
SELECT ROUND(325.415, -2) Result;
300

Note: the `ROUND` function with a negative second argument rounds the number to
the left of the decimal point. In this case, -1 specifies rounding to the nearest ten's
place. Since 15.193 is closer to 20 than 10, the rounded result is 20.
Q. Round off value 15.193 to 2 decimal places Result

SELECT ROUND(15.193,2) Result; 15.19

SELECT ROUND(29.21,1) Result; 29.2

SELECT ROUND(32.76,1) Result; 32.8

SELECT ROUND(29.21) Result; 29

SELECT ROUND(32.76) Result; 33


TEXT FUNCTIONS

UCASE( ) / UPPER( )
Result
This function converts the given string into uppercase.
SELECT UPPER(‘Hello’) Result; HELLO
SELECT UCASE(‘Hello’) Result;

LCASE( ) / LOWER( )
This function converts the given string into lowercase. Result

SELECT LOWER(‘Hello’) Result; hello

SELECT LCASE(‘Hello’) Result;


SUBSTRING( ) / SUBSTR( ) / MID( )
This function extracts a substring from a given string.
str - given string
SUBSTR( str, m, n) m - beginning position of the substring
n - number of characters to be extracted from the
beginning position (m)

SELECT SUBSTR(‘WELCOME’, 3, 4) Result; Result

SELECT MID(‘WELCOME’, 3, 4) Result; LCOM


SELECT SUBSTRING(‘WELCOME’, -5, 4) Result;
SUBSTR(NAME,1,3)
Q. First three characters of the student names.
ANA
SELECT SUBSTR(NAME, 1, 3)
FROM Student BIN
WHERE SID = 103 or SID = 104;
LENGTH( )
This function returns the length of a given string in bytes. The blank
spaces in the str are also counted. If the str is null function returns null.

LENGTH( str)
length
SELECT LENGTH(‘WELCOME’) length;
7

SELECT Name, LENGTH(Name) AS ‘Name Length’


FROM Student
WHERE SID = 102 or SID = 103; Name Name Length
Meera 5
Pratyush 8
RIGHT( )
This function returns the rightmost number of characters as specified.
Returns NULL if the argument is NULL.
RIGHT(str, len)
SELECT RIGHT(‘WELCOME’, 4) Result; Result

COME
LEFT( )
This function returns the leftmost number of characters as specified.
Returns NULL if the argument is NULL.
Result
LEFT(str, len)
SELECT LEFT(‘WELCOME’, 4) Result; WELC
INSTR( )
This function searches for a substring in a given string and returns the
position of its first occurrence.
Result
INSTR(str, substr)
6
SELECT INSTR(‘WELCOME’, ‘ME’) Result;
Name Result

Meera 0
SELECT Name, INSTR(Name, ‘at’) AS Result
Pratyush 3
FROM Student;
Sathya 2
LTRIM( )
This function removes leading spaces from the left of a given string.

LTRIM(str) Result

SELECT LTRIM(‘ WELCOME’) Result; WELCOME

RTRIM( )
This function removes trailing spaces from the right of a given string.

RTRIM(str) Result

SELECT RTRIM(‘WELCOME ’) ‘Result’; WELCOME


TRIM( )
This function removes leading spaces and trailing spaces from the right
and left of a given string.
Result

TRIM(substr FROM str) WCOME


Result

WELCOME###
SELECT TRIM(‘EL’ FROM ‘WELCOME’) AS Result;

SELECT TRIM(leading ‘#’ FROM ‘### WELCOME###’) AS Result; Result

SELECT TRIM(trailing ‘#’ FROM ‘### WELCOME###’) AS ‘Result’; ###WELCOME

Result
SELECT TRIM(both ‘#’ FROM ‘### WELCOME###’) AS ‘Result’;
WELCOME
DATE FUNCTIONS

CURDATE( ) / CURRENT_DATE( ) CURDATE()

This function returns the current date 2024-08-08

SELECT CURDATE( );

DATE( )
This function extracts the date part of a date or datetime Result
expression.
2022-12-31
SELECT DATE(‘2022-12-31 01:02:03’ ) AS Result;
MONTH( )
Result
This function returns the month from the date argument,
12
in the range 1 to 12

SELECT MONTH(‘2022-12-31’ ) AS Result;

MONTHNAME( )
Result
This function returns the month from the date argument
September
SELECT MONTHNAME(‘2022-09-31’ ) AS Result;
YEAR( )
Result
This function returns the year from the date
argument, in the range 1000 to 9999 2022

SELECT YEAR(‘2022-09-30’ ) AS Result;

DAY( )
Result
This function returns day part of the date.
29
SELECT DAY(‘2022-09-29’ ) AS Result;
DAYNAME( )
Result
This function returns name of the day of a week.
Wednesday
SELECT DAYNAME(‘2024-08-07’) AS Result;

NOW( )
Result
This function returns the current date and
time.("YYYY-MM-DD HH-MM-SS") 2024-08-07 19:14:08
SELECT NOW( ) AS Result;
AGGREGATE FUNCTIONS
MAX( )

This function returns the maximum value from a given column or


expression.
SELECT MAX(SALARY) “MAXSALARY;

MIN( )

This function returns the minimum value from a given column or


expression.
SELECT MIN(SALARY) “ MINSALARY”;
SUM( )

This function returns the sum of values from a given column or


expression.
SELECT SUM(SALARY) AS Result
FROM EMPLOYEE;

AVG( )
This function returns the average of values from a given column or
expression.
SELECT AVG(MARKS) “Average”
FROM Student;
COUNT( )

This function counts the number of rows in a given column or


expression.
Q. Count no.of records in table Employee.
SELECT COUNT(*) “No of Employees”
FROM Employee;

Q. Count no.of jobs in table Employee.


SELECT COUNT(JOB) “No of Jobs”
FROM Employee;
Q. Count no.of distinct jobs in table Employee.
SELECT COUNT(DISTINCT JOB) “Distinct Jobs”
FROM Employee;

COUNT(*) - this function returns all rows including duplicates and


nulls.

COUNT(DISTINCT/ALL column) - this function returns all rows


where expression is not null. ALL rows are counted or
DISTINCT rows are counted..
GROUP BY CLAUSE
The GROUP BY clause combines all those records that have
identical values in a particular column or a group of columns.

Q. To calculate the number of employees in each grade


JOB COUNT(*)
SELECT JOB, COUNT (*)
FROM Employee Analyst 2
GROUP BY JOB; Clerk 5
Manager 3
Salesman 4
Q. To calculate the number of departments and group them
and find the sum of salary of the employees in that
department.

SELECT DEPTNO, COUNT (*),SUM(SAL)


FROM Employee
GROUP BY DEPTNO;
DEPTNO COUNT(*) SUM(SAL)

10 3 8750.00
14 5 10885.00
20 4 12678.00
24 6 9400.00
Q. To calculate the number of employees in each department

SELECT DEPTNO, COUNT (EMPNO) DEPTNO COUNT(EMPNO)


FROM Employee 10 2
GROUP BY DEPTNO; 20 5
30 3
HAVING CLAUSE
The HAVING clause places conditions on groups. The WHERE
clause places condition on individual rows. WHERE cannot include
aggregate functions (SUM, MAX,MIN,AVG, COUNT). The
HAVING clause can do so.

Q. To calculate the average gross and total gross for


employees belonging to ‘E4’ grade

SELECT AVG(GROSS), SUM(GROSS),


FROM EMPLOYEE
GROUP BY GRADE
HAVING GRADE =’E4’;
Q. To display the jobs where the number of employees
is less than 3.

SELECT JOB, COUNT(*)


FROM EMPLOYEE
GROUP BY JOB
HAVING COUNT(*)<3;

Logical operators in having clause - combine conditions

SELECT DEPTNO, AVG(COMM), AVG (SAL),


FROM EMPLOYEE
GROUP BY DEPTNO
HAVING AVG(COMM)>750 AND AVG (SAL)>2000;
Table:orders

SELECT product_id, SUM(quantity) as total_quantity


FROM orders
GROUP BY product_id
HAVING SUM(quantity) > 100;
HAVING COUNT(*)<=3;

HAVING JOB IN (‘CLERK’,’SALESMAN’);

HAVING SUM(SAL) BETWEEN 3000 AND


7000;
ORDER BY CLAUSE
ASC / DESC

Sorts the order of a specified column either in ascending or


descending order. Ascending order by default

SELECT * FROM Student Order by Name ASC;


OR
SELECT * FROM Student Order by Name;

SELECT * FROM Student Order by Marks DESC;


Equi - Join

Equi join in SQL is used mostly to join two or more


tables on the basis of equality of column values of the
tables.

SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
Table : Product Table : Discount

id prod_name id dis_percent
1 Iwatch 2 20
2 IPhone 4 17
3 IPod
4 macbook

id prod_name id dis_percent
SELECT * 2 IPhone 2 20
FROM Product, Discount
4 macbook 4 17
WHERE Product.id = Discount.id;
● Equi join in SQL can be written with WHERE as well as
ON clause.

SELECT column_list SELECT column_list


FROM table1, table2.... FROM table1
WHERE JOIN table2
table1.column_name = ON column_name =
table2.column_name; table2.column_name;
SELECT FUEL, AVG(QT1)
FROM CAR_SALES
GROUP BY FUEL;

SELECT SEGMENT, MAX(QT2)


FROM CAR_SALES
GROUP BY SEGMENT;

SELECT *
FROM CAR_SALES
ORDER BY QT2 DESC;
a. SELECT LEFT(COLOR, 3) FROM Cars WHERE
COLOR='Blue' OR COLOR='Black' OR COLOR='Brown';

b. UPDATE Cars SET Color = 'Green' WHERE CarID = 103;

c. Number of tuples: 6
Primary key column: CarID
I. SELECT SUBSTRING('Database Management System', 10, 6);

II. SELECT INSTR('Database Management System', 'base');


SELECT YEAR(MIN(transaction_date)) AS oldest_year
FROM BLOCKCHAIN;

SELECT MONTH(MAX(transaction_date)) AS most_recent_month


FROM BLOCKCHAIN;

SELECT *
FROM BLOCKCHAIN
WHERE MONTH(transaction_date) = 5;

SELECT COUNT(*) AS total_transactions_2022


FROM BLOCKCHAIN
WHERE YEAR(transaction_date) = 2022;

You might also like