Dbms Lab File Bca 507p 6
Dbms Lab File Bca 507p 6
1.2 THEORY AND CONCEPTS: A table is the main database object. A table is defines with
columns and stores rows of data. A table should have atleast one column.
Creating tables:
Syntax:
CREATE TABLE <table name>
( <columnname1> datatype(size),
….
<columnname n> datatype(size)
);
Describing a table:
Syntax
DESCRIBE <table name> or DESC <table name>
Adding columns:
Syntax
ALTER TABLE <table name> ADD <columnname> <datatype(size)>;
Modifying columns:
Syntax
ALTER TABLE <table name> MODIFY (<columnname> <new datatype(size)>);
Updating tables:
Syntax
UPDATE TABLE <table name> SET <columnname>=’value’
WHERE <condition>;
Dropping tables:
Syntax
DROP TABLE <table name>;
Renaming tables:
Syntax
RENAME <old table name> TO <new table name>;
Creating constraints: Integrity constraints prevent bad data from being entered into the
database. These constraints can be specified at the column level or at the table level by
CREATE TABLE or ALTER TABLE command. These are as follows:
NOT NULL: prevents NULL values from being entered into the column. It is defined at the
column level. It cannot be defined at the table level.
Syntax
CREATE TABLE <table name>
(<columnname> <datatype(size)> CONSTRAINT <constraint name > NOT NULL);
Or
ALTER TABLE <table name> MODIFY <columnname> NOT NULL;
CHECK: constraint can be defined at the column level as well at the table level. The
condition specified in the CHECK clause should evaluate to a Boolean result and can refer
to values in other columns of the same row; the condition cannot use queries.
Syntax
CREATE TABLE <table name>
(<columnname> <datatype(size)>,
CONSTRAINT CHECK <constraint name> (<condition>));
or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name> CHECK
(<condition>);
UNIQUE: constraint protects one or more columns in a table, ensuring that no two rows
contain duplicate data in the protected columns. It can be specified at the column level as
wella s at the table level.
Syntax
Columns level: CONSTRAINT <constraint name> UNIQUE
Table level: CONSTRAINT <constraint name>
UNIQUE(<columnname1>,….,<columnname n>);
Or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name>
UNIQUE(<columnname1>,….,<columnname n>);
PRIMARY KEY: All characteristics of the unique key are applicable to the primary key
constraint, except the NULL values are not allowed in the primary key columns. A table
can have only one primary key.
Syntax
DBMS Lab (BCA-507(P))
Column level: CONSTRAINT <constraint name> PRIMARY KEY
Table level: CONSTRAINT <constraint name>
PRIMARY KEY (<columnname1>,….,<columnname n>);
Or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name> PRIMARY KEY
(<columnname1>,….,<columnname n>);
FOREIGN KEY: constraint protects one or more columns in a table by ensuring that for
each non-NULL value there is data available elsewhere in the database with a primary or
unique key.
Syntax
Column level: CONSTRAINT <constraint name> REERENCES <table
name>(<columnname1>,….,<columnname n>);
Table level: CONSTRAINT <constraint name>
FOREIGN KEY (<columnname1>,….,<columnname n>)
REERENCES <table name>(<columnname1>,….,<columnname n>);
Or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name>
FOREIGN KEY (<columnname1>,….,<columnname n>)
REERENCES <table name>(<columnname1>,….,<columnname n>);
Dropping constraints:
Syntax
ALTER TABLE <table name> DROP <constraint name>
ASSIGNMENTS:
Assignment 8
Que 1. Create the following tables:
Student
Column Name Data Type Size Constraint(s)
Rollno Number 10 Primary Key
Sname Varchar2 30 Not Null
Status Varchar2 10 Reg/Ex/Readmitted
Sem Number 1 Not Null
DateofReg Date Not Null
Rank Number 10 Unique
Scholarship Number 10 --
BranchNo Varchar2 5 Foreign Key
Department
Column Name Data Type Size Constraint(s)
BranchNo Varchar2 5 Primary Key
BranchName Varchar2 30 Not Null
HOD Varchar2 30 Not Null