SQL Integrity Constraints 1
SQL Integrity Constraints 1
LANGUAGE (SQL)
STRUCTURED QUERY LANGUAGE
SQL stands for “Structured Query Language” and can be
pronounced as “SQL” or “sequel – (Structured English Query
Language)”.
It is a query language used for accessing and modifying
information in the database.
IBM first developed SQL in 1970s. Also it is an ANSI/ISO
standard. It has become a Standard Universal Language used by
most of the relational database management systems (RDBMS).
Some of the RDBMS systems are: Oracle, Microsoft SQL server,
Sybase etc. Most of these have provided their own implementation
thus enhancing it's feature and making it a powerful tool.
Few of the sql commands used in sql programming are SELECT
Statement, UPDATE Statement, INSERT INTO Statement,
DELETE Statement, WHERE Clause, ORDER BY Clause,
GROUP BY Clause, ORDER Clause, Joins, Views, GROUP
Functions, Indexes etc.
CREATE DATABASE Statement
The CREATE DATABASE statement is used
to create a database.
SQL CREATE DATABASE Syntax
CREATE DATABASE database_name
The CREATE TABLE Statement
The CREATE TABLE statement is used to create a
table in a database.
SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
STRUCTURED QUERY LANGUAGE
In a simple manner, SQL is a non-procedural, English-
like language that processes data in groups of records
rather than one record at a time. Few functions of SQL
are:
store data
modify data
retrieve data
modify data
delete data
create tables and other database objects
STRUCTURED QUERY LANGUAGE
Database Tables
A database most often contains one or more tables. Each table is
identified by a name (e.g. "Customers" or "Orders"). Tables contain
records (rows) with data.
Below is an example of a table called "Persons":
SQL Server comes with the following data types for storing a date or a
date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
TIMESTAMP - format: a unique number
SQL Aggregate Functions
SQL aggregate functions return a single value,
calculated from values in a column.
Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
SQL Scalar functions
SQL scalar functions return a single value, based on the input
value.
Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals
specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed
AVG() Function
The AVG() function returns the average value
of a numeric column.
SQL AVG() Syntax
SELECT AVG(column_name) FROM
table_name
COUNT() function
The COUNT() function returns the number of rows that matches a
specified criteria.
SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values
(NULL values will not be counted) of the specified column:
SELECT COUNT(column_name) FROM table_nameSQL COUNT(*)
Syntax
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name
SQL COUNT(DISTINCT column_name) Syntax
The COUNT(DISTINCT column_name) function returns the number of
distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name
FIRST() Function
The FIRST() function returns the first value
of the selected column.
SQL FIRST() Syntax
SELECT FIRST(column_name) FROM
table_name
LAST() Function
The LAST() function returns the last value of
the selected column.
SQL LAST() Syntax
SELECT LAST(column_name) FROM
table_name
MAX() Function
The MAX() function returns the largest value
of the selected column.
SQL MAX() Syntax
SELECT MAX(column_name) FROM
table_name
MIN() Function
The MIN() function returns the smallest value
of the selected column.
SQL MIN() Syntax
SELECT MIN(column_name) FROM
table_name
SUM() Function
The SUM() function returns the total sum of a
numeric column.
SQL SUM() Syntax
SELECT SUM(column_name) FROM
table_name
GROUP BY Statement
The GROUP BY statement is used in
conjunction with the aggregate functions to
group the result-set by one or more columns.
SQL GROUP BY Syntax
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING Clause
The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate
functions.
SQL HAVING Syntax
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name)
operator value
UCASE() Function
The UCASE() function converts the value of
a field to uppercase.
SQL UCASE() Syntax
SELECT UCASE(column_name) FROM
table_nameSyntax for SQL Server
SELECT UPPER(column_name) FROM
table_name
LCASE() Function
The LCASE() function converts the value of a
field to lowercase.
SQL LCASE() Syntax
SELECT LCASE(column_name) FROM
table_nameSyntax for SQL Server
SELECT LOWER(column_name) FROM
table_name
MID() Function
The MID() function is used to extract characters from a text
field.
SQL MID() Syntax
SELECT MID(column_name,start[,length]) FROM table_name
column_name -> Required. The field to extract characters from
Start -> Required. Specifies the starting position (starts at 1)
Length -> Optional. The number of characters to return. If
omitted, the MID() function returns the rest of the text
LEN() Function
The LEN() function returns the length of the
value in a text field.
SQL LEN() Syntax
SELECT LEN(column_name) FROM
table_name
ROUND() Function
The ROUND() Function
The ROUND() function is used to round a numeric
field to the number of decimals specified.
SQL ROUND() Syntax
SELECT ROUND(column_name,decimals) FROM
table_name
column_name -> Required. The field to round.
Decimals -> Required. Specifies the number of
decimals to be returned.
NOW() Function
The NOW() function returns the current
system date and time.
SQL NOW() Syntax
SELECT NOW() FROM table_name
FORMAT() Function
The FORMAT() function is used to format how
a field is to be displayed.
SQL FORMAT() Syntax
SELECT FORMAT(column_name,format)
FROM table_name
column_name -> Required. The field to be
formatted.
Format -> Required. Specifies the format
SQL Integrity Constraints
Integrity Constraints are used to apply business rules
for the database tables.
The constraints available in SQL are Foreign Key,
Not Null, Unique, Check.
Constraints can be defined in two ways
1) The constraints can be specified immediately after
the column definition. This is called column-level
definition.
2) The constraints can be specified after all the columns
are defined. This is called table-level definition.
SQL Primary key
This constraint defines a column or combination of
columns which uniquely identifies each row in the
table.
Syntax to define a Primary key at column level:
column name datatype [CONSTRAINT
constraint_name] PRIMARY KEY
Syntax to define a Primary key at table level:
[CONSTRAINT constraint_name] PRIMARY KEY
(column_name1,column_name2,..)
SQL Primary key
For Example: To create an employee table with Primary Key constraint, the
query would be like.
Primary Key at table level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
or
CREATE TABLE employee
( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
SQL Primary key
or
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) CONSTRAINT loc_un UNIQUE
);
SQL Unique Key
Unique Key at table level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT loc_un UNIQUE(location)
);
SQL Check Constraint
This constraint defines a business rule on a column.
All the rows must satisfy this rule. The constraint
can be applied for a single column or a group of
columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK
(condition)
For Example: In the employee table to select the
gender of a person, the query would be like
SQL Check Constraint
Check Constraint at column level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
gender char(1) CHECK (gender in ('M','F')),
salary number(10),
location char(10)
);