DBMS Unit-2
DBMS Unit-2
DBMS Unit-2
The main construct for representing data in the relational model is a relation. A relation consists of a relation
schema and a relation instance. The relation instance is a table, and the relation schema describes the column heads
for the table.
The schema specifies the relation's name, the name of each field (or column, or attribute), and the domain of each
field. A domain is referred to in a relation schema by the domain name and has a set of associated values
e.g: Students(sid: string, name: string, login: string, age: integer, gpa: real)
An instance of a relation is a set of tuples, also called records, in which each tuple has the same number of fields
as the relation schema. A relation instance can be thought of as a table in which each tuple is a row, and all rows
have the same number of fields
Creating and Modifying Relations Using SQL
The SQL language standard uses the word table to denote relation, and we often follow this convention when
discussing SQL. The subset of SQL that supports the creation, deletion, and modification of tables is called the Data
Definition Language (DDL).
The CREATE TABLE statement is used to define a new table. To create the Students relation, we can use the
following statement:
CREATE TABLE Students ( sid CHAR(20) , name CHAR(30) , login CHAR(20) , age INTEGER, gpa
REAL);
Tuples are inserted ,using the INSERT command. We can insert a single tuple into the Students table as follows:
INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, 'Smith', 'smith@ee', 18, 3.2);
We can delete tuples using the DELETE command. DELETE FROM Students S WHERE S.name = 'Smith‘;
We can modify the column values in an existing row using the UPDATE command.
UPDATE Students S SET S.gpa = S.gpa - 0.1 WHERE S.gpa >= 3.3
INTEGRITY CONSTRAINTS OVER RELATIONS
SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the accuracy and
integrity of the data inside table.
Constraints are used to make sure that the integrity of data is maintained in the database. Following are the most
used constraints that can be applied to a table.
•NOT NULL
•UNIQUE
•PRIMARY KEY
•FOREIGN KEY
•CHECK
•DEFAULT
PRIMARY KEY is a column or group of columns in a table that uniquely identify every row in that table. The
Primary Key can't be a duplicate meaning the same value can't appear more than once in the table. A table cannot
have more than one primary key.
The syntax to create a primary key using the CREATE TABLE statement in Oracle/PLSQL is:
CREATE TABLE table_name ( column1 datatype null/not null, column2 datatype null/not null, ...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n) );
NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint is applied to a
column, you cannot pass a null value to that column. It enforces a column to contain a proper value.
One important point to note about this constraint is that it cannot be defined at table level.
CREATE TABLE Student(sid int NOT NULL, Name varchar(60), Age int);
UNIQUE Constraint
UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE constraint field will not
have duplicate data. This constraint can be applied at column level or table level.
CHECK constraint is used to restrict the value of a column between a range. It performs check on the values, before
storing them into the database. Its like condition checking before saving data into a column.
CREATE table Student(sid int CHECK(sid > 0), Name varchar(60) NOT NULL, Age int );
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with
no repeated attributes. The Primary key should be selected from the candidate keys. Every table must have at least a
single candidate key. A table can have multiple candidate keys but only a single primary key.
ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that table. A
table can have multiple choices for a primary key but only one can be set as the primary key. All the keys which are
not primary key are called an Alternate Key.
Super Key: The set of attributes which can uniquely identify a tuple is known as Super Key. For Example,
STUD_NO, (STUD_NO, STUD_NAME) etc.
The following insertion violates the primary key constraint because there is already a tuple with the s'id 53688,
and it will be rejected by the DBMS:
INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, 'Mike', 'mike@ee', 17,3.4);
The following insertion violates the constraint that the primary key cannot contain null:
INSERT INTO Students (sid, name, login, age, gpa) VALUES (null, 'Mike', 'mike@ee', 17,3.4);
We can retrieve rows corresponding to students who are younger than 18 with the following SQL query:
A view can contain all rows of a table or select rows from a table. A view can be created from one or many tables
which depends on the written SQL query to create a view.
Views, which are a type of virtual tables allow users to do the following
Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple
tables or another view.
To create a view, a user must have the appropriate system privilege according to the specific implementation.
CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition];
SQL > CREATE VIEW CUSTOMERS_VIEW AS SELECT name, age FROM CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table. Following is an
example for the same.
The SQL ALTER TABLE command is used to add, delete or modify columns in an existing table. You should also
use the ALTER TABLE command to add and drop various constraints on an existing table.
Syntax
The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows.
The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as
follows.
The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to a column in a table is as
follows.
The basic syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as follows.
The basic syntax of an ALTER TABLE command to ADD CHECK CONSTRAINT to a table is as follows.
The basic syntax of an ALTER TABLE command to DROP CONSTRAINT from a table is as follows.
The WHERE clause can be used to delete a record from table / view with conditions.
A tuple variable is a variable that takes on tuples of a particular relation schema as values. That is, every value
assigned to a given tuple variable has the same number and type of fields.
A tuple relational calculus query has the form { T I p(T) }, where T is a tuple variable and p(T) denotes a
formula that describes T
The result of this query is the set of all tuples t for which the formula p(T) evaluates to true with T = t.
The language for writing formulas p(T) is thus at the heart of TRC and essentially a simple subset of first-order
logic.
Domain Relational Calculus