0% found this document useful (0 votes)
9 views33 pages

DDL and SQL Commands Chapter 4

Uploaded by

evan.ibarra12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views33 pages

DDL and SQL Commands Chapter 4

Uploaded by

evan.ibarra12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

DATA DEFINITION IN SQL

HONEY GIRL A. AVO


CONTENT
◼ Data Definition Language (DDL)
◼ Creating a Table
◼ Deleting a Table
◼ Truncate a Table
◼ Modifying a Table Structure
◼ Data Manipulation Language (DML)
◼ Inserting records into a table
◼ Exercises
DATA DEFINITION LANGUAGE (DDL)
DATA DEFINITION LANGUAGE

◼ The SQL language has facilities to create, manipulate and


delete (drop) tables
◼ These command line activities are duplicated through a GUI
(such as the one in Access)
CREATING A TABLE

◼ Syntax:
CREATE TABLE tablename
(column_name type [NULL/NOT NULL],
column_name type [NULL/NOT NULL],
column_name type [NULL/NOT NULL]
..);
RELATIONAL MODEL RULES

◼ Each tablename must be unique for the database


◼ Each column_name must be unique for each relation/table
COLUMN_NAME

◼ May be up to 30 characters in length starting with initial


alphabetic character
◼ The name may consist of alphanumeric characters and the
special characters _, $, # or @
TYPE

Type Description
CHAR (size) Text data, maximum of ‘size’ characters up to 240
LONG Character data up to 65535 (some restrictions may
apply on the use of this field in a select statement)
NUMBER Maximum of 40 digits (will accept scientific
notation)
NULL AND NOT NULL

◼ These indicate whether the field will allow NULL values (which
is the default) or whether all cells must have a value
EXAMPLE 1

◼ Creates a table called branch


with the three fields
branchno, street and
city
◼ Attempting to insert a
record with null for
branchno will generate an
error as you have defined it
as not null (meaning not
empty)
EXAMPLE 2

◼ Creates a table called staff


with six fields: staff number
(staffno), employee first and
last name (fname, lname), job
title (job), salary (sal) and
branch number (branchno)
◼ Null values will be allowed in
the branchno field
DELETING A TABLE

◼ This means to ‘drop’ a table


◼ Use the drop command to permanently delete a table
◼ Syntax:
DROP table tablename;
◼ Note: most databases regard this as an irreversible process
◼ no undo features are typically supplied
EXAMPLE

DROP table branch; ◼ Deleting the branch table


TRUNCATE A TABLE

◼ This removes data entries from a table in a database, but keeps


the table, its data type and column parameters
◼ Syntax:
TRUNCATE TABLE table_name;
EXAMPLE

◼ Deleting all the rows from


the table staff and free the
TRUNCATE table staff;
space containing the table
staff
MODIFYING A TABLE STRUCTURE
◼ Use the alter table command to change a table structure
◼ Syntax:
ALTER table tablename
(
[MODIFY columnname type |
ADD columnname type ]
);
EXAMPLE 1

ALTER table staff add spouses_name char(15);

◼ To add a spouses_name field to the staff table


EXAMPLE 1I

ALTER table staff alter column lname char(20);

◼ To increase the size of the lname column


NOTE:

◼ Different databases will react in different ways if attempts are


made to:
◼ Delete columns where there is data present
◼ Decrease the size of a column where data is present
◼ Change NULL columns to NOT NULL
◼ Convert a column to a different type
DATA MANIPULATION LANGUAGE (DML)
DATA MANIPULATION LANGUAGE (DML)

◼ Most SQL queries allow views on the original data, without


manipulating the original data set
◼ Actual changes to rows (records or tuples) in a table are done
through the Insert, Update or Delete statements
THE INSERT STATEMENT

◼ This adds records (rows) to a table


◼ It has two forms
FORM 1

◼ This will insert a record using a supplied column list the


supplied values
◼ If no column list is supplied the record will be inserted as is,
which may generate errors if the columns don’t match up.
EXAMPLE

◼ Inserting a new record into the branch table


◼ If a field is left off the list but is defined as NOT NULL an error
message will be generated
◼ Autoincrementing key fields can be left off the insert list and
the appropriate values will be calculated and pasted in
FORM 2

◼ This form allows an insert to be based on the results of a


select query
EXAMPLE

Insert into client (fname, lname, telno)


Select fname, lname, telno
FROM owner;

◼ Inserting the result of the query into the table client


EXERCISES
DDL AND DDM ACTIVITIES
WRITE THE SQL COMMAND OF THE FOLLOWING:
WRITE THE SQL COMMAND OF THE FOLLOWING:
WRITE THE SQL COMMAND OF THE FOLLOWING:
WRITE THE SQL COMMAND OF THE FOLLOWING:

You might also like