SQL-Data Definition Language: Sanjay Goel, School of Business, University at Albany, Suny
SQL-Data Definition Language: Sanjay Goel, School of Business, University at Albany, Suny
Note: Different databases name their numeric fields differently and may not
support all numeric types. They may also support additional numeric
types.
Sanjay Goel, School of Business, University at Albany,
SUNY 9
DDL
Temporal Data Types
• These represent the dates and time:
• Three basic types are supported:
– Dates
– Times
– Date-Time Combinations
• 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)
)
• Example:
DML
Modifying Records
• Example:
DELETE FROM City_State
WHERE state = ‘TX’
• Deletes all the rows where the state is Texas keeps all the
other rows.
• 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.
Sanjay Goel, School of Business, University at Albany,
SUNY 26
Modifying Records
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
• 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)
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)
• 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)