You are on page 1of 26

Database Systems

SQL

Data Definition Language - DDL


WEEK -8
LECTURE 15
What is SQL?
• Structured Query Language
• Open ANSI standard
– Supported by most databases
– Some variation in implementation
• A skill that is used by many people in many
environments
– Programmers
– Database Administrators
– Managers
What Does SQL Do?
• Views information from relational database
– Single or Multiple Tables
– Tools to Calculate, Summarize, and Consolidate
• Manipulates infor4mation in relational database
– Insert Records
– Update Records
– Delete Records
– Operates on entire record set with single command
• Define relational database
– Create Database, Tables, Primary and Foreign Keys
SQL Environment
SQL is categorized into:
• Data Definition Language (DDL):
– Commands that define a database, including creating, altering, and
dropping tables and establishing constraints
• Data Manipulation Language (DML)
– Commands that maintain and query a database
• Data Control Language (DCL)
– Commands that control a database, including administering
privileges and committing data
• Transaction Control Language (DCL)
– This comprises of commands that are used to manage the changes
made by the ‘ Data Manipulation Language’ commands.
Figure 7-4:
DDL, DML, DCL, and the database development process
Introduction To Tables
• Tables forms the foundation of the database
structure in RDBMS.
• Primary function to hold the data.
• Made up of intersection of rows and
columns.
• Data in a table is easy to sort, filter,
calculate and manipulate as required.
Designing Tables
Before Creating tables we need to

• Pick Fields
• Specify a Correct data type and size
• Apply constraints on the columns
Planning the data type
• This characteristics describes the type of data that the
column will contain, whether it is a character, number
or a data.
• Different software packages will have different data
type names for representing data.

Text Number
Data Types

Data types in MS-Access Data types in SQL


• AutoNumber Server 2000
• Text • Char
• Memo • Varcher
• Number • Decimal
• Date/Time • Numeric
• Currency etc • Money
Specifying Field Size
• This is the process of specifying the
maximum amount of data that can be stored
in a field
• If the length of the description is not
defined, most of the software packages
assign a default value.
Assigning Names to Tables
• The table name should be unique, descriptive and
meaningful to all the users in an organization.
• The name should clearly and accurately identify
the contents of the table.
• Do not use the word such as ‘File’ ‘Record’ and
‘Table’ which could get confused with system
names and cause error later.
• Avoid using abbreviations and acronyms
Features of Tables
• It represents a single subject, which can be
object or an event
• Each row in a table is unique
• It does not contain calculated fields.
• It does not contain duplicate fields.
• It contains only minimum amount of
redundant data.
Data types
Data type Description Example

Alphanumeri Alphabets “1”,”a”,”a10


c and numbers 0”

Number Numbers 1,2,4,500


only
Date Dates only 5-Jan-2000
Specifying field size
• This is the process of specifying the
maximum amount of data that can be
stored in a field
Data Type Description

Text Stores alphanumeric data.

Memo Stores data that exceeds 255 characters

Number Stores numeric data that is to be used in calculations later

Date/Time Stores a date or time

Currency Stores currency values

AutoNumb Automatically generates serial or random numbers (the numbers


er never repeat)
Yes/No Stores a logical or ‘Boolean’ value of Yes/No or True/False

Hyperlink Used to Store URLs, email addresses or links to other files on


the system
OLE object Stores pictures, photographs of employees, Word, Excel or other
such files
Creating Tables
sSyntax

CREATE TABLE <Table_Name>


(<Column_Name> <Data_Type>,….)

Example
CREATE TABLE Student
( Std_Id int (3) ,
Std_name varchar(15) ) ;
Modifying Table Structure
Syntax
ALTER TABLE <table_name>
{ALTER COLUMN <column_name> <new_data_type>}
| {ADD [<column_name> <data_type>]}
| {DROP COLUMN <column_name>}

Example
ALTER TABLE Airlines_Master
ADD COLUMN (NoOfAircraft INT)
Modifying Table Contents
Syntax: Adding rows
INSERT [INTO] <table_name> VALUES <values>

Syntax: Updating rows


UPDATE <table_name>
SET <column_name = value>
WHERE <condition>

Syntax: Deleting rows


DELETE FROM <table_name> WHERE <condition>
Viewing Tables

Syntax: Viewing table information


sp_help <table_name>

Syntax: Viewing table data


SELECT <select_list> FROM <table_name>
Removing tables

Syntax
DROP TABLE <Table_Name>

Example
DROP TABLE Airlines_Master
Modifying Table Structure
Syntax
ALTER TABLE <table_name>
{ALTER COLUMN <column_name> <new_data_type>}
| {ADD [<column_name> <data_type>]}
| {DROP <column_name>}

Example
ALTER TABLE Student
ADD Address TEXT(20)
Constraints
 A property that can be placed on a column
or set of columns in a table
 Enforces limitations on data entered in a
particular column
 Can be defined at the time of table
creation, or added later
Enforcing Constraints
• Constraints are
limitations placed on
incoming data

Can be for a range


Can be for the reference
value

Can be for a value Can be for absence


of a value
Types of Constraints

Constraints

Check Not Null Primary Key Default

Foreign Key
PRIMARY KEY Constraint
 A column or set of columns that identifies
each table row uniquely
Syntax
CREATE TABLE Table_name
(<Column_definition> CONSTRATINT <Name of Constraint> PRIMARY KEY)

Example
CREATE TABLE Reservation_copy
( PNR_no number CONSTRAINT pk_res PRIMARY KEY )
Decision for the Primary Key
• There’s no absolute rule as to which
candidate key is best
• Fabian Pascal, in his book SQL and
Relational Basics, notes that decision
should be based upon the Principles of
– Minimality
– Stability
– Simplicity

You might also like