You are on page 1of 7

Sql

Data Integrity
Entity integrity: It specifies that there should be no duplicate rows in a table.

Domain integrity: It enforces valid entries for a given column by restricting the type,
the format, or the range of values.

Referential integrity: It specifies that rows cannot be deleted, which are used by other
records.

User-defined integrity: It enforces some specific business rules that are defined by
users. These rules are different from entity, domain or referential integrity.

SQL session

A SQL session is an occurrence of a user interacting with a relational database through the use of
SQL commands. When a user initially connects to the database, a session is established

When a user connects to a database, the SQL session is initialized. The CONNECT command is used
to establish a database connection.

CONNECT user@database

When a user disconnects from a database, the SQL session is terminated. The DISCONNECT
command is used to disconnect a user from the database.

Basic categories of commands used in SQL to perform various functions. These functions include
building database objects, manipulating objects

 Data Definition Language (DDL)


 Data Manipulation Language (DML)
 Data Query Language (DQL)
 Data Control Language (DCL)
 Data administration commands
 Transactional control commands

Defining Database Structures


Data Definition Language (DDL) is the part of SQL that allows a database user to create and
restructure database objects,

CREATE TABLE
ALTER TABLE
DROP TABLE
CREATE INDEX
ALTER INDEX
DROP INDEX
CREATE VIEW
DROP VIEW
Data Manipulation Language (DML)

Data Manipulation Language (DML) is the part of SQL used to manipulate data within objects
of a relational database.

INSERT
UPDATE
DELETE

Data Query Language (DQL)

Data Query Language (DQL) is the most concentrated focus of SQL for modern relational
database users. The base command is as follows:

SELECT

Data Control Language (DCL)

Data control commands in SQL allow you to control access to data within the database.
These Data Control Language (DCL) commands are normally used to create objects related
to user access and also control the distribution of privileges among user

ALTER PASSWORD
GRANT REVOKE
CREATE SYNONYM

commands are normally used to create objects related to user access and also control the
distribution of privileges among users

Data administration commands

Data administration commands allow the user to perform audits and perform analyses on
operations within the database. They can also be used to help analyse system performance

START AUDIT
STOP AUDIT

In addition to the previously introduced categories of commands, there are commands that
allow the user to manage database transactions

COMMIT—Saves database transactions


ROLLBACK—Undoes database transactions
SAVEPOINT—Creates points within groups of transactions in which to ROLLBACK
SET TRANSACTION—Places a name on a transaction
Database object

A database object is any defined object in a database that is used to store or reference data.
Some examples of database objects include tables, views, clusters, sequences, indexes, and
synonyms.

A schema is a collection of database objects (as far as this hour is concerned—tables)


associated with one particular database username. This username is called the schema
owner

Creating a Table from an Existing Table

create table new_table_name as


select [ *|column1, column2 ] from table_name [ where ]

Dropping Tables

drop table table_name [ restrict|cascade ]

Integrity constraints

Integrity constraints are used to ensure accuracy and consistency of data in a relational
database. Data integrity is handled in a relational database through the concept of
referential integrity. Many types of integrity constraints play a role in referential integrity
(RI).

Primary Key Constraints

Primary key is the term used to identify one or more columns in a table that make a row of
data unique

Unique Constraints

A unique column constraint in a table is similar to a primary key in that the value in that
column for every row of data in the table must have a unique value

Foreign Key Constraints

A foreign key is a column in a child table that references a primary key in the parent table. A
foreign key constraint is the main mechanism used to enforce referential

NOT NULL Constraints

Previous examples use the keywords NULL and NOT NULL listed on the same line as each
column and after the data type. NOT NULL is a constraint that you can place on a table’s
column
Check Constraints

Check (CHK) constraints can be utilized to check the validity of data entered into particular
table columns. Check constraints are used to provide back-end database edit
although edits are commonly found in the front-end application as well. General edits
restrict values that can be entered into columns or objects, whether within the database
itself or on a front-end application. The check constraint is a way of providing another
protective layer for the data.

Dropping Constraints

Any constraint that you have defined can be dropped using the ALTER TABLE command with
the DROP CONSTRAINT option.
ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK;

Normalization

Normalization is a process of reducing redundancies of data in a database;

The Normal Forms

it does not contain any composite or multi-valued attribute. A relation is in first normal
form if every attribute in that relation is singled valued attribute.

The Second Normal Form

Relation must be in first normal form and relation must not contain any partial
dependency. A relation is in 2NF if it has No Partial Dependency, i.e., no non-prime
attribute (attributes which are not part of any candidate key) is dependent on any
proper subset of any candidate key of the table.

The Third Normal Form

The third normal form’s objective is to remove data in a table that is not dependent on the
primary key

Denormalization

Denormalization is the process of taking a normalized database and modifying table


structures to allow controlled redundancy for increased database performance.

Attempting to improve performance is the only reason to ever denormalize a database. A


denormalized database is not the same as a database that has not been normalized.
Denormalizing a database is the process of taking the level of normalization within the
database down a notch or two. Remember, normalization can actually slow performance
with its frequently occurring table join operations.

transaction

A transaction is a unit of work that is performed against a database. Transactions are units or
sequences of work accomplished in a logical order, whether in a manual fashion by a user or
automatically by some sort of a database program

 COMMIT
 ROLLBACK
 SAVEPOINT

When a transaction has completed, the transactional information is stored either in an


allocated area or in a temporary rollback area in the database. All changes are held in this
temporary rollback area until a transactional control command is issued. When a
transactional control command is issued, changes are either made to the database or
discarded; then, the temporary rollback area is emptied

The SAVEPOINT Command

A savepoint is a point in a transaction where you can roll the transaction back to this point
without rolling back the entire transaction

savepoint savepoint_name

The RELEASE SAVEPOINT Command

RELEASE SAVEPOINT savepoint_name;

The SET TRANSACTION Command

SET TRANSACTION READ WRITE;


SET TRANSACTION READ ONLY;
READ ONLY is used for transactions that require query-only access. READ ONLY is useful for
report generation and for increasing the speed at which transactions are accomplished

A query is an inquiry into the database using the SELECT statement. A query is used to
extract data from the database in a readable format according to the user’s request

IN
The IN operator is used to compare a value to a list of literal values that have been specified.
For TRUE to be returned, the compared value must match at least one of the values in the
list.

The IN operator is used to compare a value to a list of literal values that have been specified.
For TRUE to be returned, the compared value must match at least one of the values in the
list

WHERE SALARY IN(‘20000’, ‘30000’, ‘40000’)

Using the IN operator can achieve the same results as using the OR operator and can return
the results more quickly

LIKE

The percent sign (%)


The underscore (_)

EXISTS

The EXISTS operator is used to search for the presence of a row in a specified table that
meets certain criteria

ALL, SOME, and ANY Operators


The ALL operator is used to compare a value to all values in another value set.

Aggregate functions.

This hour covers aggregate functions. An aggregate function is used to


provide summarization information for an SQL statement, such as counts, totals, and
averages.

The DISTINCT command cannot be used with COUNT(*), only with COUNT
(column_name)
The value of an argument must be numeric to use the SUM function. The SUM function
cannot be used on columns having a data type other than numeric, such as
character or date
The GROUP BY clause

The GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups. The GROUP BY clause follows the WHERE clause in a
SELECT statement and precedes the ORDER BY clause.
WHERE clause and must precede the ORDER BY clause if one is used.

You might also like