Data Definition Language - PDF
Data Definition Language - PDF
SQL Identifiers
Used to identify objects in the database, such as table names, view
names, and columns.
Default characters: A-Z, a-z, 0-9, and the underscore.
Restrictions on identifiers
An identifier can be no longer than 128 characters
An identifier must start with a letter
An identifier cannot contain spaces.
Creating a Database
Fixed Length:
Occupies the same length of space in memory no
matter how much data is stored in them.
Syntax:
char(n) where n is the length of the String
e.g. name char(50)
If the variable stored for name is ‘Sanjay’ the extra
43 fields are padded with blanks
String Data
Example:
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
PRIMARY KEY (studio_id),
UNIQUE (name),
UNIQUE(city, state)
)
Specfying Keys – Single and MultiColumn Keys
Example:
Example:
INSERT INTO city_state
SELECT studio_city, studio_state FROM studios
Example:
DELETE FROM City_State
WHERE state = ‘TX’
Deletes all the rows where the state is Texas keeps all the
other rows.
Update Statement
Update Statement:
used to make changes to existing rows of the table. It has three parts.
First, you ,must specify which table is going to be updated. The
second part of the statement is the set clause, in which you should
specify the columns that will be updated as well as the values that will
be inserted. Finally, the where clause is used to specify which rows will
be updated.
Syntax:
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, …..
[WHERE Condition]
Example:
UPDATE studios
SET studio_city = ‘New York’, studio_state = ‘NY’
WHERE studio_id = 1
Notes1: If the condition is dropped then all the rows are updated
Truncate Statement
Truncate Statement:
used to delete all the rows of a table. Delete can also be used to
delete all the rows from the table. The difference is that delete
performs a delete operation on each row in the table and the database
performs all attendant tasks on the way. On the other had the
Truncate statement simply throws away all the rows at once and is
much quicker. The note of caution is that truncate does not do integrity
checks on the way which can lead to inconsistencies on the way. If
there are dependencies requiring integrity checks we should use
delete.
Example:
TRUNCATE TABLE studios
Drop Statement:
used to remove elements from a database, such as
tables, indexes or even users and databases. Drop
command is used with a variety of keywords based on
the need.
Syntax:
ALTER TABLE table_name
ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
Example:
ALTER TABLE studios
ADD (revenue Number DEFAULT 0)
Alter Statement
Add table level constraints:
Syntax:
ALTER TABLE table_name
ADD ([CONSTRAINT constraint_name CHECK comparison]
[columns REFERENCES table_name (columns)]
Example:
ALTER TABLE studios
ADD (CONSTRAINT check_state CHECK (studio_state in (‘TX’, ‘CA’, ‘WA’))
Modify Columns:
Syntax:
ALTER TABLE table_name
MODIFY column [data type]
[Default Expression]
[REFERENCES table_name (column_name)’
[CHECK condition]
Example:
ALTER TABLE People
MODIFY person_union varchar(10)
Notes1: Columns can not be removed from the table using alter. If you
want to remove columns you have to drop the table and then recreate it
without the column that you want to discard
Alter Statement
Alter Statement:
used to make changes to the schema of the table. Columns
can be added and the data type of the columns changed as
long as the data in those columns conforms to the data type
specified.
Syntax:
ALTER TABLE table_name
ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
Example:
ALTER TABLE studios
ADD (revenue Number DEFAULT 0)
Creating a View
Format: