12 It
12 It
12 It
Class: XII
Topic: Unit-1:
Subject: Information Technology (802) SQL Database Concept
Database Management Software (DBMS) simplifies the task of managing the data and extracting
useful information out of it.
Constraints, are restrictions on the values, stored in a database based on the requirements.
For example, in the relation EMPLOYEE, the Employee_ID must be a 4-digit number, the
Date_of_Birth must be such that the birth year > 1985.
We describe below various types of constraints in Relational model:
1. Domain Constraint: It specifies that the value of every attribute in each tuple must be
from the domain of that attribute. For example, the Employee_ID must be a 4-digit
number. Hence a value such as “12321” or “A234” violates the domain constraint as the
former is not 4-digit long and the letter contains an alphabet.
2. Key Constraint: Before we can explain this constraint, we need to describe the terms
superkey, key, candidate key and primary key.
3. Null Value Constraint: Sometimes it is required that certain attributes cannot have null
values. For example, if every EMPLOYEE must have a valid name then the Name attribute
is constrained to be NOT NULL.
4. Entity Integrity Constraint: This constraint specifies that primary key of a relation
cannot have null value. The reason behind this constraint is that we know primary key
contains no duplicates.
Self-Referencing Tables: A foreign key constraint can reference columns within the
same table. These tables are called as self-referencing tables.
Naming of Constraint: A constraint can be named by using the keyword CONSTRAINT followed
by the name of the constraint and its specification.
CREATE TABLE Teacher ( Teacher_ID INTEGER, First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20), Gender CHAR(1), Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE, Dept_No INTEGER, CONSTRAINT TEACHER_PK PRIMARY KEY
(Teacher_ID), CONSTRAINT TEACHER_FK FOREIGN KEY (Dept_No) REFERENCES
Department(Dept_ID) ON DELETE SET NULL ON UPDATE SET NULL );
Drop Table Command: This command is used to delete tables. For example, suppose
you want to drop the Teacher table then the command would be:
Types of SQL Commands
MySQL follows SQL specifications for its commands. These SQL commands can be categorized as
(DDL, DML)–
Data Definition Language (DDL): These SQL commands are used to create, alter and delete
database objects like table, views, index etc.
Example : CREATE , ALTER , DROP etc.
Data Manipulation Language (DML): These commands are used to insert, delete, update and
retrieve the stored records from the table.
Ex. SELECT…., INSERT…, DELETE…, UPDATE…. etc.
Alter Table Command: This command is used to modify the base table definition. The
modifications that can be done using this command are:
Adding a column: Suppose we want to add a column Age in the Teacher table.
Following command is used to add the column:
ALTER TABLE Teacher ADD Age INTEGER;
Dropping a column: A column can be dropped using this command but one must specify the
options (RESTRICT or CASCADE) for the drop behavior. As discussed earlier, RESTRICT would
not let the column be dropped if it is being referenced in other tables and CASCADE would drop
the constraint associated with this column in this relation as well as all the constraints that refer this
column.
ALTER TABLE Teacher DROP Dept_No CASCADE;
Altering a Column: A column definition can also be altered. For example – dropping the default
value or defining a new default value. For example, in the Teacher table the default value of Salary
is 40000. If you want to drop this default value or change this value to 30000 then it can be done by
using the following commands:
ALTER TABLE Teacher ALTER Salary DROP DEFAULT;
ALTER TABLE Teacher ALTER Salary SET DEFAULT 30000;
Dropping keys: A foreign key/primary key/key can be dropped by using ALTER TABLE
command. For example if you want to delete the foreign key TEACHER_FK in the Teacher table
then following command can be used:
ALTER TABLE Teacher DROP FOREIGN KEY TEACHER_FK;
Insert Command: This command is used to insert a tuple in a relation. We must specify the name
of the relation in which tuple is to be inserted and the values. The values must be in the same order
as specified during the Create Table command.
INSERT INTO Teacher VALUES (101,"Shanaya", "Batra", 'F', 50000, '1984-08-11', 1);
Update Command: This command is used to update the attribute values of one or more tuples in a
table. For example in the Teacher table, we want to update the Salary of teacher with
Teacher_ID=101 to 55000. This can be done using the following command:
UPDATE Teacher SET Salary=55000 WHERE Teacher_ID=101;
Delete Command: In order to delete one or more tuples, DELETE command is used. If we want to
delete the tuple for Teacher with ID=101 the command would be:
DELETE FROM Teacher WHERE Teacher_ID=101;
Select Command: The SELECT Command is used to retrieve information from a database. There
are various ways in which the SELECT command can be used. Syntax of SELECT Command is as
follows:
SELECT <attribute list> FROM <table list> WHERE <condition>