SlideShare a Scribd company logo
1 of 29
Sanjay Goel, School of Business, University at Albany, SUNY 1
SQL- Data Definition Language
ITM 692
Sanjay Goel
Sanjay Goel, School of Business, University at Albany, SUNY 2
DDL
Introduction
• To understand the SQL Data Definition Language
– Create
– Insert
– Delete
– Drop
– Truncate
– Alter
Sanjay Goel, School of Business, University at Albany, SUNY 3
Section I
Data Definition Language
Introduction
Sanjay Goel, School of Business, University at Albany, SUNY 4
DDL
Creating a Database
• To initialize a new database:
• Syntax:
CREATE DATABASE database_name
• There are numerous arguments that go along with this command
but are database specific
• Only some databases require database to be created and space to
be allocated prior to creation of tables.
• Some databases provide graphical user interfaces to create
databases and allocate space.
– Access only allows database to be created using User Interface
Sanjay Goel, School of Business, University at Albany, SUNY 5
DDL
Creating a Table
• Syntax
CREATE TABLE table_name
(Column_name datatype[(size)],
Column_name datatype[(size)],
)
• Example
CREATE TABLE books
(ISBN char(20),
Title char(50),
AuthorID Integer,
Price float)
• Creates a table with four columns
Sanjay Goel, School of Business, University at Albany, SUNY 6
DDL
Data Types
• Following broad categories of data types exist in
most databases:
– String Data
– Numeric Data
– Temporal Data
– Large Objects
Sanjay Goel, School of Business, University at Albany, SUNY 7
DDL
String Data
• Fixed Length:
• Occupies the same length of space in memory no matter how
much data is stored in them.
• Syntax:
char(n) where n is the length of the String
e.g. name char(50)
• If the variable stored for name is ‘Sanjay’ the extra 43 fields
are padded with blanks
Sanjay Goel, School of Business, University at Albany, SUNY 8
DDL
String Data
• Variable Length string is specified with maximum length of
characters possible in the string, however, the allocation is
sized to the size of the data stored in memory.
• Syntax:
Varchar(n) – n is the maximum length of data possible for the type
• There may be a restriction in the maximum length of the data
that you can specify in the declaration which will vary
according to the database.
• All character data has to be enclosed in single quotes during
specification.
Sanjay Goel, School of Business, University at Albany, SUNY 9
DDL
Numeric Data Types
• Store all the data related to purely numeric data.
• Some numeric data may also be stored as a character field e.g.
zip codes
• Common Numeric Types:
– Decimal Floating point number
– Float Floating point number
– Integer(size) Integer of specified length
– Money A number which contains exactly two
digits after the decimal point
– Number A standard number field that can hold a
floating point data
Note: Different databases name their numeric fields differently and may not
support all numeric types. They may also support additional numeric
types.
Sanjay Goel, School of Business, University at Albany, SUNY 10
DDL
Temporal Data Types
• These represent the dates and time:
• Three basic types are supported:
– Dates
– Times
– Date-Time Combinations
Sanjay Goel, School of Business, University at Albany, SUNY 11
DDL
Large Data Objects
• These are used for storing data objects like files and
images:
• There are two types:
– Character Large Objects (clobs)
– Binary Large Objects (blobs)
Sanjay Goel, School of Business, University at Albany, SUNY 12
DDL
Specifying Keys- Introduction
• Unique keyword is used to specify keys.
– This ensures that duplicate rows are not created in the database.
• Both Primary keys and Candidate Keys can be specified in the database.
• Once a set of columns has been declared unique any data entered that
duplicates the data in these columns is rejected.
• Specifying a single column as unique:
• Example
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
UNIQUE (name))
• Here the name column has been declared as a candidate key
Sanjay Goel, School of Business, University at Albany, SUNY 13
DDL
Specifying Keys- Multiple Columns
• Specifying multiple columns as unique:
• Example:
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
UNIQUE (name),
UNIQUE(city, state))
• Here both name & city/state combination are declared as
candidate keys
Sanjay Goel, School of Business, University at Albany, SUNY 14
DDL
Specifying Keys- Primary Key
• Specifying multiple columns as unique:
• To specify the Primary Key the Primary Key clause is used
• Example:
CREATE TABLE Studios
(studio_id Number,
name char(20),
city varchar(50),
state char(2),
PRIMARY KEY (studio_id),
UNIQUE (name),
UNIQUE(city, state)
)
Sanjay Goel, School of Business, University at Albany, SUNY 15
DDL
Specifying Keys- Single and MultiColumn Keys
• Single column keys can be defined at the column level instead
of at the table level at the end of the field descriptions.
• MultiColumn keys still need to be defined separately at the
table level
CREATE TABLE Studios
(studio_id Number PRIMARY KEY,
name char(20) UNIQUE,
city varchar(50),
state char(2),
Unique(city, state))
• Note: Some databases require the use of Unique Index for
specification of keys.
Sanjay Goel, School of Business, University at Albany, SUNY 16
DDL
Specifying Keys- Foreign Keys
• References clause is used to create a relationship between a
set of columns in one table and a candidate key in the table
that is being referenced.
• Example:
CREATE TABLE Movies
(movie_title varchar(40),
studio_id Number REFERENCES Studios(studio_id))
• Creates a relationship from the Movies table to the Studios
table
Sanjay Goel, School of Business, University at Albany, SUNY 17
DDL
Constraints- Disallowing Null Values
Disallowing Null Values:
– Null values entered into a column means that the data in not known.
– These can cause problems in Querying the database.
– Specifying Primary Key automatically prevents null being entered in
columns which specify the primary key
• Not Null clause is used in preventing null values from being
entered in a column.
• Example:
CREATE TABLE Studios
( studio_id number PRIMARY KEY,
name char(20) NOT NULL,
city varchar(50) NOT NULL,
state char(2) NOT NULL)
• Null clause can be used to explicitly allow null values in a
column also
Sanjay Goel, School of Business, University at Albany, SUNY 18
DDL
Constraints- Value Constraints
Value Constraints:
– Allows value inserted in the column to be checked condition in the column constraint.
• Check clause is used to create a constraint in SQL
• Example:
CREATE TABLE Movies
(movie_title varchar(40) PRIMARY KEY,
studio_id Number,
budget Number check (budget > 50000)
)
• Table level constraints can also be defined using the Constraint keyword
• Example:
CREATE TABLE Movies
(movie_title varchar(40) PRIMARY KEY,
studio_id Number,
budget Number check (budget > 50000),
release_date Date,
CONSTRAINT release_date_constraint Check (release_date between ’01-Jan-1980’ and ’31-dec-
1989))
• Such constraints can be activated and deactivated as required.
Sanjay Goel, School of Business, University at Albany, SUNY 19
DDL
Constraints- Default Value
Default Value:
– A default value can be inserted in any column by using the Default
keyword.
• Example:
CREATE TABLE Movies (
movie_title varchar(40) NOT NULL,
release_date date DEFAULT sysdate NULL,
genre varchar(20) DEFAULT ‘Comedy’ Check genere
In (‘Comedy’, ‘Horror’, ‘Drama’)
)
• Table level constraints can also be defined using the
Constraint keyword
• release_date defaults to the current date, however Null value is enabled in
the column which will need to be added explicitly when data is added.
• Note: Any valid expression can be used while specifying constraints
Sanjay Goel, School of Business, University at Albany, SUNY 20
Section II
Modifying Records
Sanjay Goel, School of Business, University at Albany, SUNY 21
Modifying Records
Insert Statement
• Insert:
– Allows you to add new records to the Table
• Syntax:
– Insert into table_name[(column_list)] values (value_list)
• Example:
INSERT INTO studios
VALUES (1, ‘Giant’, ‘Los Angeles’, ‘CA’)
INSERT INTO studios
(studio_city, studio_state, studio_name, studio_id)
VALUES (‘Burbank’, ‘CA’, ‘MPM’, 2)
• Notes1: If the columns are not specified as in the first example the data
goes in the order specified in the table
• Notes2: There are two ways of inserting Null values
1. If the field has a default value of Null, you can use an Insert statement
that ignores the column where the value is to be Null.
2. You can specify the column in the column list specification and assign
a value of Null to the corresponding value field.
Sanjay Goel, School of Business, University at Albany, SUNY 22
Modifying Records
Select & Insert
• Select & Insert:
– A select query can be used in the insert statement to get the values for the
insert statement
• Example:
INSERT INTO city_state
SELECT studio_city, studio_state FROM studios
• This selects the corresponding fields from the studios table and inserts
them into the city_state table.
• Example:
INSERT INTO city_state
SELECT Distinct studio_city, studio_state FROM studios
• This selects the corresponding fields from the studios table, deletes the
duplicate fields and inserts them into the city_state table. Thus the final
table has distinct rows
Sanjay Goel, School of Business, University at Albany, SUNY 23
Modifying Records
Delete Statement
• Delete Statement:
– is used to remove records from a table of the database. The where
clause in the syntax is used to restrict the rows deleted from the
table otherwise all the rows from the table are deleted.
• Syntax: DELETE FROM table_name [WHERE Condition]
• Example:
DELETE FROM City_State
WHERE state = ‘TX’
• Deletes all the rows where the state is Texas keeps all the
other rows.
Sanjay Goel, School of Business, University at Albany, SUNY 24
Modifying Records
Update Statement
• Update Statement:
– used to make changes to existing rows of the table. It has three parts. First,
you ,must specify which table is going to be updated. The second part of the
statement is the set clause, in which you should specify the columns that will
be updated as well as the values that will be inserted. Finally, the where
clause is used to specify which rows will be updated.
• Syntax:
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, …..
[WHERE Condition]
• Example:
UPDATE studios
SET studio_city = ‘New York’, studio_state = ‘NY’
WHERE studio_id = 1
• Notes1: If the condition is dropped then all the rows are updated.
Sanjay Goel, School of Business, University at Albany, SUNY 25
Modifying Records
Truncate Statement
• Truncate Statement:
– used to delete all the rows of a table. Delete can also be used to delete all the
rows from the table. The difference is that delete performs a delete
operation on each row in the table and the database performs all attendant
tasks on the way. On the other had the Truncate statement simply throws
away all the rows at once and is much quicker. The note of caution is that
truncate does not do integrity checks on the way which can lead to
inconsistencies on the way. If there are dependencies requiring integrity
checks we should use delete.
• Syntax: TRUNCATE TABLE table_name
• Example:
TRUNCATE TABLE studios
• This deletes all the rows of the table studios
Sanjay Goel, School of Business, University at Albany, SUNY 26
Modifying Records
Drop Statement
• Drop Statement:
– used to remove elements from a database, such as tables, indexes or
even users and databases. Drop command is used with a variety of
keywords based on the need.
• Drop Table Syntax: DROP TABLE table_name
• Drop Table Example: DROP TABLE studios
• Drop Index Syntax: DROP INDEX table_name
• Drop Index Example: DROP INDEX movie_index
Sanjay Goel, School of Business, University at Albany, SUNY 27
Modifying Records
Alter Statement
• Alter Statement:
– used to make changes to the schema of the table. Columns can be
added and the data type of the columns changed as long as the data
in those columns conforms to the data type specified.
• Syntax:
ALTER TABLE table_name
ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
• Example:
ALTER TABLE studios
ADD (revenue Number DEFAULT 0)
Sanjay Goel, School of Business, University at Albany, SUNY 28
Modifying Records
Alter Statement
Add table level constraints:
• Syntax:
ALTER TABLE table_name
ADD ([CONSTRAINT constraint_name CHECK comparison]
[columns REFERENCES table_name (columns)]
• Example:
ALTER TABLE studios
ADD (CONSTRAINT check_state CHECK (studio_state in (‘TX’, ‘CA’, ‘WA’))
Modify Columns:
• Syntax:
ALTER TABLE table_name
MODIFY column [data type]
[Default Expression]
[REFERENCES table_name (column_name)’
[CHECK condition]
• Example:
ALTER TABLE People
MODIFY person_union varchar(10)
• Notes1: Columns can not be removed from the table using alter. If you want to
remove columns you have to drop the table and then recreate it without the
column that you want to discard
Sanjay Goel, School of Business, University at Albany, SUNY 29
Modifying Records
Alter Statement
• Alter Statement:
– used to make changes to the schema of the table. Columns can be
added and the data type of the columns changed as long as the data
in those columns conforms to the data type specified.
• Syntax:
ALTER TABLE table_name
ADD (column datatype [Default Expression])
[REFERENCES table_name (column_name)’
[CHECK condition]
• Example:
ALTER TABLE studios
ADD (revenue Number DEFAULT 0)

More Related Content

Similar to ddl (1).ppt

Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql MengChun Lam
 
Lesson-02 (1).pptx
Lesson-02 (1).pptxLesson-02 (1).pptx
Lesson-02 (1).pptxssuserc24e05
 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systemsSHAKIR325211
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle Abrar ali
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tablesSyed Zaid Irshad
 
•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdf•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdfjyothimuppasani1
 
lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxcAnhTrn53
 
Unit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relateUnit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relateumang2782love
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Prosanta Ghosh
 
chapter2.PPT
chapter2.PPTchapter2.PPT
chapter2.PPTDEFECTOR
 

Similar to ddl (1).ppt (20)

Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
 
Lesson-02 (1).pptx
Lesson-02 (1).pptxLesson-02 (1).pptx
Lesson-02 (1).pptx
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Database
Database Database
Database
 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systems
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Create table
Create tableCreate table
Create table
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle
 
Relational_Model.ppt
Relational_Model.pptRelational_Model.ppt
Relational_Model.ppt
 
plsql Les09
 plsql Les09 plsql Les09
plsql Les09
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdf•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdf
 
lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptx
 
Unit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relateUnit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relate
 
Sql basics
Sql  basicsSql  basics
Sql basics
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
Access 04
Access 04Access 04
Access 04
 
chapter2.PPT
chapter2.PPTchapter2.PPT
chapter2.PPT
 

Recently uploaded

在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信
在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信
在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信oopacde
 
办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样
办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样
办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样vwymvu
 
NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...
NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...
NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...Amil Baba Dawood bangali
 
如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证mestb
 
在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一
在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一
在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一uodye
 
Matrix Methods.pptxhhhhhhhhhhhhhhhhhhhhh
Matrix Methods.pptxhhhhhhhhhhhhhhhhhhhhhMatrix Methods.pptxhhhhhhhhhhhhhhhhhhhhh
Matrix Methods.pptxhhhhhhhhhhhhhhhhhhhhhjoshuaclack73
 
Mahindra XUV new version for smooth travelling
Mahindra XUV new version for smooth travellingMahindra XUV new version for smooth travelling
Mahindra XUV new version for smooth travellingSailaja Gudipati
 
Jual Obat Aborsi Samarinda ( No.1 ) 088980685493 Obat Penggugur Kandungan Cy...
Jual Obat Aborsi Samarinda (  No.1 ) 088980685493 Obat Penggugur Kandungan Cy...Jual Obat Aborsi Samarinda (  No.1 ) 088980685493 Obat Penggugur Kandungan Cy...
Jual Obat Aborsi Samarinda ( No.1 ) 088980685493 Obat Penggugur Kandungan Cy...Obat Aborsi 088980685493 Jual Obat Aborsi
 
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制uodye
 
Best CPU for gaming Intel Core i9-14900K 14th Gen Desktop CPU
Best CPU for gaming  Intel Core i9-14900K 14th Gen Desktop CPUBest CPU for gaming  Intel Core i9-14900K 14th Gen Desktop CPU
Best CPU for gaming Intel Core i9-14900K 14th Gen Desktop CPUZiaurRehman887108
 
如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样
如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样
如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样wsppdmt
 
Vibration of Continuous Systems.pjjjjjjjjptx
Vibration of Continuous Systems.pjjjjjjjjptxVibration of Continuous Systems.pjjjjjjjjptx
Vibration of Continuous Systems.pjjjjjjjjptxjoshuaclack73
 
如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证mestb
 
一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书
一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书
一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书ougvy
 
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理uodye
 
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in DammamAbortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammamahmedjiabur940
 

Recently uploaded (20)

在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信
在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信
在线办理(scu毕业证)南十字星大学毕业证电子版学位证书注册证明信
 
办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样
办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样
办理(uw学位证书)美国华盛顿大学毕业证续费收据一模一样
 
NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...
NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...
NO1 Pakistan Best vashikaran specialist in UK USA UAE London Dubai Canada Ame...
 
Abortion pills in Riyadh Saudi Arabia!+966572737505 ) Where to get cytotec
Abortion pills in Riyadh Saudi Arabia!+966572737505 ) Where to get cytotecAbortion pills in Riyadh Saudi Arabia!+966572737505 ) Where to get cytotec
Abortion pills in Riyadh Saudi Arabia!+966572737505 ) Where to get cytotec
 
如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(OP毕业证书)奥塔哥理工学院毕业证成绩单本科硕士学位证留信学历认证
 
在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一
在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一
在线制作(UQ毕业证书)昆士兰大学毕业证成绩单原版一比一
 
Matrix Methods.pptxhhhhhhhhhhhhhhhhhhhhh
Matrix Methods.pptxhhhhhhhhhhhhhhhhhhhhhMatrix Methods.pptxhhhhhhhhhhhhhhhhhhhhh
Matrix Methods.pptxhhhhhhhhhhhhhhhhhhhhh
 
Mahindra XUV new version for smooth travelling
Mahindra XUV new version for smooth travellingMahindra XUV new version for smooth travelling
Mahindra XUV new version for smooth travelling
 
Contact +971581248768 to buy 100% original and safe abortion pills in Dubai a...
Contact +971581248768 to buy 100% original and safe abortion pills in Dubai a...Contact +971581248768 to buy 100% original and safe abortion pills in Dubai a...
Contact +971581248768 to buy 100% original and safe abortion pills in Dubai a...
 
Abortion Pills in Jeddah |+966572737505 | Get Cytotec
Abortion Pills in Jeddah |+966572737505 | Get CytotecAbortion Pills in Jeddah |+966572737505 | Get Cytotec
Abortion Pills in Jeddah |+966572737505 | Get Cytotec
 
Jual Obat Aborsi Samarinda ( No.1 ) 088980685493 Obat Penggugur Kandungan Cy...
Jual Obat Aborsi Samarinda (  No.1 ) 088980685493 Obat Penggugur Kandungan Cy...Jual Obat Aborsi Samarinda (  No.1 ) 088980685493 Obat Penggugur Kandungan Cy...
Jual Obat Aborsi Samarinda ( No.1 ) 088980685493 Obat Penggugur Kandungan Cy...
 
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
 
Best CPU for gaming Intel Core i9-14900K 14th Gen Desktop CPU
Best CPU for gaming  Intel Core i9-14900K 14th Gen Desktop CPUBest CPU for gaming  Intel Core i9-14900K 14th Gen Desktop CPU
Best CPU for gaming Intel Core i9-14900K 14th Gen Desktop CPU
 
如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样
如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样
如何办理(USYD毕业证书)悉尼大学毕业证成绩单原件一模一样
 
Vibration of Continuous Systems.pjjjjjjjjptx
Vibration of Continuous Systems.pjjjjjjjjptxVibration of Continuous Systems.pjjjjjjjjptx
Vibration of Continuous Systems.pjjjjjjjjptx
 
如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(AUT毕业证书)奥克兰理工大学毕业证成绩单本科硕士学位证留信学历认证
 
Buy Abortion pills in Riyadh |+966572737505 | Get Cytotec
Buy Abortion pills in Riyadh |+966572737505 | Get CytotecBuy Abortion pills in Riyadh |+966572737505 | Get Cytotec
Buy Abortion pills in Riyadh |+966572737505 | Get Cytotec
 
一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书
一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书
一比一原版(LaTrobe毕业证书)澳洲拉筹伯大学毕业证学位证书
 
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
 
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in DammamAbortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
Abortion Pill for sale in Riyadh ((+918761049707) Get Cytotec in Dammam
 

ddl (1).ppt

  • 1. Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel
  • 2. Sanjay Goel, School of Business, University at Albany, SUNY 2 DDL Introduction • To understand the SQL Data Definition Language – Create – Insert – Delete – Drop – Truncate – Alter
  • 3. Sanjay Goel, School of Business, University at Albany, SUNY 3 Section I Data Definition Language Introduction
  • 4. Sanjay Goel, School of Business, University at Albany, SUNY 4 DDL Creating a Database • To initialize a new database: • Syntax: CREATE DATABASE database_name • There are numerous arguments that go along with this command but are database specific • Only some databases require database to be created and space to be allocated prior to creation of tables. • Some databases provide graphical user interfaces to create databases and allocate space. – Access only allows database to be created using User Interface
  • 5. Sanjay Goel, School of Business, University at Albany, SUNY 5 DDL Creating a Table • Syntax CREATE TABLE table_name (Column_name datatype[(size)], Column_name datatype[(size)], ) • Example CREATE TABLE books (ISBN char(20), Title char(50), AuthorID Integer, Price float) • Creates a table with four columns
  • 6. Sanjay Goel, School of Business, University at Albany, SUNY 6 DDL Data Types • Following broad categories of data types exist in most databases: – String Data – Numeric Data – Temporal Data – Large Objects
  • 7. Sanjay Goel, School of Business, University at Albany, SUNY 7 DDL String Data • Fixed Length: • Occupies the same length of space in memory no matter how much data is stored in them. • Syntax: char(n) where n is the length of the String e.g. name char(50) • If the variable stored for name is ‘Sanjay’ the extra 43 fields are padded with blanks
  • 8. Sanjay Goel, School of Business, University at Albany, SUNY 8 DDL String Data • Variable Length string is specified with maximum length of characters possible in the string, however, the allocation is sized to the size of the data stored in memory. • Syntax: Varchar(n) – n is the maximum length of data possible for the type • There may be a restriction in the maximum length of the data that you can specify in the declaration which will vary according to the database. • All character data has to be enclosed in single quotes during specification.
  • 9. Sanjay Goel, School of Business, University at Albany, SUNY 9 DDL Numeric Data Types • Store all the data related to purely numeric data. • Some numeric data may also be stored as a character field e.g. zip codes • Common Numeric Types: – Decimal Floating point number – Float Floating point number – Integer(size) Integer of specified length – Money A number which contains exactly two digits after the decimal point – Number A standard number field that can hold a floating point data Note: Different databases name their numeric fields differently and may not support all numeric types. They may also support additional numeric types.
  • 10. Sanjay Goel, School of Business, University at Albany, SUNY 10 DDL Temporal Data Types • These represent the dates and time: • Three basic types are supported: – Dates – Times – Date-Time Combinations
  • 11. Sanjay Goel, School of Business, University at Albany, SUNY 11 DDL Large Data Objects • These are used for storing data objects like files and images: • There are two types: – Character Large Objects (clobs) – Binary Large Objects (blobs)
  • 12. Sanjay Goel, School of Business, University at Albany, SUNY 12 DDL Specifying Keys- Introduction • Unique keyword is used to specify keys. – This ensures that duplicate rows are not created in the database. • Both Primary keys and Candidate Keys can be specified in the database. • Once a set of columns has been declared unique any data entered that duplicates the data in these columns is rejected. • Specifying a single column as unique: • Example CREATE TABLE Studios (studio_id Number, name char(20), city varchar(50), state char(2), UNIQUE (name)) • Here the name column has been declared as a candidate key
  • 13. Sanjay Goel, School of Business, University at Albany, SUNY 13 DDL Specifying Keys- Multiple Columns • Specifying multiple columns as unique: • Example: CREATE TABLE Studios (studio_id Number, name char(20), city varchar(50), state char(2), UNIQUE (name), UNIQUE(city, state)) • Here both name & city/state combination are declared as candidate keys
  • 14. Sanjay Goel, School of Business, University at Albany, SUNY 14 DDL Specifying Keys- Primary Key • Specifying multiple columns as unique: • To specify the Primary Key the Primary Key clause is used • Example: CREATE TABLE Studios (studio_id Number, name char(20), city varchar(50), state char(2), PRIMARY KEY (studio_id), UNIQUE (name), UNIQUE(city, state) )
  • 15. Sanjay Goel, School of Business, University at Albany, SUNY 15 DDL Specifying Keys- Single and MultiColumn Keys • Single column keys can be defined at the column level instead of at the table level at the end of the field descriptions. • MultiColumn keys still need to be defined separately at the table level CREATE TABLE Studios (studio_id Number PRIMARY KEY, name char(20) UNIQUE, city varchar(50), state char(2), Unique(city, state)) • Note: Some databases require the use of Unique Index for specification of keys.
  • 16. Sanjay Goel, School of Business, University at Albany, SUNY 16 DDL Specifying Keys- Foreign Keys • References clause is used to create a relationship between a set of columns in one table and a candidate key in the table that is being referenced. • Example: CREATE TABLE Movies (movie_title varchar(40), studio_id Number REFERENCES Studios(studio_id)) • Creates a relationship from the Movies table to the Studios table
  • 17. Sanjay Goel, School of Business, University at Albany, SUNY 17 DDL Constraints- Disallowing Null Values Disallowing Null Values: – Null values entered into a column means that the data in not known. – These can cause problems in Querying the database. – Specifying Primary Key automatically prevents null being entered in columns which specify the primary key • Not Null clause is used in preventing null values from being entered in a column. • Example: CREATE TABLE Studios ( studio_id number PRIMARY KEY, name char(20) NOT NULL, city varchar(50) NOT NULL, state char(2) NOT NULL) • Null clause can be used to explicitly allow null values in a column also
  • 18. Sanjay Goel, School of Business, University at Albany, SUNY 18 DDL Constraints- Value Constraints Value Constraints: – Allows value inserted in the column to be checked condition in the column constraint. • Check clause is used to create a constraint in SQL • Example: CREATE TABLE Movies (movie_title varchar(40) PRIMARY KEY, studio_id Number, budget Number check (budget > 50000) ) • Table level constraints can also be defined using the Constraint keyword • Example: CREATE TABLE Movies (movie_title varchar(40) PRIMARY KEY, studio_id Number, budget Number check (budget > 50000), release_date Date, CONSTRAINT release_date_constraint Check (release_date between ’01-Jan-1980’ and ’31-dec- 1989)) • Such constraints can be activated and deactivated as required.
  • 19. Sanjay Goel, School of Business, University at Albany, SUNY 19 DDL Constraints- Default Value Default Value: – A default value can be inserted in any column by using the Default keyword. • Example: CREATE TABLE Movies ( movie_title varchar(40) NOT NULL, release_date date DEFAULT sysdate NULL, genre varchar(20) DEFAULT ‘Comedy’ Check genere In (‘Comedy’, ‘Horror’, ‘Drama’) ) • Table level constraints can also be defined using the Constraint keyword • release_date defaults to the current date, however Null value is enabled in the column which will need to be added explicitly when data is added. • Note: Any valid expression can be used while specifying constraints
  • 20. Sanjay Goel, School of Business, University at Albany, SUNY 20 Section II Modifying Records
  • 21. Sanjay Goel, School of Business, University at Albany, SUNY 21 Modifying Records Insert Statement • Insert: – Allows you to add new records to the Table • Syntax: – Insert into table_name[(column_list)] values (value_list) • Example: INSERT INTO studios VALUES (1, ‘Giant’, ‘Los Angeles’, ‘CA’) INSERT INTO studios (studio_city, studio_state, studio_name, studio_id) VALUES (‘Burbank’, ‘CA’, ‘MPM’, 2) • Notes1: If the columns are not specified as in the first example the data goes in the order specified in the table • Notes2: There are two ways of inserting Null values 1. If the field has a default value of Null, you can use an Insert statement that ignores the column where the value is to be Null. 2. You can specify the column in the column list specification and assign a value of Null to the corresponding value field.
  • 22. Sanjay Goel, School of Business, University at Albany, SUNY 22 Modifying Records Select & Insert • Select & Insert: – A select query can be used in the insert statement to get the values for the insert statement • Example: INSERT INTO city_state SELECT studio_city, studio_state FROM studios • This selects the corresponding fields from the studios table and inserts them into the city_state table. • Example: INSERT INTO city_state SELECT Distinct studio_city, studio_state FROM studios • This selects the corresponding fields from the studios table, deletes the duplicate fields and inserts them into the city_state table. Thus the final table has distinct rows
  • 23. Sanjay Goel, School of Business, University at Albany, SUNY 23 Modifying Records Delete Statement • Delete Statement: – is used to remove records from a table of the database. The where clause in the syntax is used to restrict the rows deleted from the table otherwise all the rows from the table are deleted. • Syntax: DELETE FROM table_name [WHERE Condition] • Example: DELETE FROM City_State WHERE state = ‘TX’ • Deletes all the rows where the state is Texas keeps all the other rows.
  • 24. Sanjay Goel, School of Business, University at Albany, SUNY 24 Modifying Records Update Statement • Update Statement: – used to make changes to existing rows of the table. It has three parts. First, you ,must specify which table is going to be updated. The second part of the statement is the set clause, in which you should specify the columns that will be updated as well as the values that will be inserted. Finally, the where clause is used to specify which rows will be updated. • Syntax: UPDATE table_name SET column_name1 = value1, column_name2 = value2, ….. [WHERE Condition] • Example: UPDATE studios SET studio_city = ‘New York’, studio_state = ‘NY’ WHERE studio_id = 1 • Notes1: If the condition is dropped then all the rows are updated.
  • 25. Sanjay Goel, School of Business, University at Albany, SUNY 25 Modifying Records Truncate Statement • Truncate Statement: – used to delete all the rows of a table. Delete can also be used to delete all the rows from the table. The difference is that delete performs a delete operation on each row in the table and the database performs all attendant tasks on the way. On the other had the Truncate statement simply throws away all the rows at once and is much quicker. The note of caution is that truncate does not do integrity checks on the way which can lead to inconsistencies on the way. If there are dependencies requiring integrity checks we should use delete. • Syntax: TRUNCATE TABLE table_name • Example: TRUNCATE TABLE studios • This deletes all the rows of the table studios
  • 26. Sanjay Goel, School of Business, University at Albany, SUNY 26 Modifying Records Drop Statement • Drop Statement: – used to remove elements from a database, such as tables, indexes or even users and databases. Drop command is used with a variety of keywords based on the need. • Drop Table Syntax: DROP TABLE table_name • Drop Table Example: DROP TABLE studios • Drop Index Syntax: DROP INDEX table_name • Drop Index Example: DROP INDEX movie_index
  • 27. Sanjay Goel, School of Business, University at Albany, SUNY 27 Modifying Records Alter Statement • Alter Statement: – used to make changes to the schema of the table. Columns can be added and the data type of the columns changed as long as the data in those columns conforms to the data type specified. • Syntax: ALTER TABLE table_name ADD (column datatype [Default Expression]) [REFERENCES table_name (column_name)’ [CHECK condition] • Example: ALTER TABLE studios ADD (revenue Number DEFAULT 0)
  • 28. Sanjay Goel, School of Business, University at Albany, SUNY 28 Modifying Records Alter Statement Add table level constraints: • Syntax: ALTER TABLE table_name ADD ([CONSTRAINT constraint_name CHECK comparison] [columns REFERENCES table_name (columns)] • Example: ALTER TABLE studios ADD (CONSTRAINT check_state CHECK (studio_state in (‘TX’, ‘CA’, ‘WA’)) Modify Columns: • Syntax: ALTER TABLE table_name MODIFY column [data type] [Default Expression] [REFERENCES table_name (column_name)’ [CHECK condition] • Example: ALTER TABLE People MODIFY person_union varchar(10) • Notes1: Columns can not be removed from the table using alter. If you want to remove columns you have to drop the table and then recreate it without the column that you want to discard
  • 29. Sanjay Goel, School of Business, University at Albany, SUNY 29 Modifying Records Alter Statement • Alter Statement: – used to make changes to the schema of the table. Columns can be added and the data type of the columns changed as long as the data in those columns conforms to the data type specified. • Syntax: ALTER TABLE table_name ADD (column datatype [Default Expression]) [REFERENCES table_name (column_name)’ [CHECK condition] • Example: ALTER TABLE studios ADD (revenue Number DEFAULT 0)