Download part 2 - LaBRI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SQL: DDL
SQL Statements
• DDL - data definition language
– Defining and modifying data structures (metadata):
database, tables, views, etc.
• DML - data manipulation language
– Manipulating user data: insert, update, select, etc.
• DCL - data control language
– Control data access: permissions, etc.
SQL DDL
• DDL statements include
– CREATE Table
– ALTER Table
– DROP Table
3
Create Table
CREATE TABLE TableName
(
ColumnDefinitions,
Table Constraints
)
Column Definition
• What to define?
– Column name (required)
– Data type and length (required)
– Column constraints (optional): primary key, null, not
null, default, unique, check
• Example
FirstName VARCHAR(30) NOT NULL,
5
Data Types
• Data types are slightly different in many database products
• Data types reference
– Access 2007:
http://msdn.microsoft.com/en-us/library/bb208866.aspx
– SQL Server 2005:
http://msdn.microsoft.com/en-us/library/ms187752(SQL.90).aspx
6
Example
Example: Using Table Constraints
Example: Foreign Key
ALTER Table
• Alter Table TableName
[Modification]
• Modification include
– Add, modify, drop columns
– Add, modify, drop table constraints
Altering Columns
• Adding a new column • Modify a column
ALTER TABLE tablename
MODIFY (column newdatatype);
ALTER TABLE tablename
ADD (column datatype);
ALTER TABLE Customer
ADD (FNAME VARCHAR(30));

Drop a column
ALTER TABLE tablename
DROP COLUMN colummname;
ALTER TABLE Customer
MODIFY (FName VARCHAR(40));
ALTER TABLE Customer
DROP COLUMN FName;
11
Altering Table Constraints
• Add a foreign key
Alter table GroupAssignment
add constraint GroupAssignment_FK2 foreign
key(GroupNumber)
references Groups(GroupId) on update cascade;
DROP Table
DROP TABLE tablename;
DROP TABLE Customer;
Warning… The DROP statement
will permanently remove table
structure and all data
13
Summary
• Key concepts
–
–
–
–
–
–
DDL
Create
Alter
Drop
SQL Data types
Referential integrity, cascade
Related documents