Sie sind auf Seite 1von 7

EXPERIMENT-3

Aim – To Create The Table Of Your Project Using Various Constraints.

CREATE TABLE COMMAND Tables are defined with the CREATE TABLE
command. It is used to specify a new relation by giving its name and specifying its
attributes and initial constraints. When a table is created, its columns are named, data
types, sizes and any attribute constraints are supplied for each column. Each table must
have at least one column. The key, entity integrity, and referential integrity constraints
can be specified within the CREATE TABLE statement after the attributes are declared.

PURPOSE The purpose of CREATE TABLE command is to create tables. Typically,


the SQL schema in which the relations are declared is implicitly specified in the
environment in which the CREATE TABLE statements are executed.

SYNTAX The syntax of the CREATE TABLE command is :


CREATE TABLE <table-name>
(<column-name> <datatype> [(size)] <column constraint>,
<column-name> <datatype> [(size)] <column constraint> …);

EXAMPLE To create an student progress card table whose scheme is as follows:


Studprogrcard(admission number, external, internal, sessional, university)

CREATE TABLE STUREPT1 // CREATE TABLE COMMAND USED


(ADNO NUMBER(6),
EXTMARKS NUMBER(3),
INTMARKS NUMBER(3),
SESSTEST NUMBER(3),
UNIVTEST NUMBER(3));

CONSTRAINTS A constraint is a condition or check applicable on a field or set of


fields. SQL include key and referential integrity constraints, as well as restrictions on
attribute domains and NULLs and constraints on individual tuples within a relation. The
two basic type of constraints are column constraints and table constraints. The difference
between the two is that column constraints apply only tro individual columns, whereas
table constraints apply to groubs of one or more columns These constraints ensure
database integrity, thus are sometimes called database integrity constraints. A few of
them are
1. Unique Constraint
2. Primary key Constraint
3. Default Constraint
4. NOT NULL Constraint
5. Foreign key constraint
6. Check constraint

UNIQUE CONSTRAINT The constraint ensures that no two rows have the same
value in the specified column(s). For example, UNIQUE constraint can be applied as
1. At column level

CREATE TABLE STUREPT1


(ADNO NUMBER(6) UNIQUE, // UNIQUE CONSTRAINT USED
EXTMARKS NUMBER(3),
INTMARKS NUMBER(3),
SESSTEST NUMBER(3),
UNIVTEST NUMBER(3));

2. At table level

CREATE TABLE STUREPT2


(ADNO NUMBER(6),
EXTMARKS NUMBER(3),
INTMARKS NUMBER(3),
SESSTEST NUMBER(3),
UNIVTEST NUMBER(3),
UNIQUE (ADNO) );

UNIQUE constraint applied on ADNO of table STUREPT ensures that no rows have the
same ADNO value.
PRIMARY KEY CONSTRAINT This constraint declares a column as the primary
key of the table. This constraint is similar to unique constraint except that only one
column (or group of columns) can be applied in this constraint. The primary keys cannot
allow NULL values, thus, this constraint must be applied to columns declared as NOT
NULL. Consider the following example
1. At column level 

CREATE TABLE STUDATA1


(ADNO NUMBER(6) PRIMARY KEY, //PRIMARY KEY CONSTRAINT
DOA DATE,
NAME CHAR(9),
DOB DATE,
SEX CHAR(6),
CURICLR CHAR(10),
CLASS VARCHAR2(4),
RELIGION CHAR(12),
LOCATION CHAR(13),
CITY CHAR(7),
PINCODE NUMBER(8),
PH_NO NUMBER(12));

2. At table level 

CREATE TABLE STUDATA2


(ADNO NUMBER(6)
DOA DATE,
NAME CHAR(9),
DOB DATE,
SEX CHAR(6),
CURICLR CHAR(10),
CLASS VARCHAR2(4),
RELIGION CHAR(12),
LOCATION CHAR(13),
CITY CHAR(7),
PINCODE NUMBER(8),
PH_NO NUMBER(12),
PRIMARY KEY(ADNO));

PRIMARY KEY constraint applied on ADNO of table STUDATA ensures that no rows
have the same ADNO value and there are no NULL values.

DEFAULT CONSTRAINT A default value can be specified for a column using the
DEFAULT clause. When a user does not enter a value for the column (having the default
value), automatically the defined default value is inserted in the field. Consider the
following 

CREATE TABLE STUFEE


( RECNO NUMBER(8) PRIMARY KEY,
RNO NUMBER(7),
RECDATE DATE,
TUTNFEE NUMBER(10),
BUILDFUND NUMBER(8) DEFAULT 2000, // DEFAULT CONSTRAINT USED
COMPFEE NUMBER(5),
LABFEE NUMBER(5),
SPRTSFEE NUMBER(5),
LATEFEE NUMBER(5),
TOTALFEE NUMBER(14));
According to the above command, if no value is provided for BUILDFUND, the default
value of 2000, will be entered.

NOT NULL CONSTRAINT A constraint NOT NULL may be specified if NULL is


not permitted for a particular attribute. This is always implicitly specified for the
attributes that are the part of the primary key of each relation, but it can be specified for
any other attributes whose calues are required not to be NULL. Consider the following
example
CREATE TABLE STUPRSNL
(ADNO NUMBER(6),
FNAME CHAR(12) NOT NULL, // NOT NULL CONSTRAINT USED
MNAME CHAR(10),
LNAME CHAR(10),
FOCC CHAR(12),
RELIGION CHAR(12),
LOCATION CHAR(13),
CITY CHAR(7),
PINCODE NUMBER(8),
PH_NO NUMBER(12));

According to the above command, the FNAME can never be NULL as the constraint
NOT NULL used.
FOREIGN KEY CONSTRAINT Foreign keys represent relationships between
tables. A foreign key is a column (or a group of columns) whose values are derived from
the primary key of the same or some other table. The existence of a foreign key implies
that the table with the foreign key is related to the primary key table from which the
foreign key is derived. A foreign key must have a corresponding primary key value in the
primary key table to have a meaning. Consider the following example

1. At column level

CREATE TABLE STUREPT3


(ADNO NUMBER(6) REFERENCES STUDATA(ADNO),
EXTMARKS NUMBER(3),
INTMARKS NUMBER(3),
SESSTEST NUMBER(3),
UNIVTEST NUMBER(3));

2. At Table Level 

CREATE TABLE STUREPT4


(ADNO NUMBER(6),
EXTMARKS NUMBER(3),
INTMARKS NUMBER(3),
SESSTEST NUMBER(3),
UNIVTEST NUMBER(3),
FOREIGN KEY(ADNO) REFERENCES STUDATA(ADNO));
CHECK CONSTRAINT It is used when we need to enforce integrity rules that can
be evaluated based on a logical expression. That is, it is used to utilize to check the
validity of data entered into particular table columns. Consider the following example

CREATE TABLE STUDENT


(ROLLNO VARCHAR2(4) CHECK(ROLLNO LIKE ‘C%’),
NAME VARCHAR2(20),
ADDRESS VARCHAR2 (30) ,
MARKS NUMBER(5,2) CHECK(MARKS > 40));

Das könnte Ihnen auch gefallen