Professional Documents
Culture Documents
SQL
• Pick Fields
• Specify a Correct data type and size
• Apply constraints on the columns
Planning the data type
• This characteristics describes the type of data that the
column will contain, whether it is a character, number
or a data.
• Different software packages will have different data
type names for representing data.
Text Number
Data Types
Example
CREATE TABLE Student
( Std_Id int (3) ,
Std_name varchar(15) ) ;
Modifying Table Structure
Syntax
ALTER TABLE <table_name>
{ALTER COLUMN <column_name> <new_data_type>}
| {ADD [<column_name> <data_type>]}
| {DROP COLUMN <column_name>}
Example
ALTER TABLE Airlines_Master
ADD COLUMN (NoOfAircraft INT)
Modifying Table Contents
Syntax: Adding rows
INSERT [INTO] <table_name> VALUES <values>
Syntax
DROP TABLE <Table_Name>
Example
DROP TABLE Airlines_Master
Modifying Table Structure
Syntax
ALTER TABLE <table_name>
{ALTER COLUMN <column_name> <new_data_type>}
| {ADD [<column_name> <data_type>]}
| {DROP <column_name>}
Example
ALTER TABLE Student
ADD Address TEXT(20)
Constraints
A property that can be placed on a column
or set of columns in a table
Enforces limitations on data entered in a
particular column
Can be defined at the time of table
creation, or added later
Enforcing Constraints
• Constraints are
limitations placed on
incoming data
Constraints
Foreign Key
PRIMARY KEY Constraint
A column or set of columns that identifies
each table row uniquely
Syntax
CREATE TABLE Table_name
(<Column_definition> CONSTRATINT <Name of Constraint> PRIMARY KEY)
Example
CREATE TABLE Reservation_copy
( PNR_no number CONSTRAINT pk_res PRIMARY KEY )
Decision for the Primary Key
• There’s no absolute rule as to which
candidate key is best
• Fabian Pascal, in his book SQL and
Relational Basics, notes that decision
should be based upon the Principles of
– Minimality
– Stability
– Simplicity