Documente Academic
Documente Profesional
Documente Cultură
CONSTRAINTS
SWE1004 – DATABASE MANAGEMENT SYSTEMS LAB
Introduction
Database
Collection of Interrelated Data
Database Management Systems
Set of programs used to manipulate database
Relational Database
A database structured to recognize relations between stored
items of information.
Table – Real World Object
Attribute / Field – Properties if a Table
Tuple / Record – an ordered set of data constituting a
record
Domain / Column – Set of all values of an attribute
Introduction
Structured Query Language (SQL)
The ANSI standard language for the definition and
manipulation of relational database.
Includes data definition language (DDL), statements that
specify and modify database schemas.
Includes a data manipulation language (DML), statements that
manipulate database content.
What is SQL?
When a user wants to get some information from a
database file, he can issue a query.
A query is a user–request to retrieve data or
information with a certain condition.
SQL is a query language that allows user to specify
the conditions. (instead of algorithms)
Why SQL?
Describe Table
Add
Table altered.
Table altered.
Table altered.
1 row created.
1 row created.
Modify – Drop
Table altered.
Table truncated.
no rows selected
Table dropped.
Table created.
Insert Command
1 row created.
DML - Explanation
Method 2 - Inserting Multiple records
1 row created.
DML - Explanation
SQL> /
Enter value for name: gilchrist
Enter value for country: australia
Enter value for matches: 50
Enter value for runs: 5000
Enter value for jersy: 3
old 1: insert into player
values('&name','&country',&matches,&runs,&jers
y)
new 1: insert into player
values('gilchrist','australia',50,5000,3)
1 row created.
DML - Explanation
Method 3 - Inserting Values for selected attributes
1 row created.
DML - Explanation
Select Command
4 rows selected.
DML - Explanation
Selecting domain values
NAME COUNTRY
-------------------- ---------------
sachin india
shewag india
gilchrist australia
pathani india
4 rows selected.
DML - Explanation
Selecting values by avoiding repetitions and duplications
COUNTRY
---------------
australia
india
DML - Explanation
Selecting tuple values based on conditions
NAME RUNS
-------------------- ----------
gilchrist 5000
DML - Explanation
Selecting tuple values based on sorting in ascending order
Selecting tuple values based on sorting with both asc and desc
3 rows updated.
DML - Explanation
Delete Command
no rows selected
Rename Table / Rename Attribute / Copy a
Table
copy a table
create table table_name as select * from table_name;
Rename a table
alter table table_name rename to new_name;
Examples:
alter table student rename column branch to course;
alter table student rename to students;
Rename Table / Rename Attribute / Copy a
Table
Grant Command
a. Granting privileges for a relation of one user to
be accessed from another user.
b. Granting privilege privileges for a user to grant
privilege for another user.
Revoke Command
Getting back the privileges given from a user.
SQL CONSTRAINTS
NOT NULL Constraint: Ensures that a column cannot have
NULL value.
DEFAULT Constraint: Provides a default value for a column
when none is specified.
UNIQUE Constraint: Ensures that all values in a column are
different.
PRIMARY Key: Uniquely identified each rows/records in a
database table.
FOREIGN Key: Uniquely identified a rows/records in any
another database table.
CHECK Constraint: The CHECK constraint ensures that all
values in a column satisfy certain conditions.
INDEX : Use to create and retrieve data from the database
very quickly.
NOT NULL CONSTRAINT
SQL> create table acct(acctno number(5) not null, balance
number(12,2) not null);
Table created.
1 row created.
NOT NULL CONSTRAINT
SHOWS ERROR WHEN NULL VALUE IS INSERTED FOR BALANCE ATTRIBUTE
Table created.
1 row created.
1 row created.
SHOWS ERROR WHEN NON UNIQUE VALUE IS INSERTED FOR NAME ATTRIBUTE
Table created.
SQL> desc stu
1 row created.
1 row created.
1 row created.
CHECK CONSTRAINT
SHOWS ERROR WHEN WRONG VALUE IS INSERTED FOR DEGREELEVEL
ATTRIBUTE
SQL> insert into stu values('jensi',104,'Mastres');
insert into stu values('jensi',104,'Mastres')
*
ERROR at line 1:
ORA-02290: check constraint (JUJO.SYS_C003210) violated
CHECK CONSTRAINT
SQL> insert into stu values('jeni',104,'Bachelors');
1 row created.
Syntax:
create table table_name(attribute_name1 data_type primary key,
attribute_name2 data_type);
Or
create table table_name(attribute_name1 data_type,
attribute_name2 data_type, primary key(attribute_name1));
Example
create table student(regno number(5)
primary key, name varchar2(15),
rollno number(8), dept varchar2(5),
doj date);
Or
create table student(regno number(5), name
varchar2(15), rollno number(8), dept
varchar2(5), doj date, primary
key(regno));
Add primary key
SQL> alter table table_name add
constraint pk primary
key(attribute_name);
Drop primary key
SQL> alter table table_name drop primary
key;
Or
SQL> alter table table_name drop pk;