SlideShare a Scribd company logo
SQL	
  
SQL
• SQL = “Structured Query Language”
• Standard query language for relational DBMSs
• History:
Developed at IBM in late 70s
1st standard: SQL-86
2nd standard: SQL-92
3rd standard: SQL-99 or SQL3, well over 1000 pages
“The nice thing about standards is that
you have so many to choose from!”
-Andrew S. Tannenbaum
2	
  
SQL
Consists of two parts:
• Data Definition Language (DDL)
Allows the specification of the database schema
• Data Manipulation Language (DML)
Allows the specification of queries & insert/update/delete
statements
3	
  
SQL	
  
data	
  defini.on	
  
language	
  
SQL Data Definition
Language (DDL)
5	
  
• Allows the specification of the database schema
a set of relations with information about each relation
• Schema information:
- The schema of each relation
- The domain of values associated with each attribute
- Integrity constraints
• Other information one can specify:
- The set of indices to be maintained for each relation
- Security and authorization information for each relation
- The physical storage structure of each relation on disk
CREATE TABLE Command
• Used to define a relation
• Syntax:
CREATE TABLE relationName
(attrName1 Domain1,
…
attrNamen Domainn
(integrity-constraint1),
…,
(integrity-constraintn))
• Example:
CREATE TABLE branch
(branch_name char(15) not null,
branch_city char(30),
assets integer)
6	
  
Domain Types in SQL
• char(n)
Fixed length character string, with user-specified length n
• varchar(n)
Variable length character strings, with user-specified maximum
length n
• int
Integer (a finite subset of integers that is machine-dependent)
• smallint
Small integer (a machine-dependent subset of the integer
domain type)
7	
  
Domain Types in SQL
• numeric(p, d)
Fixed point number, with user-specified precision of p digits,
with d digits to the right of decimal point
• real, double precision
Floating point and double-precision floating point numbers, with
machine-dependent precision
• float
Floating point number, with user-specified precision of at least n
digits
and others…
8	
  
CREATE TABLE Command
• Can be used to also specify:
- Primary key attributes (PRIMARY KEY keyword)
- Secondary keys (UNIQUE keyword)
- Referential integrity constraints/foreign keys
(FOREIGN KEY keyword)
• Example:
CREATE TABLE DEPT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP )
9	
  
Primary key declaration on an attribute automatically ensures not null in
SQL-92 onwards, but it needs to be explicitly stated in SQL-89
DROP TABLE Command
• Used to remove a relation & its definition
The relation can no longer be used in queries, updates, or any other
commands since its description no longer exists
• Syntax:
DROP TABLE relationName
• Example:
DROP TABLE branch
10	
  
11
ALTER TABLE Command
• Used to add/drop attributes from a relation
• Add attribute syntax:
ALTER TABLE relationName ADD attribName attribDomain
All tuples in the relation are assigned null as the default value of the
new attribute
• Drop attribute syntax:
ALTER TABLE relationName DROP attribName
Dropping of attributes not supported by many DBMSs
ALTER TABLE Command
• Since new attribute will have NULL values right after the
ALTER command is executed, the NOT NULL constraint
is not allowed for such an attribute
• Example:
ALTER TABLE employee ADD job varchar(12)
• The database users must still enter a value for the new
attribute JOB for each EMPLOYEE tuple. This can be
done using the UPDATE command.
12	
  
Integrity Constraints
• Guard against accidental damage to the database
by ensuring that authorized changes to the database do not
result in a loss of data consistency.
• Examples:
- A savings account must have a balance greater than
$10,000.00
- A salary of a bank employee must be at least $6.00 an hour
- A customer must have a (non-null) phone number
13	
  
SQL Integrity Constraints
• On single relations:
- not null
- primary key
- unique
- check(P), where P is a predicate
• On multiple relations:
- foreign key
14	
  
NOT NULL Constraint
• Specifies that an attribute does not accept null values
• Can be specified as part of:
- The definition of an attribute in the CREATE TABLE statement
e.g. CREATE TABLE branch
(branch_name char(15) not null, …)
- The definition of a domain
(i.e., a “type” that can be used where a type is needed)
e.g. CREATE DOMAIN Dollars numeric(12, 2) not null
15	
  
UNIQUE Constraint
• Specifies that a set of attributes form a candidate key
• Syntax:
UNIQUE (AttrName1, …, AttrNamen)
• Candidate keys are permitted to be null
(in contrast to primary keys)
16	
  
CHECK Clause
17	
  
• Enforce a predicate (condition)
• Syntax:
CHECK (Predicate)
• Example:
Ensure that the values of the assets are non-negative
CREATE TABLE branch
(branch_name char(15),
branch_city char(30),
assets integer,
primary key (branch_name),
CHECK (assets >= 0) )
CHECK Clause
18	
  
• Can be also used to constrain domains
e.g., CREATE DOMAIN hourly_wage numeric (5,2)
CONSTRAINT value_test CHECK (value > = 4.00)
• Can be named
(useful to indicate which constraint an update violated)
e.g., CREATE DOMAIN hourly_wage numeric (5,2)
CONSTRAINT value_test CHECK (value > = 4.00)
Referential Integrity
• Ensures that a value that appears in one relation for a
given set of attributes also appears for a set of attributes in
another relation.
• Example:
If “La Jolla” is a branch name appearing in one of the tuples in
the account relation, then there exists a tuple in the branch
relation for branch “La Jolla”.
19	
  
Referential Integrity
• In the CREATE TABLE statement we can use:
- The PRIMARY KEY clause to list primary key (PK) attributes.
- The UNIQUE KEY clause to list candidate key attributes
- The FOREIGN KEY clause to list foreign key (FK) attributes
and the name of the relation referenced by the FK. By default, a
FK references PK attributes of the referenced table.
20	
  
Referential Integrity Example
21	
  
create table customer
(customer_name char(20),
customer_street char(30),
customer_city char(30),
primary key (customer_name ))
create table branch
(branch_name char(15),
branch_city char(30),
assets numeric(12,2),
primary key (branch_name ))
Referential Integrity Example
22	
  
create table account
(account_number char(10),
branch_name char(15),
balance integer,
primary key (account_number),
foreign key (branch_name) references branch )
create table depositor
(customer_name char(20),
account_number char(10),
primary key (customer_name, account_number),
foreign key (account_number ) references account,
foreign key (customer_name ) references customer )

More Related Content

PDF
Database management system important topic.pdf
PDF
6_SQL.pdf
PPT
SQL commands in database management system
PPTX
Presentation on SQL Basics to Advance in DBMS
PPT
Lec07-sql-ddldadadadajajdnajbdadwhyvydawvyduavd
PPT
database management system presentation on integrity constraints
PPTX
Quick Revision on DATA BASE MANAGEMENT SYSTEMS concepts.pptx
Database management system important topic.pdf
6_SQL.pdf
SQL commands in database management system
Presentation on SQL Basics to Advance in DBMS
Lec07-sql-ddldadadadajajdnajbdadwhyvydawvyduavd
database management system presentation on integrity constraints
Quick Revision on DATA BASE MANAGEMENT SYSTEMS concepts.pptx

Similar to DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL (20)

PPTX
05 Create and Maintain Databases and Tables.pptx
PDF
DBMS MODULE 3 NOTES ENGINEERING CSE .pdf
PDF
CS121Lec04.pdf
PPT
Database Technology Teaching Material For Learn
PPTX
Unit 2 Chap 4 SQL DDL.pptx
PPT
Lecture-9-10-11-12(a-b).ppt modern database
PPT
Dbms ii mca-ch7-sql-2013
PPT
Vnsispl dbms concepts_ch3
PPTX
Relational data model
PPT
Chapter06.ppt
PPT
Introduction to structured query language (sql)
PDF
4 the sql_standard
PPTX
MS SQL - Database Programming Concepts by RSolutions
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
PPTX
Sql commands
PPT
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
PPT
Module02
PPT
Introduction to Structured Query Language (SQL) (1).ppt
PDF
Intruduction to SQL.Structured Query Language(SQL}
PPTX
structured query language elmarsi and navathe edition 7th SQL Chapter06.pptx
05 Create and Maintain Databases and Tables.pptx
DBMS MODULE 3 NOTES ENGINEERING CSE .pdf
CS121Lec04.pdf
Database Technology Teaching Material For Learn
Unit 2 Chap 4 SQL DDL.pptx
Lecture-9-10-11-12(a-b).ppt modern database
Dbms ii mca-ch7-sql-2013
Vnsispl dbms concepts_ch3
Relational data model
Chapter06.ppt
Introduction to structured query language (sql)
4 the sql_standard
MS SQL - Database Programming Concepts by RSolutions
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
Sql commands
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
Module02
Introduction to Structured Query Language (SQL) (1).ppt
Intruduction to SQL.Structured Query Language(SQL}
structured query language elmarsi and navathe edition 7th SQL Chapter06.pptx
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Review of recent advances in non-invasive hemoglobin estimation
Building Integrated photovoltaic BIPV_UPV.pdf
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Digital-Transformation-Roadmap-for-Companies.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Understanding_Digital_Forensics_Presentation.pptx
Empathic Computing: Creating Shared Understanding
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Ad

DBMS_sql-ddl_V1.pdf890 ddl dml dql TCL DCL

  • 2. SQL • SQL = “Structured Query Language” • Standard query language for relational DBMSs • History: Developed at IBM in late 70s 1st standard: SQL-86 2nd standard: SQL-92 3rd standard: SQL-99 or SQL3, well over 1000 pages “The nice thing about standards is that you have so many to choose from!” -Andrew S. Tannenbaum 2  
  • 3. SQL Consists of two parts: • Data Definition Language (DDL) Allows the specification of the database schema • Data Manipulation Language (DML) Allows the specification of queries & insert/update/delete statements 3  
  • 4. SQL   data  defini.on   language  
  • 5. SQL Data Definition Language (DDL) 5   • Allows the specification of the database schema a set of relations with information about each relation • Schema information: - The schema of each relation - The domain of values associated with each attribute - Integrity constraints • Other information one can specify: - The set of indices to be maintained for each relation - Security and authorization information for each relation - The physical storage structure of each relation on disk
  • 6. CREATE TABLE Command • Used to define a relation • Syntax: CREATE TABLE relationName (attrName1 Domain1, … attrNamen Domainn (integrity-constraint1), …, (integrity-constraintn)) • Example: CREATE TABLE branch (branch_name char(15) not null, branch_city char(30), assets integer) 6  
  • 7. Domain Types in SQL • char(n) Fixed length character string, with user-specified length n • varchar(n) Variable length character strings, with user-specified maximum length n • int Integer (a finite subset of integers that is machine-dependent) • smallint Small integer (a machine-dependent subset of the integer domain type) 7  
  • 8. Domain Types in SQL • numeric(p, d) Fixed point number, with user-specified precision of p digits, with d digits to the right of decimal point • real, double precision Floating point and double-precision floating point numbers, with machine-dependent precision • float Floating point number, with user-specified precision of at least n digits and others… 8  
  • 9. CREATE TABLE Command • Can be used to also specify: - Primary key attributes (PRIMARY KEY keyword) - Secondary keys (UNIQUE keyword) - Referential integrity constraints/foreign keys (FOREIGN KEY keyword) • Example: CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP ) 9   Primary key declaration on an attribute automatically ensures not null in SQL-92 onwards, but it needs to be explicitly stated in SQL-89
  • 10. DROP TABLE Command • Used to remove a relation & its definition The relation can no longer be used in queries, updates, or any other commands since its description no longer exists • Syntax: DROP TABLE relationName • Example: DROP TABLE branch 10  
  • 11. 11 ALTER TABLE Command • Used to add/drop attributes from a relation • Add attribute syntax: ALTER TABLE relationName ADD attribName attribDomain All tuples in the relation are assigned null as the default value of the new attribute • Drop attribute syntax: ALTER TABLE relationName DROP attribName Dropping of attributes not supported by many DBMSs
  • 12. ALTER TABLE Command • Since new attribute will have NULL values right after the ALTER command is executed, the NOT NULL constraint is not allowed for such an attribute • Example: ALTER TABLE employee ADD job varchar(12) • The database users must still enter a value for the new attribute JOB for each EMPLOYEE tuple. This can be done using the UPDATE command. 12  
  • 13. Integrity Constraints • Guard against accidental damage to the database by ensuring that authorized changes to the database do not result in a loss of data consistency. • Examples: - A savings account must have a balance greater than $10,000.00 - A salary of a bank employee must be at least $6.00 an hour - A customer must have a (non-null) phone number 13  
  • 14. SQL Integrity Constraints • On single relations: - not null - primary key - unique - check(P), where P is a predicate • On multiple relations: - foreign key 14  
  • 15. NOT NULL Constraint • Specifies that an attribute does not accept null values • Can be specified as part of: - The definition of an attribute in the CREATE TABLE statement e.g. CREATE TABLE branch (branch_name char(15) not null, …) - The definition of a domain (i.e., a “type” that can be used where a type is needed) e.g. CREATE DOMAIN Dollars numeric(12, 2) not null 15  
  • 16. UNIQUE Constraint • Specifies that a set of attributes form a candidate key • Syntax: UNIQUE (AttrName1, …, AttrNamen) • Candidate keys are permitted to be null (in contrast to primary keys) 16  
  • 17. CHECK Clause 17   • Enforce a predicate (condition) • Syntax: CHECK (Predicate) • Example: Ensure that the values of the assets are non-negative CREATE TABLE branch (branch_name char(15), branch_city char(30), assets integer, primary key (branch_name), CHECK (assets >= 0) )
  • 18. CHECK Clause 18   • Can be also used to constrain domains e.g., CREATE DOMAIN hourly_wage numeric (5,2) CONSTRAINT value_test CHECK (value > = 4.00) • Can be named (useful to indicate which constraint an update violated) e.g., CREATE DOMAIN hourly_wage numeric (5,2) CONSTRAINT value_test CHECK (value > = 4.00)
  • 19. Referential Integrity • Ensures that a value that appears in one relation for a given set of attributes also appears for a set of attributes in another relation. • Example: If “La Jolla” is a branch name appearing in one of the tuples in the account relation, then there exists a tuple in the branch relation for branch “La Jolla”. 19  
  • 20. Referential Integrity • In the CREATE TABLE statement we can use: - The PRIMARY KEY clause to list primary key (PK) attributes. - The UNIQUE KEY clause to list candidate key attributes - The FOREIGN KEY clause to list foreign key (FK) attributes and the name of the relation referenced by the FK. By default, a FK references PK attributes of the referenced table. 20  
  • 21. Referential Integrity Example 21   create table customer (customer_name char(20), customer_street char(30), customer_city char(30), primary key (customer_name )) create table branch (branch_name char(15), branch_city char(30), assets numeric(12,2), primary key (branch_name ))
  • 22. Referential Integrity Example 22   create table account (account_number char(10), branch_name char(15), balance integer, primary key (account_number), foreign key (branch_name) references branch ) create table depositor (customer_name char(20), account_number char(10), primary key (customer_name, account_number), foreign key (account_number ) references account, foreign key (customer_name ) references customer )