Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Definition Language

Similar presentations


Presentation on theme: "Data Definition Language"— Presentation transcript:

1 Data Definition Language
Randy Moss

2 Agenda Learn how databases are created
Create and alter the field structure of tables

3 Database Definition Language (DDL)
Subset of SQL commands to create and modify database objects Can create an entire database using SQL rather than a graphical user interface With DDL you gain more control over exactly what database obejcts are built and what they are named DDL saved into a script can be easily ported from one database installation to another

4 Naming tips Using consistent names
Do not include spaces in your table or fields Avoid the use of SQL reserved words and punctuation marks in table and field names Keep names short, but long enough to be descriptive Be consistent in your use of abbreviations Name primary key and foreign key fields consistently Avoid generic field names, such as ID or Date

5 Creating a Table Syntax: CREATE TABLE tableName ( )
fieldname datatype null | not null, fieldname datatype null | not null )

6 Example of Creating a Table
CREATE TABLE Contracts ( ArtistID Integer NOT NULL, ContractDate DateTime NOT NULL );

7 Dropping a Table DROP TABLE tableName; Example: DROP TABLE Contracts;

8 Adding Columns to an Existing Table
Syntax: ALTER TABLE tableName ADD columnName dataType null | not null; Example: ALTER TABLE Contracts ADD ContractType VarChar(10) NULL;

9 Altering Existing Columns in a Table
Syntax: ALTER TABLE {tableName} MODIFY COLUMN {columnName} {dataType} {null | not null}; Example: # Change the length on the ContractType field to 15 in the Contracts Table ALTER TABLE Contracts MODIFY ContractType VarChar(15);

10 Dropping a Column Syntax: ALTER TABLE {tableName}
DROP COLUMN {columnName}; Example: # Remove the ContractType column from Contracts table ALTER TABLE Contracts DROP COLUMN ContractType;

11 Columns and Sequences Techniques to automatically generated a sequential number with each row that is inserted Never have to insert a value to it, but it will always be unique within the table Make excellent primary key in cases where there is no naturally occurring primary key

12 Column Sequence Example
CREATE TABLE tbl ( ID int AUTO_INCREMENT NOT NULL, anotherField int null, primary key(ID) );

13 Sequences Vs. Guid Columns
An identity column (or sequence) is unique within a given a table in a given database on a given server With distributed database, identity column may not\be unique across every instance of the table One solution is to use the identity column in conjunction with a locationID as the primary key Another solution Globally Unique Identifier (GUID), which is supported in some databases A GUID is a 16-byte ID guaranteed to be unique across every instance of a distributed database GUIDs are cumbersome to work with: C1C5FCE2-879E-4A96-B845- 9F3FC4A10EC8


Download ppt "Data Definition Language"

Similar presentations


Ads by Google