0% found this document useful (0 votes)
104 views

CHAPTER 3-DDsql

The document discusses SQL data definition and data manipulation languages. It defines how to create, alter and delete tables using SQL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, and how to insert, update and delete records from tables using INSERT, UPDATE, DELETE statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

CHAPTER 3-DDsql

The document discusses SQL data definition and data manipulation languages. It defines how to create, alter and delete tables using SQL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, and how to insert, update and delete records from tables using INSERT, UPDATE, DELETE statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

DATA

DEFINITION
IN SQL
Data Definition Language (DDL)

The SQL language has facilities to


create, manipulate and delete (drop)
tables. Often these command line
activities are duplicated through a GUI
(such as the one in Access), however
there are advantages to performing
these operations through text. As an
example consider a temporary table
created, filled with records and then
dropped with no user intervention.
Creating a Table

The SQL create table syntax is of the form


CREATE TABLE tablename
(column_name type [NULL/NOT NULL],
column_name type [NULL/NOT NULL],
column_name type [NULL/NOT NULL]..)
According to the relational model rules,
each tablename must be unique for the
database and each column_name must
be unique for each relation/table.

Column_name may be up to 30
characters in length starting with an initial
alphabetic character. The name may
consist of alphanumeric characters and
the special characters _, $, #, or @.
The SQL standard suggests the following types:

Text data, maximum of ‘size’


CHAR (size)
characters up to 240
Dates (which include time)
DATE
Character data up to 65535 (some
LONG
restrictions may apply on the use of this
field in a select statement)
Maximum of 40 digits (will accept
NUMBER
scientific notation)

There are other types which can be found in


the SQL standard or the user guide for the
particular database you are using.
NULL and NOT NULL indicate whether the field
will allow NULL values (which is the default) or
whether all cells must have a value.

Note that the way that desktop databases have


implemented these types varies from version to
version- Access 97/2000 offers Text, Memo,
Number, Date/Time, Currency, Autonumber,
Yes/No, OLE Object and Hyperlink.
Example

CREATE table branch


(
Branchno number not null,
Street char (15),
City char (15)
);

Creates a table called branch with 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)
above.
Example
CREATE table staff
(
Staffno number,
fname char(15),
lname char(15),
job char(15),
sal number,
branchno number null
);

Create 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

To permanently delete a table (to ‘drop’ a table), use


the drop command

DROP table tablename;

Note that most databases regard this as an


irreversible process- no undo features are typically
supplied.
Example
Deleting the branch table:

DROP table branch;


Modifying a table structure
To change a table structure use the alter table
command

ALTER table tablename


(
[MODIFY columnname type |
ADD columnname type ]
);
Example
To add a spouses_name field to the staff table:

ALTER table staff add spouses_name char(15);


To increase the size of the lname column:

ALTER table staff alter column lname char(20);

Note that 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)

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.
Inserting records into a table
The INSERT statement adds records (rows) to a table
and has two form:

INSERT into table [(columnname, columnname,


…)]
values (value, value,…)
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.

INSERT into table [(columnname, columnname, …)]


select select-list
from table(s) – etc.
This form allows an insert to be based on the results of a select
query.
Example
Inserting a new record into the dept. table.

INSERT into branch (branchno, street, city)


VALUES (50, “22 Deer Road”, “London”);

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.

INSERT into table [(columnname, columnname, …)]


select select-list
from table(s) – etc.
This form allows an insert to be based on the results of a select
query.
EXERCISES- DDL and DML
activities
1. Create a specialized property table called
propertyBarbados, which has the same field names
as the property table.

SQL:
2. Write an appropriate SQL query to insert a new
property into propertyBarbados with the following
details

propertyno – BD67
Street – Sunrise St
Country – Barbados
Type – Villa
Rooms – 7
Rent – 600
Year income – 14000
Owner – CO96

SQL:
3. Write a query that will insert the details of other
properties in Barbados into a table

SQL:
4. Write an SQL statement to drop the
propertyBarbados table

SQL:

You might also like