The document outlines the design and development of SQL DDL statements, focusing on various SQL objects such as tables, views, indexes, sequences, and synonyms. It provides syntax and examples for creating and altering tables, defining constraints, and managing database objects. Additionally, it explains the purpose of each SQL object and the rules for their usage in database management.
The document outlines the design and development of SQL DDL statements, focusing on various SQL objects such as tables, views, indexes, sequences, and synonyms. It provides syntax and examples for creating and altering tables, defining constraints, and managing database objects. Additionally, it explains the purpose of each SQL object and the rules for their usage in database management.
Assignment No. 2 (a)
Title: Design and Develop SQL DDL statements which demonstrate the use of SQL objects
such as Table, View , Index, Sequence, Synonym
Objectives: To study SQL DDL statements
Theory: SQL~ Structured Query Language
Data
Definition in
SQLCreating
Tables
Syntax:-
Create table
(colume_name 1
datatype size),
colume_name 2
datatype size(),
colume_name n datatype size());
e.g. Create table student with the following fields(name,roll,class,branch)
Create table student
(name
char(20),
Roll
number(
5), Class
char(10),
Branch
char(15));
Atable from a table
+ Synta
CREATE TABLE(,
)ASSELECT ,
FROM ;
- If the source table contains the records, then new table is also created with the same
records present in the source table.
Department of Computer Engineering, SCOE, Pune Page 19If you want only structure without records then select statement must havecondition.
Syntax:
CREATE TABLE (, ) ASSELECT
, FROM WHERE 1=2; (Or)
CREATE TABLE (, ) ASSELECT
, FROM WHERE ColumnName
JULL;
Constraints
The definition of a table may include the specification of integrity constraints. Basicallytwo
types of constraints are provided: column constraints are associated with a single column
whereas table constraints are typically associated with more than one column. Aconstraint
can be named. It is advisable to name a constraint in order to get more meaningful
information when this constraint is violated due to, e.g,, an insertion of a tuple that violates
the constraint. If no name is specified for the constraint, Oracle automatically generates a
name of the pattern SYS Ccnumber>.Rules are enforced on data being stored in a table, are
called Constraints.
Both the Create table & Alter Table SQL can be used to write SQL
sentences thatattach constraints.
Basically constraints are of three types :
1) Domain
- Not Null
~ Check
2) Entity
- Primary Key
~ Unique
3) Referential
- Foreign key
4) Not Null:-Not null constraint can be applied at column level only.
‘We can define these constraints
1) at the time of table creation Syntax :
CREATE TABLE ( datatype(size) NOT NULL,
datatype(size),..._);
Department of Computer Engineering, SCOE, Pune Page 202) After the table creation
ALTER TABLE
Modify( datatype(size)
NOT NULL);
Check constraints
Can be bound to column or a table using CREATE TABLE
or ALTER TABLEcommand.
Checks are performed when write operation is performed .
- Insert or update statement causes the relevant check constraint.
- Ensures the integrity of the data in tables.
‘Syntax :
+ Check constraints at column levelSyntax :
CREATE TABLE
(data
type(size)CHECK(column Namecondition),
( data type(size) CONSTRAINT
CHECK (column Name condition)...
%
* Check constraints at table level Syntax :
(CREATE TABLE
( data type(size),
data type(size),
CONSTRAINT CHECK (column Name condition),
Check constraints at table level
Syntax:
CREATE TABLE
( data type(size),
data type(size).....,
CHECK (column Name
condition));After table
creation
Alter table tablename
Add constraints constraintname
Department of Computer Engineering, SCOE, Pune Page 21check(condition)The PRIMARY KEY Constraint
Aprimary key is one or more column(s) in a table used to uniquely identity
each row inthe table.
* Atable can have only one primary key. Can not be left blank Data must be
UNIQUE.
* Not allows null values
* Not allows duplicate values.
+ Unique index is created automatically if there is a
primary key. Primary key constraint defined at column level
Syntax:
(CREATE TABLE
( ()PRIMARY
KEY,).....);
* Primary key constraint defined at
Table levelSyntax:
(CREATE TABLE
( () ssPRIMARY
KEY());
* key constraint defined at Table level
Syntax:
CREATE TABLE