Module4 Partial
Module4 Partial
Module4 Partial
MANAGEMEN
T SYSTEMS
Couse code:CSC403
Prof. Juhi Janjua
Module 4: • Overview of SQL
Slide
8- 12
SQL TRUNCATE table
• It is used to delete complete data from an existing table.
• You can also use DROP TABLE command to delete
complete table but it would remove complete table
structure form the database and you would need to re-
create this table once again if you wish to store some
data.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE retailers;
• Integrity constraints are a set of rules. It is used to maintain
the quality of information.
Integrity • It ensure that the data insertion, updating, and other
processes must be performed in such a way that data
constraints integrity is not affected.
• It is used to guard against accidental damage to the
database.
Constraints
• A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
CREATE TABLE Persons (
CREATE TABLE Persons (
ID int NOT NULL,
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
LastName
FirstName varchar(255),
varchar(255) NOT NULL,
Age int,
FirstName varchar(255),
PRIMARY KEY (ID)
Age int
);
);
• A missing WHERE-clause indicates no condition; hence, all tuples of the relations in the FROM-
clause are selected
• This is equivalent to the condition WHERE TRUE
Retrieve the SSN values for all employees.
SELECT SSN
FROM EMPLOYEE
• If more than one relation is specified in the FROM-clause and there is no join condition, then the
CARTESIAN PRODUCT of tuples is selected
ALIASES (contd.)
• An alternate form of INSERT specifies explicitly the attribute names that correspond to the values
in the new tuple
• Attributes with NULL values can be left out
• Example: Insert a tuple for a new EMPLOYEE for whom we only know the FNAME, LNAME, and
SSN attributes.
U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)
VALUES ('Richard', 'Marini', '653298653')
INSERT (contd.)
• Example: Suppose we want to create a temporary table that has the name, number of
employees, and total salaries for each department.
• A table DEPTS_INFO is created by U3A, and is loaded with the summary information
retrieved from the database by the query in U3B.
U3A: CREATE TABLE DEPTS_INFO
(DEPT_NAME VARCHAR(10),
NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);
• Note: The DEPTS_INFO table may not be up-to-date if we change the tuples in either the
DEPARTMENT or the EMPLOYEE relations after issuing U3B. We have to create a view
(see later) to keep such a table up to date.
DELETE
• Removes tuples from a relation
• Includes a WHERE-clause to select the tuples
to be deleted
• A missing WHERE-clause specifies that all
tuples in the relation are to be deleted; the
table then becomes an empty table
• The number of tuples deleted depends on the
number of tuples in the relation that satisfy
the WHERE-clause
DELETE (contd.)
• Examples:
U4A: DELETE FROM EMPLOYEE
WHERE LNAME='Brown’
• In this request, the modified SALARY value depends on the original SALARY value in each tuple
• The reference to the SALARY attribute on the right of = refers to the old SALARY value before
modification
• The reference to the SALARY attribute on the left of = refers to the new SALARY value after
modification
• Data Control Language (DCL) helps users to
retrieve and modify the data stored in the
database with some specified queries.
Data Control • DCL commands are used to grant and take back
Language(D authority from any database user.
CL) • Here are some commands that come under DCL:
• Grant
• Revoke
GRANT
• SQL Grant command is specifically used to provide privileges to database objects for an
user.
• This command also allows users to grant permissions for other users too.
Syntax:
GRANT privilege_name on object_name
to user_name
Example:
GRANT INSERT,SELECT on Department to U1
GRANT ALL PRIVELEGES on Employee to U2
REVOKE
• Revoke command withdraw user privileges on database objects if any granted. It does
operations opposite to the Grant command. When a privilege is revoked from a particular
user U, then the privileges granted to all other users by user U will be revoked.
Syntax:
REVOKE privilege_name on object_name from user_name
Example:
REVOKE INSERT on Department from U1
• Set operators combine the results of two
component queries into a single result.
• Queries containing set operators are called
compound queries
Set operations
Set operators(contd..)
UNION
• The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
• In the union operation, all the number of datatype and columns must be same in both the
tables on which UNION operation is being applied.
• The union operation eliminates the duplicate rows from its resultset.
Syntax:
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
UNION(contd..)
String
operations
String functions (contd..)
• ASCII(): This function is used to find the ASCII value of a character.
Syntax: SELECT ascii('t’) from dual;
Output: 116
• CHAR_LENGTH(): This function is used to find the length of a word.
Syntax: SELECT char_length('Hello!’);
For oracle: SELECT length('Hello!’) from dual;
Output: 6
• CONCAT_WS(): This function is used to add two words or strings with a symbol as concatenating
symbol.
Syntax: SELECT CONCAT_WS('_', ‘SQL’,’Programming');
Output: SQL_Programming
Note: for oracle method is concat(), it will concatenate two strings
String functions (contd..)
LOWER(): This function is used to convert the upper case string into lower case.
Syntax: SELECT LOWER(‘SQL PROGRAMMING’);
Output: sql programming
Example:
SELECT LOWER(FNAME) AS LowercaseEmployeeName
FROM Employee;
String functions (contd..)
UPPER(): This function is used to make the string in upper case.
Syntax: SELECT UPPER(‘sql programming’);
Output: SQL PROGRAMMING
Example:
SELECT UPPER(FNAME) AS UpperCaseEmployeeName
FROM Employee;
SQL LIKE operator
• The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
• There are two wildcards often used in conjunction with the LIKE operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one single character
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
SQL LIKE operator(contd..)
Example
Aggregate
functions
Aggregate functions(contd…)
COUNT()
• It is used to Count the number of rows in a database table.
• It can work on both numeric and non-numeric data types.
• COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
• COUNT(*) considers duplicate and Null.
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Example:
Select COUNT(*) from emp: Returns total number of records .i.e 6.
COUNT(salary): Return number of Non Null values over the column salary. i.e 5.
COUNT(Distinct Salary): Return number of distinct Non Null values over the column salary i.e. 4
Aggregate functions(contd…)
SUM()
• Sum function is used to calculate the sum of all selected columns.
• It works on numeric fields only.
Syntax:
SUM()
or
SUM( [ALL|DISTINCT] expression)
Example:
Select SUM(salary) as Sum from emp
Sum all Non Null values of Column salary i.e., 310
Select SUM(Distinct salary) from emp
Sum of all distinct Non-Null values i.e., 250.
Aggregate functions(contd…)
MAX()
• It is used to find the maximum value of a certain column.
• This function determines the largest value of all selected values of a column.
Syntax:
MAX()
or
MAX( [ALL|DISTINCT] expression )
Example:
SELECT MAX(Salary)
FROM emp;
Aggregate functions(contd…)
MIN()
• It is used to find the minimum value of a certain column.
• This function determines the smallest value of all selected values of a column.
Syntax:
MIN()
or
MIN( [ALL|DISTINCT] expression )
Example:
SELECT MIN(Salary)
FROM emp;
Aggregate functions with GROUP BY
• SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in
the SELECT-clause.
• In many cases, we want to apply the aggregate functions to subgroups of tuples in a relation.
Syntax:
SELECT column_name(s), aggregate_function (aggregate_expression)
FROM table_name
WHERE condition
GROUP BY column_name(s)
[ORDER BY column_name(s) [ASC|DESC];]
Using GROUP BY with the SUM Function
• Sometimes we want to retrieve the values of these functions for only those groups that satisfy certain
conditions
• The HAVING-clause is used for specifying a selection condition on groups (rather than on individual
tuples)
Syntax:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
Example
SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;