Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Database Management System (DBMS)

SQL custom notes

Uploaded by

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

Database Management System (DBMS)

SQL custom notes

Uploaded by

Rohit Solanki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Database

A database is an organized collection of structured information, or data, typically stored electronically in


a computer system for rapid search and retrieval.

Database types

1. Relational Database
2. NoSQL Database(Non-Relational Database)
3. Cloud Database

Database Management System (DBMS) is a software that is used to define, create, and maintain a
database and provides controlled access to the data.

 Makes it easy to manage large amounts of information


 Handles Security
 Backups
 Importing/exporting data
 Concurrency

1. Relational Database

The name comes from the way that data is stored in multiple, related tables. Within the tables, data is
stored in rows and columns. The relational database management system (RDBMS) is a program that
allows you to create, update, and administer a relational database. Structured Query Language (SQL) is
the most common language for reading, creating, updating, and deleting data. Column represents the
attribute while row represents the value. They are compliant with ACID (Atomicity, Consistency,
Isolation, and Durability), which is a standard set of properties for reliable database transactions.
Relational databases work well with structured data.

Examples: Microsoft SQL Server, Oracle Database, MySQL, PostgreSQL, and IBM Db2

2. NoSQL Database

NoSQL databases (aka "not only SQL") are non-tabular databases and store data differently than
relational tables. NoSQL databases come in a variety of types based on their data model. The main types
are document, key-value, wide-column, and graph. They provide flexible schemas and scale easily with
large amounts of data and high user loads, increasing data size won’t affect the performance. It is great
for organizations seeking to store unstructured or semi-structured data.

Examples: Apache Cassandra, MongoDB, CouchDB, and CouchBase.


3. Cloud Database

A cloud database refers to any database that’s designed to run on the cloud. Like other cloud-based
applications, cloud databases offer flexibility and scalability, along with high availability. Cloud databases
are also often low-maintenance since many are offered via a SaaS model.

Examples: Microsoft Azure SQL Database, Amazon Relational Database Service, Oracle Autonomous
Database.
Structured Query Language (SQL)

 SQL is a language used for interacting with Relational Database Management Systems (RDBMS).
 You can use SQL to get the RDBMS to do things for you :
o Create, retrieve, update & delete data
o Create & manage databases
o Design & create database tables
o Perform administration tasks (security, user management, import/export, etc)
 SQL implementations vary between systems
o Not all RDBMS' follow the SQL standard to a 'T'
o The concepts are the same but the implementation may vary
 Here are some dialects of the SQL language:

o Transact-SQL (short for T-SQL) – used in Microsoft SQL Server;


o PL/SQL (Procedural Language / Structured Query Language) – used in Oracle Database;
o PL/pgSQL (Procedural Language/PostGres Structured Query Language) – used in
PostgreSQL
 SQL is a hybrid language, it's 4 types of languages in one (i.e. The SQL commands are mainly
categorized into four categories):
I. Data Definition Language (DDL) Used for defining database schemas/objects and working with
them, i.e. create, modify and delete them.

This group includes the following operators:

 CREATE – used to create database objects;


 DROP – used to delete database objects.
 TRUNCATE -used to delete all the content from the table. we can’t delete the single row as
WHERE clause is not used
 ALTER – used to modify database objects;

II. Data Query Language (DQL) Used to query the database for information. Get information that is
already stored there. The purely read-only SELECT query statement is classed with the DQL and
so is considered by the standard to be outside of DML. The SELECT ... INTO form is considered to
be DML because it manipulates (i.e. modifies) data. In common practice though, this distinction
is not made and SELECT is widely considered to be part of DML.
III. Data Manipulation Language (DML) Used for inserting, updating and deleting data from the
database i.e. manipulate them.

This group includes the most common SQL language operators:

 SELECT– performs data sampling;


 INSERT – adds new data;
 UPDATE – changes the existing data;
 DELETE – deletes a single record or multiple records depending on the condition specified in
the WHERE clause. DELETE is a DML Command so it can be rolled back. If we omit the WHERE
clause then all of the records will be deleted. The DELETE statement scans every row before
deleting it, thus it is slower as compared to TRUNCATE command.

IV. Data Control Language (DCL) Used for controlling access to the data in the database. User &
permissions management.

This includes:

 GRANT – gives the user or group permissions to perform certain operations on the object;
 REVOKE – revokes the granted permissions;
 DENY – sets a ban that has priority over the permission.

V. Transaction Control Language (TCL) – a group of operations to manage transactions. A


transaction is a command or block of commands (instructions) that are successfully completed
as a whole, with all the changes made in the database being fixed permanently or cancelled, i.e.
all the changes made by any command included in the transaction will be cancelled

TCL operator group is designed just for the implementation and management of transactions. It
can be referred to here:

 BEGIN TRANSACTION – serves to determine the beginning of the transaction;


 COMMIT TRANSACTION – applies the transaction;
 ROLLBACK TRANSACTION – rollback all changes made in the context of the current transaction;
 SAVE TRANSACTION – sets the intermediate saving point within the transaction. It is used for
dividing (or) breaking a transaction into multiple units so that the user has a chance of roll
backing the transaction up to a specified point.

Database query: request made to the RDBMS to perform certain operation like fetching some
information/data. It is a set of instruction given to RDBMS (usually written in SQL) that tells RDBMS what
information you want it to retrieve for you.
Data Definition Language (DDL)

Create Table

create table test.employee(


EMPLOYEE_ID INT not null ,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20),
DESIGNATION VARCHAR(30),
SALARY INT ,
primary key (EMPLOYEE_ID)
);
Drop Table
drop table test.employee;

How does DROP TABLE work?

The DROP TABLE operation removes the table definition and data as well as the indexes, constraints, and triggers
related to the table.

This command frees the memory space. No triggers are fired when executing DROP TABLE.

This operation cannot be rolled back in MySQL, but it can in Oracle, SQL Server, and PostgreSQL.

In SQL Server, DROP TABLE requires ALTER permission in the schema to which the table belongs; MySQL requires
the DROP privilege; Oracle the requires the DROP ANY TABLE privilege. In PostgreSQL, users can drop their own
tables.

TRUNCATE TABLE

The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.

TRUNCATE TABLE test.employee;

How does TRUNCATE TABLE work?

Be careful using this command as TRUNCATE transactions can be rolled back in database engines like SQL Server
and PostgreSQL, but not in MySQL and Oracle.

TRUNCATE is faster than DELETE, as it doesn't scan every record before removing it. TRUNCATE TABLE locks the
whole table to remove data from a table; thus, this command also uses less transaction space than DELETE.

Unlike DELETE, TRUNCATE does not return the number of rows deleted from the table. It also resets the table auto-
increment value to the starting value (usually 1). If you add a record after truncating the table, it will have
ID=1. Note: In PostgreSQL, you can choose to restart or continue the auto-increment value. Oracle uses a
sequence to increment values, which is not reset by TRUNCATE.

Of course, you need permission to use TRUNCATE TABLE. In PostgreSQL, you need the privilege TRUNCATE; in SQL
Server, the minimum permission is ALTER table; in MySQL, you need the DROP privilege. Finally, Oracle requires
the DROP ANY TABLE system privilege to use this command.

alter Table

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. It is also used to add
and drop various constraints on an existing table.

 ALTER TABLE - ADD Column

To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

 To ADD 2 columns to table.


ALTER TABLE table_name ADD (column_name datatype, column_name datatype);

 ALTER TABLE - DROP COLUMN

To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):

ALTER TABLE table_name


DROP column_name;
 ALTER TABLE - ALTER/MODIFY COLUMN

o To change the data type of a column in a table, use the following syntax:

My SQL / Oracle (prior version 10G):

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

Oracle 10G and later:

ALTER TABLE table_name


MODIFY column_name datatype;

o To change the constraint of a column in a table, use the following syntax:

ALTER table TableName modify column_name data type constraint;

Add Not null constraint

Remove Not null constraint


Add primary key, auto increment constraint

Add foreign key constraint

DROP a FOREIGN KEY Constraint

To drop a FOREIGN KEY constraint, use the following SQL:

MySQL:
ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;

SQL Server / Oracle / MS Access:

ALTER TABLE Orders


DROP CONSTRAINT FK_PersonOrder;

CHANGE

Can change a column name or definition, or both


ALTER TABLE t1 CHANGE a b BIGINT NOT NULL

MODIFY

Can change a column definition but not its name


ALTER TABLE t1 MODIFY b INT NOT NULL

RENAME COLUMN (from MySQL 8.0)

Can change a column name but not its definition


ALTER TABLE t1 RENAME COLUMN b TO a

A word of caution:
 you need to specify the full column definition again when using a MODIFY query. If your column has, for
example, a DEFAULT value, or a column comment, you need to specify it in the MODIFY statement along
with the data type and the NOT NULL, or it will be lost.
 The safest practice to guard against such mishaps is to copy the column definition from the output of
a SHOW CREATE TABLE YourTable query, modify it to include the NOT NULL constraint, and paste it into
your ALTER TABLE... MODIFY... query.

Data Manipulation Language (DML)

EMPLOYEE

EMPLOYEE_I FIRST_NAM LAST_NAM DESIGNATIO SALAR COUNTR DEPARTMENT_I


D E E N Y DOB Y D
SOFTWARE
100001 DEAN JONES DEVELOPER 12000 0000-00-00 INDIA 101
SOFTWARE
100002 ROGER DSOUZA TESTER 10000 8/2/1994 INDIA 101
JUNIOR
100003 NIEL SONI RECRUITER 10000 8/2/1995 INDIA 102
ACCOUNTS
100004 JEEVAN DSOUZA MANAGER 20000 1/10/1989 INDIA 103
OPERATIONS
100005 RUSKY BOND HEAD 30000 6/24/1981 INDIA 104
OPERATIONS
100006 GEORGE FLYODD HEAD 40000 1/24/1980 USA 104
SOFTWARE 10/18/199
100007 RAM SINGH DEVELOPER 15000 2 USA 105
SOFTWARE
100008 MONA ALI TESTRER 14000 8/2/1994 USA 105
SENIOR
100009 BERRY DREWSKY RECRUITER 20000 8/2/1991 USA 106
DEVELOPER
100010 XING WONG INTERN 5000 8/2/1998 USA 105

DEPARTMENT

DEPARTMENT_I DEPARTMENT_NAM
D E BU_ID
101 IT 1
102 HR 1
103 ACCOUNTS 1
104 OPERATIONS 1
105 IT 2
106 HR 2

BUSINESS_UNIT

BU_ID BU_NAME
1 PAYMENTS
2 RETAIL BANKING
CORPORATE
3 BANKING

SELECT

 The SELECT statement is used to select data from a database. The data returned is stored in a result table,
called the result-set.
SELECT Syntax

SELECT column1, column2, ...


FROM table_name;

 Here, column1, column2, ... are the field names of the table you want to select data from. If you want to
select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

SELECT DISTINCT

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often
contains many duplicate values; and sometimes you only want to list the different (distinct) values.

SELECT DISTINCT Syntax

SELECT DISTINCT column1, column2, ...


FROM table_name;

WHERE Clause

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.

WHERE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition;

AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:

 The AND operator displays a record if all the conditions separated by AND are TRUE.
 The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;

ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order,
use the DESC keyword.

ORDER BY Syntax

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

NULL Values

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.We will have to use the IS
NULL and IS NOT NULL operators instead.
IS NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

IS NOT NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

SELECT TOP Clause

The SELECT TOP clause is used to specify the number of records to return.The SELECT TOP clause is useful on
large tables with thousands of records. Returning a large number of records can impact performance.

SQL Server / MS Access Syntax:

SELECT TOP number|percent column_name(s)


FROM table_name
WHERE condition;

MySQL Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

Find nth highest salary

Note: The above query doesn’t handle duplicate salaries

Oracle 12 Syntax:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;

Older Oracle Syntax:

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

Find nth highest salary


Note: The above query doesn’t handle duplicate salaries

Find top nth highest salaries

Note: The above query doesn’t handle duplicate salaries

Older Oracle Syntax (with ORDER BY):

SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;

MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

MIN() Syntax

SELECT MIN(column_name)
FROM table_name
WHERE condition;

MAX() Syntax

SELECT MAX(column_name)
FROM table_name
WHERE condition;
COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that match a specified criterion.

COUNT() Syntax

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Using count in Joins


The AVG() function returns the average value of a numeric column.

AVG() Syntax

SELECT AVG(column_name)
FROM table_name
WHERE condition;

The SUM() function returns the total sum of a numeric column.

SUM() Syntax

SELECT SUM(column_name)
FROM table_name
WHERE condition;
//CONDITIONS INSIDE COUNT()

LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

A wildcard character is used to substitute one or more characters in a string. 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

LIKE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

BETWEEN Operator

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Aliases

SQL aliases are used to give a table , or a column in a table, a temporary name.Aliases are often used to make
column names more readable.

An alias only exists for the duration of that query.An alias is created with the AS keyword.

Alias Column Syntax

SELECT column_name AS alias_name


FROM table_name;

Alias Table Syntax

SELECT column_name(s)
FROM table_name AS alias_name;
INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL
query. However, make sure the order of the values is in the same order as the columns in the table. Here,
the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE Statement

The DELETE statement is used to delete existing records in a table.

DELETE Syntax

DELETE FROM table_name WHERE condition;

Delete All Records

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes,
and indexes will be intact:

DELETE FROM table_name;

How does DELETE work?

If you don’t want to remove table structure or you’re only deleting specific rows, use the DELETE command. It can
remove one, some, or all rows in a table. DELETE returns the number of rows removed from the table.

However, DELETE uses a row lock during execution and can be rolled back. Every deleted row is locked, so it will
require a lot of locks if you’re working in a large table.
DELETE also keeps the auto-increment ID in the table. If you remove the last record in the table with ID=20 and
then add a new record, this record will have ID=21 – even though the record immediately before it will be ID=19.

DELETE can be executed by triggers. A trigger can be called before, after, or instead of the DELETE operation. It can
be executed for any row change or when all rows are removed. Removing rows in another table can also
trigger DELETE.

Of course, to use the DELETE command you need DELETE permission for that table.

DELETE VS TRUNCATE VS DROP

Which operation is best for which use case?

 To remove specific rows, use DELETE.


 To remove all rows from a large table and leave the table structure, use TRUNCATE TABLE. It’s faster
than DELETE.
 To remove an entire table, including its structure and data, use DROP TABLE.

DELETE TRUNCATE DROP

Type DML DDL DDL

Uses a lock Row lock Table lock Table lock

Works in WHERE Yes No No

Entire table structure: data,


One, some, or all
Removes ... All rows in a table. privileges, indexes, constraints,
rows in a table.
triggers.

MySQL: Yes Doesn’t apply


Oracle: No
Resets ID auto-
No PostgreSQL: User
increment
decides
SQL Server: Yes

MySQL: No MySQL: No
Oracle: No Oracle: Yes
Rollback Yes
PostgreSQL: Yes PostgreSQL: Yes
SQL Server: Yes SQL Server : Yes

Transaction Whole table


Each row Whole table (minimal)
logging (minimal)
DELETE TRUNCATE DROP

Works with No
Yes No
indexed views

Space
More space Less space More space
requirements

Fires triggers Yes No No

Speed Slow Fastest Faster

SQL JOIN

A JOIN clause is used to combine rows multiple tables, based on a related column between them.

Different Types of SQL JOINs

 (INNER) JOIN: Returns records that have matching values in both tables
 LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
 RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left
table
 FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
 SELF JOIN: A self join is a regular join, but the table is joined with itself.

 INNER JOIN

The INNER JOIN keyword selects records that have matching values in both tables.

INNER JOIN Syntax


SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

 LEFT JOIN

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the
right table (table2). The result is 0 records from the right side, if there is no match.

LEFT JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.


ORACLE 11G QUERY

SELECT A.EMPLOYEE_ID,B.VENDOR_NAME,B.AMOUNT,B.CLAIMED_DATE

FROM EMPLOYEE A,COUPONS B

WHERE A.EMPLOYEE_ID=B.EMPLOYEE_ID(+);

Oracle outer join operator (+) allows you to perform outer joins on two or more tables

 RIGHT JOIN

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the
left table (table1). The result is 0 records from the left side, if there is no match.

RIGHT JOIN Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

ORACLE 11G QUERY

SELECT A.EMPLOYEE_ID,B.VENDOR_NAME,B.AMOUNT,B.CLAIMED_DATE

FROM EMPLOYEE A,COUPONS B

WHERE B.EMPLOYEE_ID=A.EMPLOYEE_ID(+);

 FULL OUTER JOIN


The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table
records.

Tip: FULL OUTER JOIN and FULL JOIN are the same.

FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

 Self Join

A self join is a regular join, but the table is joined with itself.

Self Join Syntax

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

T1 and T2 are different table aliases for the same table.

UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.

 Every SELECT statement within UNION must have the same number of columns
 The columns must also have similar data types
 The columns in every SELECT statement must also be in the same order
UNION Syntax

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

UNION With WHERE

SELECT column_name(s) FROM table1

WHERE condition

UNION

SELECT column_name(s) FROM table2

WHERE condition;

UNION ALL Syntax

The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

Note: The column names in the result-set are usually equal to the column names in the first SELECT statement.

UNION ALL With WHERE

SELECT column_name(s) FROM table1

WHERE condition

UNION ALL

SELECT column_name(s) FROM table2

WHERE condition;

ANOTHER UNION

GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows , like "find the number of
customers in each country".

The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group
the result-set by one or more columns.

GROUP BY Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

HAVING Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

EXISTS Operator

The EXISTS operator is used to test for the existence of any record in a subquery.

The EXISTS operator returns TRUE if the subquery returns one or more records.

EXISTS Syntax

SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

ANY and ALL Operators

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other
values.

ANY Operator

The ANY operator:

 returns a boolean value as a result


 returns TRUE if ANY of the subquery values meet the condition

ANY means that the condition will be true if the operation is true for any of the values in the range.

ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);

Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

ALL Operator

The ALL operator:

 returns a boolean value as a result


 returns TRUE if ALL of the subquery values meet the condition
 is used with SELECT, WHERE and HAVING statements

ALL means that the condition will be true only if the operation is true for all values in the range.

ALL Syntax With SELECT


SELECT ALL column_name(s)
FROM table_name
WHERE condition;

ALL Syntax With WHERE or HAVING


SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);

SELECT INTO Statement

The SELECT INTO statement copies data from one table into a new table.
SELECT INTO Syntax

Copy all columns into a new table:

SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;

Copy only some columns into a new table:

SELECT column1, column2, column3, ...


INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;

The new table will be created with the column-names and types as defined in the old table. You can create new
column names using the AS clause.

INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another table.

The INSERT INTO SELECT statement requires that the data types in source and target tables match.

Note: The existing records in the target table are unaffected.

INSERT INTO SELECT Syntax

Copy all columns from one table to another table:

INSERT INTO table2


SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:

INSERT INTO table2 (column1, column2, column3, ...)


SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

CASE Statement

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else
statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns
the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax

CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;

IFNULL(),COALESCE(), and NVL() Functions

MySQL

The MySQL IFNULL() function lets you return an alternative value if an expression is NULL:

SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0))


FROM Products;

or we can use the COALESCE() function, like this:

SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))


FROM Products;

Oracle

The Oracle NVL() function achieves the same result:

SELECT ProductName, UnitPrice * (UnitsInStock + NVL(UnitsOnOrder, 0))


FROM Products;

SQL Constraints

SQL constraints are used to specify rules for the data in a table.

CREATE TABLE table_name (


column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of
the data in the table. If there is any violation between the constraint and the data action, the action is aborted. The
following constraints are commonly used in SQL:

 NOT NULL - Ensures that a column cannot have a NULL value


 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
 FOREIGN KEY - Prevents actions that would destroy links between tables
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified
 CREATE INDEX - Used to create and retrieve data from the database very quickly

 NOT NULL Constraint

By default, a column can hold NULL values.


The NOT NULL constraint enforces a column to NOT accept NULL values.

NOT NULL on CREATE TABLE

create table test.employee(

EMPLOYEE_ID INT not null ,

FIRST_NAME VARCHAR(20),

LAST_NAME VARCHAR(20),

DESIGNATION VARCHAR(30),

SALARY INT ,

primary key (EMPLOYEE_ID)

);

NOT NULL on ALTER TABLE

To create a NOT NULL constraint on the "Age" column when the "Persons" table is already created, use the
following SQL:

ALTER TABLE employee


MODIFY SALARY int NOT NULL;

 UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different. A PRIMARY KEY constraint
automatically has a UNIQUE constraint

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of
columns. However, you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.

UNIQUE Constraint on ALTER TABLE

To create a UNIQUE constraint on the "ID" column when the table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons


ADD UNIQUE (ID);

To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following
SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons


ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

DROP a UNIQUE Constraint

To drop a UNIQUE constraint, use the following SQL:

MySQL:
ALTER TABLE Persons
DROP INDEX UC_Person;

SQL Server / Oracle / MS Access:

ALTER TABLE Persons


DROP CONSTRAINT UC_Person;

 CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a column.

If you define a CHECK constraint on a column it will allow only certain values for this column

CHECK on CREATE TABLE

The following SQL creates a CHECK constraint on the "Age"( must be 18, or older) column when the
"Persons" table is created.

MySQL:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);

SQL Server / Oracle / MS Access:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the
following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);

CHECK on ALTER TABLE

To create a CHECK constraint on the "Age" column when the table is already created, use the following
SQL:

MySQL / SQL Server / Oracle / MS Access:


ALTER TABLE Persons
ADD CHECK (Age>=18);

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the
following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons


ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');

DROP a CHECK Constraint

To drop a CHECK constraint, use the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons


DROP CONSTRAINT CHK_PersonAge;

MySQL:

ALTER TABLE Persons


DROP CHECK CHK_PersonAge;

 DEFAULT Constraint

The DEFAULT constraint is used to set a default value for a column.The default value will be added to all
new records, if no other value is specified.

DEFAULT on CREATE TABLE

The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created:

My SQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);

The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():

CREATE TABLE Orders (


ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE()
);

SQL DEFAULT on ALTER TABLE

To create a DEFAULT constraint on the "City" column when the table is already created, use the following
SQL:

MySQL:
ALTER TABLE Persons
ALTER City SET DEFAULT 'Sandnes';

SQL Server:

ALTER TABLE Persons


ADD CONSTRAINT df_City
DEFAULT 'Sandnes' FOR City;

MS Access:

ALTER TABLE Persons


ALTER COLUMN City SET DEFAULT 'Sandnes';

Oracle:

ALTER TABLE Persons


MODIFY City DEFAULT 'Sandnes';

DROP a DEFAULT Constraint

To drop a DEFAULT constraint, use the following SQL:

MySQL:

ALTER TABLE Persons


ALTER City DROP DEFAULT;

SQL Server / Oracle / MS Access:

ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT;

AUTO INCREMENT Field

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table
(Often this is the primary key).

Syntax for MySQL

The following SQL statement defines the "Personid" column to be an auto-increment primary key field in the
"Persons" table:

CREATE TABLE Persons (


Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);

By default, the starting value for AUTO_INCREMENT is 1,to let the AUTO_INCREMENT sequence start with another
value, use the following SQL statement:

ALTER TABLE Persons AUTO_INCREMENT=100;

Syntax for Oracle


In Oracle you will have to create an auto-increment field with the sequence object (this object generates a number
sequence).

Use the following CREATE SEQUENCE syntax:

CREATE SEQUENCE seq_person


MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;

To insert a new record into the "Persons" table, we will have to use the nextval function (this function retrieves the
next value from seq_person sequence):

INSERT INTO Persons (Personid,FirstName,LastName)


VALUES (seq_person.nextval,'Lars','Monsen');

SUB-QUERY

A subquery (inner query) is a SQL query nested inside a larger query(outer query).

 Subquery may occur in a SELECT clause ,FROM clause or WHERE clause.

 The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another

subquery.

 A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the

comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator,

such as IN, ANY, or ALL.

 SQL executes innermost subquery first, then next level.

ON DELETE
1. CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in
the child table.
2. SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the child
table to NULL.
Query Visualization:
SARGABLE (Search ARGument ABLE) queries:
 Queries that can use indexing for faster execution
 Essential for optimizing DB performance
 Example

 How to write SARGABLE queries:


a. Avoid using functions or calculations on indexed columns in WHERE clause
b. Use direct comparisons when possible, instead of wrapping the column in a function
c. I we need to use a function on a column, consider creating a computed column or a function-based
index, if the database system supports it.
What are the Keys in DBMS?
A key in DBMS is an attribute or a set of attributes that help to uniquely identify a tuple (or row) in a
relation (or table). Keys are also used to establish relationships between the different tables and
columns of a relational database.

Why keys are required?


There can be thousands and thousands of records, some of which may be duplicated.
There should be a way to identify each record separately and uniquely, i.e. no duplicates. Keys allow us
to be free from this hassle.

Types of Keys in DBMS


There are broadly seven types of keys in DBMS:
1. Primary Key
2. Candidate Key
3. Super Key
4. Foreign Key
5. Composite Key
6. Alternate Key
7. Unique Key
8. Surrogate Key
Primary Key

A primary key is a column or set of columns of a table that helps to identify every record present in that
table uniquely. There can be only one primary Key in a table.

The PRIMARY KEY (PK) constraint put on a column or set of columns will not allow them to have any null
values or any duplicates

Foreign Key: It is a primary key of another related table. It helps us to define relationship between two
related tables.

Super Key

Super Key is the set of all the keys that help to identify rows in a table uniquely. This means that all
those columns of a table that are capable of identifying the other columns of that table uniquely will all
be considered super keys.
Super Key is the superset of a candidate key (explained below). The Primary Key of a table is picked from
the super key set to be made the table’s identity attribute.
Max no. of possible super keys= (2n-1), where n is the number of columns (depends on the attributes)

A B C D
1 1 5 1
2 1 5 1
3 1 7 1
4 2 7 1
5 2 5 1
No of Super keys in the above table=8 i.e. {A}, {AB}, {AC}, {AD}, {ABC}, {ACD}, {ABD}, {ABCD}

Note: It is just a theoretical concept that is not implemented in RDBMS.

Candidate Key

Candidate keys are those attributes that uniquely identify rows of a table. The Primary Key of a table is
selected from one of the candidate keys. So, candidate keys have the same properties as the primary
keys explained above. There can be more than one candidate keys in a table.

Candidate key is a super key whose proper subset is not a super key

Example:

A B C
1 1 1
2 1 2
3 2 1
4 2 2

S1= {1, 2, 3}
S2= {1, 2}

S2 C S1 (S2 is a proper subset of S1 when)

S2 C S1 (S2 is a subset of S1)

S1 C S2 (S1 is not a subset of S2)

In our example, A, AB, AC, ABC, BC are the super keys and A, BC are the candidate keys.

Foreign Key

Foreign Key is used to establish relationships between two tables. A foreign key will require each value
in a column or set of columns to match the Primary Key of the referential table. Foreign keys help to
maintain data and referential integrity

Composite Key

Composite Key is a set of two or more attributes that help identify each tuple in a table uniquely. The
attributes in the set may not be unique when considered separately. However, when taken all together,
they will ensure uniqueness.
Alternate Key
As stated above, a table can have multiple choices for a primary key; however, it can choose only one.
So, all the keys which did not become the primary Key are called alternate keys

Unique Key
Unique Key is a column or set of columns that uniquely identify each record in a table. All values will
have to be unique in this Key. A unique Key differs from a primary key because it can have only one null
value, whereas a primary Key cannot have any null values.

Surrogate Key

An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key
are unique because they are created when you don’t have any natural primary key.
Trigger

cascade

Views

indexes

Write a program to find candidate keys

ACID

Type of functional dependencies


Normalization

ER Diagram and database design

You might also like