Cs8481 Dbms Lab Ex2
Cs8481 Dbms Lab Ex2
SQL constraints are used to specify rules for the data in a table.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a new record, or
update a record without adding a value to this field.
2) UNIQUE CONSTRAINT:
The UNIQUE constraint ensures that all values in a column are different.
*********************************************************************************
Primary keys must contain UNIQUE values, and cannot contain NULL values.
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.
ii) alter table <tablename> add constraint <constraint_name> primary key (<Attribute>);
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table.
The table containing the foreign key is called the child table, and the table containing the candidate
key is called the referenced or parent table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the table it points to.
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already
created, use the following SQL:
************************************************************************************
5) 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 single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values
in other columns in the row.
alter table Persons add constraint CHK_PersonAge check (Age>=18 and City='Chennai');
6) DEFAULT CONSTRAINT:
The default value will be added to all new records IF no other value is specified.
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():
*************************************************************************************
INDEXES:
Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see
the indexes, they are just used to speed up searches/queries.
*************************************************************************************
SQL Clauses
GROUP BY Clause:
SQL GROUP BY statement is used to arrange identical data into groups.
The GROUP BY statement follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause.
Syntax:
Example:
Having is used in a GROUP BY clause. If you are not using GROUP BY clause then you can use
HAVING function like a WHERE clause.
Syntax:
Example:
ORDER BY Clause:
The ORDER BY clause sorts the result-set in ascending or descending order.
It sorts the records in ascending order by default. DESC keyword is used to sort the records in
descending order.
Syntax:
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column1, column2... ASC|DESC;
*************************************************************************************
SQL Operators
Arithmetic Operators : +, -, *, /, %
Comparison Operators : =, !=, < >, >, <, >=, <=, !<, !>
Logical Operators : ALL, AND, ANY, BETWEEN, IN, NOT, OR, EXISTS, LIKE
Logical Operators
SELECT * FROM products WHERE quantity >= 5000 AND name LIKE 'Pen %';
SELECT * FROM products WHERE quantity >= 5000 AND price < 1.24 AND name LIKE 'Pen %';
SELECT * FROM products WHERE NOT (quantity >= 5000 AND name LIKE 'Pen %');
IN, NOT IN
SELECT * FROM products WHERE name IN ('Pen Red', 'Pen Black');
SQL ALIASES
SQL aliases are used to give a table, or a column in a table, a temporary name.
Example: