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 )
Ad

More Related Content

Similar to Database Management Systems s-sql-ddl.pdf (20)

Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
Dbms & oracle
Dbms & oracleDbms & oracle
Dbms & oracle
J VijayaRaghavan
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
Unit 04 dbms
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
anuragmbst
 
lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptx
cAnhTrn53
 
dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.ppt
MARasheed3
 
6 integrity and security
6 integrity and security6 integrity and security
6 integrity and security
Dilip G R
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
koolkampus
 
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
SHAKIR325211
 
plsql Les09
 plsql Les09 plsql Les09
plsql Les09
sasa_eldoby
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
LPhct2
 
Oracle notes
Oracle notesOracle notes
Oracle notes
Prashant Dadmode
 
database management system presentation on integrity constraints
database management system presentation on integrity constraintsdatabase management system presentation on integrity constraints
database management system presentation on integrity constraints
MadhaviNandikonda
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle
Abrar ali
 
ch06_IntegritySecurity task in gdbms.pdf
ch06_IntegritySecurity task in gdbms.pdfch06_IntegritySecurity task in gdbms.pdf
ch06_IntegritySecurity task in gdbms.pdf
kapishverma2005
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
Abrar ali
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Integrity Constraints in Database Management System.ppt
Integrity Constraints in Database Management System.pptIntegrity Constraints in Database Management System.ppt
Integrity Constraints in Database Management System.ppt
Roshni814224
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
Mukesh Tekwani
 
lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptx
cAnhTrn53
 
dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.ppt
MARasheed3
 
6 integrity and security
6 integrity and security6 integrity and security
6 integrity and security
Dilip G R
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
koolkampus
 
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
SHAKIR325211
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
LPhct2
 
database management system presentation on integrity constraints
database management system presentation on integrity constraintsdatabase management system presentation on integrity constraints
database management system presentation on integrity constraints
MadhaviNandikonda
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle
Abrar ali
 
ch06_IntegritySecurity task in gdbms.pdf
ch06_IntegritySecurity task in gdbms.pdfch06_IntegritySecurity task in gdbms.pdf
ch06_IntegritySecurity task in gdbms.pdf
kapishverma2005
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
Abrar ali
 
Integrity Constraints in Database Management System.ppt
Integrity Constraints in Database Management System.pptIntegrity Constraints in Database Management System.ppt
Integrity Constraints in Database Management System.ppt
Roshni814224
 

Recently uploaded (20)

Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptxTop 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
mkubeusa
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptxTop 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
mkubeusa
 
Ad

Database Management Systems s-sql-ddl.pdf

  • 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 )
  翻译: