SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

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, Queue
Ghaffar Khan
 

Mais procurados (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.
 

Semelhante a 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
 

Último

Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
amitlee9823
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
only4webmaster01
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
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
Abortion pills in Riyadh +966572737505 get cytotec
 

Último (20)

Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
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
 

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