SlideShare a Scribd company logo
1 of 21
Download to read offline
DataCamp Introduction to Relational Databases in SQL
Better data quality with
constraints
INTRODUCTION TO RELATIONAL DATABASES IN SQL
Timo Grossenbacher
Data Journalist
DataCamp Introduction to Relational Databases in SQL
Integrity constraints
1. Attribute constraints, e.g. data types on columns (Chapter 2)
2. Key constraints, e.g. primary keys (Chapter 3)
3. Referential integrity constraints, enforced through foreign keys (Chapter 4)
DataCamp Introduction to Relational Databases in SQL
Why constraints?
Constraints give the data structure
Constraints help with consistency, and thus data quality
Data quality is a business advantage / data science prerequisite
Enforcing is difficult, but PostgreSQL helps
DataCamp Introduction to Relational Databases in SQL
Data types as attribute constraints
From the .PostgreSQL documentation
DataCamp Introduction to Relational Databases in SQL
Dealing with data types (casting)
CREATE TABLE weather (
temperature integer,
wind_speed text);
SELECT temperature * wind_speed AS wind_chill
FROM weather;
operator does not exist: integer * text
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
SELECT temperature * CAST(wind_speed AS integer) AS wind_chill
FROM weather;
DataCamp Introduction to Relational Databases in SQL
Let's practice!
INTRODUCTION TO RELATIONAL DATABASES IN SQL
DataCamp Introduction to Relational Databases in SQL
Working with data types
INTRODUCTION TO RELATIONAL DATABASES IN SQL
Timo Grossenbacher
Data Journalist
DataCamp Introduction to Relational Databases in SQL
Working with data types
Enforced on columns (i.e. attributes)
Define the so-called "domain" of a column
Define what operations are possible
Enfore consistent storage of values
DataCamp Introduction to Relational Databases in SQL
The most common types
text: character strings of any length
varchar [ (x) ]: a maximum of n characters
char [ (x) ]: a fixed-length string of n characters
boolean: can only take three states, e.g. TRUE, FALSE and NULL (unknown)
From the .PostgreSQL documentation
DataCamp Introduction to Relational Databases in SQL
The most common types (cont'd.)
date, time and timestamp: various formats for date and time calculations
numeric: arbitrary precision numbers, e.g. 3.1457
integer: whole numbers in the range of -2147483648 and +2147483647
From the .PostgreSQL documentation
DataCamp Introduction to Relational Databases in SQL
Specifying types upon table creation
CREATE TABLE students (
ssn integer,
name varchar(64),
dob date,
average_grade numeric(3, 2), -- e.g. 5.54
tuition_paid boolean
);
DataCamp Introduction to Relational Databases in SQL
Alter types after table creation
ALTER TABLE students
ALTER COLUMN name
TYPE varchar(128);
ALTER TABLE students
ALTER COLUMN average_grade
TYPE integer
-- Turns 5.54 into 6, not 5, before type conversion
USING ROUND(average_grade);
DataCamp Introduction to Relational Databases in SQL
Let's apply this!
INTRODUCTION TO RELATIONAL DATABASES IN SQL
DataCamp Introduction to Relational Databases in SQL
The not-null and unique
constraints
INTRODUCTION TO RELATIONAL DATABASES IN SQL
Timo Grossenbacher
Data Journalist
DataCamp Introduction to Relational Databases in SQL
The not-null constraint
Disallow NULL values in a certain column
Must hold true for the current state
Must hold true for any future state
DataCamp Introduction to Relational Databases in SQL
What does NULL mean?
unknown
does not exist
does not apply
...
DataCamp Introduction to Relational Databases in SQL
What does NULL mean? An example
CREATE TABLE students (
ssn integer not null,
lastname varchar(64) not null,
home_phone integer,
office_phone integer
);
NULL != NULL
DataCamp Introduction to Relational Databases in SQL
How to add or remove a not-null constraint
When creating a table... After the table has been created...
CREATE TABLE students (
ssn integer not null,
lastname varchar(64) not null,
home_phone integer,
office_phone integer
);
ALTER TABLE students
ALTER COLUMN home_phone
SET NOT NULL;
ALTER TABLE students
ALTER COLUMN ssn
DROP NOT NULL;
DataCamp Introduction to Relational Databases in SQL
The unique constraint
Disallow duplicate values in a column
Must hold true for the current state
Must hold true for any future state
DataCamp Introduction to Relational Databases in SQL
Adding unique constraints
CREATE TABLE table_name (
column_name UNIQUE
);
ALTER TABLE table_name
ADD CONSTRAINT some_name UNIQUE(column_name);
DataCamp Introduction to Relational Databases in SQL
Let's apply this to the
database!
INTRODUCTION TO RELATIONAL DATABASES IN SQL

More Related Content

What's hot

Intro to t sql – 3rd session
Intro to t sql – 3rd sessionIntro to t sql – 3rd session
Intro to t sql – 3rd sessionMedhat Dawoud
 
data structure
data structuredata structure
data structurehashim102
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithmsVinayKumarV16
 
BAS 150 Lesson 6 Lecture
BAS 150 Lesson 6 LectureBAS 150 Lesson 6 Lecture
BAS 150 Lesson 6 LectureWake Tech BAS
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Hassan Ahmed
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureadeel hamid
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.Education Front
 
Chapter 1( intro & overview)
Chapter 1( intro & overview)Chapter 1( intro & overview)
Chapter 1( intro & overview)MUHAMMAD AAMIR
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURERohit Rai
 
BAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 LectureBAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 LectureWake Tech BAS
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005rainynovember12
 

What's hot (19)

Sql ppt
Sql pptSql ppt
Sql ppt
 
Intro to t sql – 3rd session
Intro to t sql – 3rd sessionIntro to t sql – 3rd session
Intro to t sql – 3rd session
 
data structure
data structuredata structure
data structure
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithms
 
BAS 150 Lesson 6 Lecture
BAS 150 Lesson 6 LectureBAS 150 Lesson 6 Lecture
BAS 150 Lesson 6 Lecture
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Data structure
Data structureData structure
Data structure
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
 
Chapter 1( intro & overview)
Chapter 1( intro & overview)Chapter 1( intro & overview)
Chapter 1( intro & overview)
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
BAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 LectureBAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 Lecture
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005
 
Database Architecture
Database ArchitectureDatabase Architecture
Database Architecture
 
Sql commands
Sql commandsSql commands
Sql commands
 
Data structure and its types.
Data structure and its types.Data structure and its types.
Data structure and its types.
 

Similar to Chapter2 (20)

Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
Module02
Module02Module02
Module02
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
DBMS LAB M.docx
DBMS LAB M.docxDBMS LAB M.docx
DBMS LAB M.docx
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Module 3
Module 3Module 3
Module 3
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
SQL
SQLSQL
SQL
 
98765432345671223Intro-to-PostgreSQL.ppt
98765432345671223Intro-to-PostgreSQL.ppt98765432345671223Intro-to-PostgreSQL.ppt
98765432345671223Intro-to-PostgreSQL.ppt
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
 
relational database
relational databaserelational database
relational database
 
PO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - SqlPO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - Sql
 
Sql
SqlSql
Sql
 
chapter9-SQL.pptx
chapter9-SQL.pptxchapter9-SQL.pptx
chapter9-SQL.pptx
 
New fordevelopersinsql server2008
New fordevelopersinsql server2008New fordevelopersinsql server2008
New fordevelopersinsql server2008
 
Sql
SqlSql
Sql
 

Recently uploaded

04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 

Recently uploaded (20)

Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 

Chapter2

  • 1. DataCamp Introduction to Relational Databases in SQL Better data quality with constraints INTRODUCTION TO RELATIONAL DATABASES IN SQL Timo Grossenbacher Data Journalist
  • 2. DataCamp Introduction to Relational Databases in SQL Integrity constraints 1. Attribute constraints, e.g. data types on columns (Chapter 2) 2. Key constraints, e.g. primary keys (Chapter 3) 3. Referential integrity constraints, enforced through foreign keys (Chapter 4)
  • 3. DataCamp Introduction to Relational Databases in SQL Why constraints? Constraints give the data structure Constraints help with consistency, and thus data quality Data quality is a business advantage / data science prerequisite Enforcing is difficult, but PostgreSQL helps
  • 4. DataCamp Introduction to Relational Databases in SQL Data types as attribute constraints From the .PostgreSQL documentation
  • 5. DataCamp Introduction to Relational Databases in SQL Dealing with data types (casting) CREATE TABLE weather ( temperature integer, wind_speed text); SELECT temperature * wind_speed AS wind_chill FROM weather; operator does not exist: integer * text HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. SELECT temperature * CAST(wind_speed AS integer) AS wind_chill FROM weather;
  • 6. DataCamp Introduction to Relational Databases in SQL Let's practice! INTRODUCTION TO RELATIONAL DATABASES IN SQL
  • 7. DataCamp Introduction to Relational Databases in SQL Working with data types INTRODUCTION TO RELATIONAL DATABASES IN SQL Timo Grossenbacher Data Journalist
  • 8. DataCamp Introduction to Relational Databases in SQL Working with data types Enforced on columns (i.e. attributes) Define the so-called "domain" of a column Define what operations are possible Enfore consistent storage of values
  • 9. DataCamp Introduction to Relational Databases in SQL The most common types text: character strings of any length varchar [ (x) ]: a maximum of n characters char [ (x) ]: a fixed-length string of n characters boolean: can only take three states, e.g. TRUE, FALSE and NULL (unknown) From the .PostgreSQL documentation
  • 10. DataCamp Introduction to Relational Databases in SQL The most common types (cont'd.) date, time and timestamp: various formats for date and time calculations numeric: arbitrary precision numbers, e.g. 3.1457 integer: whole numbers in the range of -2147483648 and +2147483647 From the .PostgreSQL documentation
  • 11. DataCamp Introduction to Relational Databases in SQL Specifying types upon table creation CREATE TABLE students ( ssn integer, name varchar(64), dob date, average_grade numeric(3, 2), -- e.g. 5.54 tuition_paid boolean );
  • 12. DataCamp Introduction to Relational Databases in SQL Alter types after table creation ALTER TABLE students ALTER COLUMN name TYPE varchar(128); ALTER TABLE students ALTER COLUMN average_grade TYPE integer -- Turns 5.54 into 6, not 5, before type conversion USING ROUND(average_grade);
  • 13. DataCamp Introduction to Relational Databases in SQL Let's apply this! INTRODUCTION TO RELATIONAL DATABASES IN SQL
  • 14. DataCamp Introduction to Relational Databases in SQL The not-null and unique constraints INTRODUCTION TO RELATIONAL DATABASES IN SQL Timo Grossenbacher Data Journalist
  • 15. DataCamp Introduction to Relational Databases in SQL The not-null constraint Disallow NULL values in a certain column Must hold true for the current state Must hold true for any future state
  • 16. DataCamp Introduction to Relational Databases in SQL What does NULL mean? unknown does not exist does not apply ...
  • 17. DataCamp Introduction to Relational Databases in SQL What does NULL mean? An example CREATE TABLE students ( ssn integer not null, lastname varchar(64) not null, home_phone integer, office_phone integer ); NULL != NULL
  • 18. DataCamp Introduction to Relational Databases in SQL How to add or remove a not-null constraint When creating a table... After the table has been created... CREATE TABLE students ( ssn integer not null, lastname varchar(64) not null, home_phone integer, office_phone integer ); ALTER TABLE students ALTER COLUMN home_phone SET NOT NULL; ALTER TABLE students ALTER COLUMN ssn DROP NOT NULL;
  • 19. DataCamp Introduction to Relational Databases in SQL The unique constraint Disallow duplicate values in a column Must hold true for the current state Must hold true for any future state
  • 20. DataCamp Introduction to Relational Databases in SQL Adding unique constraints CREATE TABLE table_name ( column_name UNIQUE ); ALTER TABLE table_name ADD CONSTRAINT some_name UNIQUE(column_name);
  • 21. DataCamp Introduction to Relational Databases in SQL Let's apply this to the database! INTRODUCTION TO RELATIONAL DATABASES IN SQL