Module No - 4 DBMS
Module No - 4 DBMS
For fixed length strings, a shorter string is padded with blank characters to the right. For
example, if the value ‘Smith’ is for an attribute of type CHAR(10), it is padded with five blank
characters to become ‘Smith ’ if needed. Padded blanks are generally ignored when strings are
compared.
Data Types
CHAR and VARCHAR
String data types of either fixed length or variable length.
Data Types
Bit-string data types
fixed length n—BIT(n)—or
varying length—BIT VARYING(n), where n is the maximum number of bits.
➢ The default for n, the length of a character string or bit string, is 1. Literal bit strings
are placed between single quotes but preceded by a B to distinguish them from
character strings; for example, B‘10101’.
➢ Another variable-length bitstring data type called BINARY LARGE OBJECT or BLOB
is also available to specify columns that have large binary values, such as images
Data Types
A Boolean data type has the traditional values of TRUE or FALSE.
In SQL, because of the presence of NULL values, a three-valued logic is used,
so a third possible value for a Boolean data type is UNKNOWN.
Data Types
Data Types
➢ The DATE data type has ten positions, and its components are YEAR, MONTH, and
DAY in the form YYYY-MM-DD.
➢ The TIME data type has at least eight positions, with the components HOUR,
MINUTE, and SECOND in the form HH:MM:SS.
Literal values are represented by single quoted strings preceded by the keyword
TIMESTAMP, with a blank space between data and time; for example, TIMESTAMP ‘2008-
09-27 09:12:47.648302’
DDL
Example:
CREATE TABLE EMPLOYEE
(Name VARCHAR(20),
Email VARCHAR(100),
DOB DATE);
DDL
DROP: It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE TABLE-NAME;
Example
DROP TABLE EMPLOYEE;
DDL
ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
Example:-
ALTER TABLE Employee ADD Phone_no INT
DDL
Syntax:
To modify existing column in the table:
ALTER TABLE TABLE_NAME MODIFY(COLUMN DEFINITION....);
Example:-
ALTER TABLE Employee MODIFY Phone_no VARCHAR(10)
DDL
Syntax:
To DROP existing column in the table:
ALTER TABLE TABLE NAME DROP(COLUMN NAME....);
Example:-
ALTER TABLE Employee DROP Phone_no
DDL
TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
Syntax: TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE
MODULE NO:-4 Introduction to Database Concepts:
➢ To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
Foreign key
Primary key
Integrity Constraints
Referential Integrity Constraints
➢ A referential integrity constraint is specified between two tables .
➢ In the Referential integrity constraints, if a foreign key in Table 1 refers to
the Primary Key of Table 2, then every value of the Foreign Key in Table 1
must be null or be available in Table 2.
Integrity Constraints
Integrity Constraints
SQL FOREIGN KEY on CREATE TABLE
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders"
table is created:
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Data Manipulation Language
Example:-
INSERT INTO employee VALUES ('RAVI','[email protected]','1990-10-18’)
Or
INSERT INTO `employee`(`Name`, `Email`, `DOB`)
VALUES ('RAVI','[email protected]','1990-10-18')
Data Manipulation Language
Data Manipulation Language
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name
SET [column_name1= value1,...column_nameN = valueN]
[WHERE CONDITION]
Example:-
UPDATE employee
SET Email=‘[email protected]’
Where Name=‘SANJAY’
Data manipulation language
Example:-
DELETE FROM employee where Name=‘SANJAY’
Example:-
DELETE FROM employee
MODULE NO:-4 Introduction to Database Concepts:
➢ Queries are SQL functions that help us to access a particular set of records from a
database table.
➢ We can request any information or data from the database using the clauses or, let’s
say, SQL statements.
➢ For example, SQL Clauses receives a conditional expression that can be a column
name or valid term involving columns where this supports the MySQL functions to
calculate the result values for a table in the database.
Data Query language
What are Clauses in SQL?
➢ Clauses are in-built functions available to us in SQL.
➢ With the help of clauses, we can deal with data easily stored in the table.
➢ Clauses help us filter and analyze data quickly.
➢ When we have large amounts of data stored in the database, we use
Clauses to query and get data required by the user.
Data Query language
There are generally five kinds of SQL Clauses in MySQL Server. They
are listed as follows:
•WHERE Clause
•ORDER BY clause
•HAVING Clause
•TOP Clause
•GROUP BY Clause
Data Query language
Where Clause in SQL
We use the WHERE clause to specify conditionals in our SQL query. Where clause can be
used in the update and delete statements as well as to perform operations on the desired
data.
Rules and Usage:
1. Rules:
When using a Where clause, we need to mention at least one condition.
2. Usage:
•Used to query and transact the database.
•Used with an update and delete statements to make sure the right data points are deleted.
Data Query language
Syntax
Example 1: Let us find the details of all employees who earn more than 25,000 and are
above 25.
Select * from employee WHERE age>25 and salary >25000 ;
Data Query language
And Clause in SQL
We use And clause while specifying multiple conditions together in a query with the Where
clause.
Rules and Usage:
1. Rules:
•When using an And clause, we need to mention at least two conditions(the result would
satisfy both.)
2. Usage:
•Used to query and transact the database.
•Used with an update and delete statements to make sure the right data points are deleted.
•Returns a data point only if all conditions meet the requirements.
Data Query language
Syntax:
Example 1: Let us find the details of employees whose age is between 22 to 28 and earn
less than 30,000.
Syntax:
Example 1: Let us find the employees with age more than 26 or a salary more
than 30000
Syntax :
Example 1: Let us find the details of employees whose name starts with A.
Syntax:
Example 1: Let us view the first 5 rows of data from our table
Syntax:
Syntax
HAVING clause
➢ A HAVING clause restricts the results of a GROUP BY in a SelectExpression.
➢ The HAVING clause is applied to each group of the grouped table, much as a
WHERE clause is applied to a select list.
➢ If there is no GROUP BY clause, the HAVING clause is applied to the entire result
as a single group.
➢ The SELECT clause cannot refer directly to any column that does not have a
GROUP BY clause. It can, however, refer to constants, aggregates, and special
registers.
Data Query language
HAVING Clause utilized in SQL as a conditional Clause with GROUP BY Clause.
This conditional clause returns rows where aggregate function results matched with
given conditions only.
It added in the SQL because WHERE Clause cannot be combined with aggregate results,
so it has a different purpose.
The primary purpose of the WHERE Clause is to deal with non-aggregated or individual
records.
•HAVING Clause always utilized in combination with GROUP BY Clause.
•HAVING Clause restricts the data on the group records rather than individual records.
•WHERE and HAVING can be used in a single query.
Data Query language
Table is grouped based on DeptID column and these grouped rows filtered using HAVING
Clause with condition AVG(Salary) > 3000.
DatainQuery
Order which language
clauses can be applied
SELECT column_name(s)
FROM table_name
WHERE condition(s)
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Data Query language
Example 1: Let us first find employees whose age, if doubled, will be more than 50.
Query: SELECT * FROM employee WHERE age*2 > 50 ;
Data Query language
SELECT *
FROM EMPLOYEE
WHERE SSN in
(SELECT MGR_SSN
FROM DEPARTMENT
WHERE DNAME='Research’ )
Data Query language
Examples:
Q1: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE , DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO
could be written as:
Q1: SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO)
WHERE DNAME='Research’
or as:
Q1: SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE NATURAL JOIN DEPARTMENT AS DEPT(DNAME, DNO,
MSSN, MSDATE)
WHERE DNAME='Research’
Data Query
Operators language
in Where Clause
Data Query
Operators language
in Where Clause
Data Query language
Find the names of all instructors whose salary is greater than the salary of all
instructors in the Biology department
select name
from instructor
where salary > all (select salary
from instructor
where dept name = ’Biology’);
Data Query language
Find names of instructors with salary greater than that of some (at least one)
instructor in the Biology department's
select name
from instructor
where salary > some (select salary
from instructor
where dept name = ’Biology’);
Data Query language
Delete all instructors whose salary is less than the average salary of instructors
Delete car
where model = ’Mazda’ and license in
(select license
from person p, owns o
where p.name = ’John Smith’
and p.driver-id = o.driver-id)
Data Query language
Update the damage amount for the car with license number “AABB2000” in the accident
with report number “AR2197” to $3000.
update participated
set damage-amount = 3000 where report-number = “AR2197”
and driver-id in (select driver-id
from owns
where license = “AABB2000”)
Data Query language
select employee-name
from works
where company-name = ’First Bank Corporation’
Data Query language
Find the names and cities of residence of all employees who work for First Bank Corporation
select *
from employee
where employee-name in (select employee-name
from works
where company-name = ’First Bank Corporation’ and salary > 10000)
Data Query language
Find all employees in the database who live in the same cities as the companies for which they
work.
Employee (employee-name, street, city)
Works (employee-name, company-name, salary)
Company (company-name, city)
Manages (employee-name, manager-name)
select e.employee-name
from employee e, works w, company c
where e.employee-name = w.employee-name and e.city = c.city
and w.company -name = c.company -name
Data Query language
Find all employees in the database who do not work for First Bank Corporation
Employee (employee-name, street, city)
Works (employee-name, company-name, salary)
Company (company-name, city)
Manages (employee-name, manager-name)
select employee-name
from works
where company-name ≠’First Bank Corporation’
Data Query language
Find all employees in the database who do not work for First Bank Corporation
select employee-name
from employee
where employee-name not in (select employee-name
from works
where company-name = ’First Bank Corporation’)
Data Query language
Find all employees in the database who earn more than every employee of Small Bank Corporation
select employee-name
from works
where salary > all (select salary
from works
where company-name = ’Small Bank Corporation’)
Data Query language
Find all employees who earn more than the average salary of all employees of their company.
Employee (employee-name, street, city)
Works (employee-name, company-name, salary)
Company (company-name, city)
Manages (employee-name, manager-name)
select employee-name
from works T
where salary > (select avg (salary)
from works S
where T.company-name = S.company-name)
Data Query language
Modify the database so that Jones now lives in Newtown..
update employee
set city = ’Newton’
where person-name = ’Jones’
Data Query language
Give all employees of First Bank Corporation a 10-percent raise
Employee (employee-name, street, city)
Works (employee-name, company-name, salary)
Company (company-name, city)
Manages (employee-name, manager-name)
update works
set salary = salary * 1.1
where company-name = ’First Bank Corporation’
Data Query language
Give all managers of First Bank Corporation a 10-percent raise.
Employee (employee-name, street, city)
Works (employee-name, company-name, salary)
Company (company-name, city)
Manages (employee-name, manager-name)
update works
set salary = salary * 1.1
where company-name = ’First Bank Corporation’ AND
employee-name in (select manager-name
from manages)
MODULE NO:-4 Introduction to Database Concepts:
TYPES OF VIEWS
➢ Simple views can only contain a single base table.
➢ Complex views can be constructed on more than one base table. In
particular, complex views can contain: join conditions, a group by
clause, a order by clause.
VIEWS IN SQL
VIEWS IN SQL
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name SQL CREATE VIEW Examples
WHERE condition;
Creates a view that shows all customers from Brazil:
Example:
If we want to delete the View MarksView, we can do this as:
There are certain conditions needed to be satisfied to update a view. If any one of these
conditions is not met, then we will not be allowed to update the view.
1.The SELECT statement which is used to create the view should not include GROUP BY clause
or ORDER BY clause.
2.The SELECT statement should not have the DISTINCT keyword.
3.The View should have all NOT NULL values.
4.The view should not be created using nested queries or complex queries.
5.The view should be created from a single table.
6.If the view is created using multiple tables then we will not be allowed to update the view.
VIEWS IN SQL
CREATE OR REPLACE VIEW
Syntax:
INSERT INTO view_name(column1, column2 , column3,..) VALUES(value1, value2, value3..);
VIEWS IN SQL
Inserting a row in a view
Insert a new row in the View DetailsView which we have created above in the
example of “creating views from a single table”.
INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("Suresh","Gurgaon");
VIEWS IN SQL
Deleting a row from a View:
Deleting rows from a view is also as simple as deleting rows from a table.
We can use the DELETE statement of SQL to delete rows from a view.
Also deleting a row from a view first delete the row from the actual table
and the change is then reflected in the view
VIEWS IN SQL
Syntax:
DELETE FROM view_name WHERE condition;
Example:
In this example we will delete the last row from the view DetailsView which we just added in the
above example of inserting rows.
Example:
In this example we will delete the last row from the view
DetailsView which we just added in the above example of
inserting rows.