SlideShare una empresa de Scribd logo
1 de 22
Rapid POSTGRESQL
learning, PART-2
BY: ALI MASUDIANPOUR MASUD.AMP@GMAIL.COM
RAPID POSTGRESQL LEARNING. 1
Creating Table
In psql maximum size of table is 32 terabytes.
In order to Create Table in psql we will follow the following syntax
◦ CREATE TABLE [table name] (
[Column name] [Column Data type],
…
);
◦ Example: CREATE TABLE sample_table (
id INTEGER,
fname VARCHAR(20),
lnameVARCHAR(25)
);
◦ Now if we write d sample_table we will see the table details:
RAPID POSTGRESQL LEARNING. 2
PRIMARY KEY CONSTRAINT
A primary key constraint is simply a combination of an UNIQUE constraint and a NOT NULL
constraint.
◦ How to define:
◦ First way
◦ CREATE TABLE test(
t1 INTEGER, t2 INTEGER, PRIMARY KEY (t1)
);
◦ Second way
◦ CREATE TABLE test(
t1 INTEGER PRIMARY KEY, t2 INTEGER
);
RAPID POSTGRESQL LEARNING. 3
FOREIGN KEY
A foreign key is a column or a group of columns that points to the primary key or another table.
◦ HOW TO DEFINE:
CREATE TABLE referenceTable(
id INTEGER PRIMARY KEY,
name VARCHAR(10)
);
CREATE TABLE testable(
id INTEGER PRIMARY KEY,
ref_id INTEGER REFERENCES referenceTable(id)
);
If we have a table with two or more foreign keys it would be something like bellow:
◦ CREATE TABLE referenceTable( id INTEGER, id1 INTEGER , PRIMARY KEY(id,id1));
◦ CREATE TABLE testable(id integer PRIMARY KEY, ref_id INTEGER, ref_id1 INTEGER, FOREIGN KEY(ref_id,red_id1) REFERENCES
referenceTable(id,id1));
RAPID POSTGRESQL LEARNING. 4
Check Constraint
We can use CHECK constraint when we need to check some values.
◦ HOW TO DEFINE CHECK CONSTRAINT
CREATE TABLE item(
id INTEGER PRIMARY KEY,
name VARCHAR(15),
price NUMERIC CHECK(price>0)
);
RAPID POSTGRESQL LEARNING. 5
NOT NULL Constraint
NOT NULL constraint is used to force a column that should not accept null value.
◦ HOW TO DEFINE:
◦ CREATE TABLE test(
M1 INTEGER NOT NULL,
M2 INTEGER CHECK (M2 IS NOT NULL)
);
RAPID POSTGRESQL LEARNING. 6
UNIQUE Constraint
If we need to have a column with unique values we have to use UNIQUE Constraint.
◦ How to Define
◦ CREATE TABLE test(
id INTEGER PRIMARY KEY,
email VARCHAR(52) UNIQUE,
nationalCode INTEGER(10),
CONSTRAINT national_code_unq_const UNIQUE(nationalCode)
);
◦ Above example uses 2 ways of defining UNIQUE Constraint
◦ Pay attention to the red color and blue color and you can find out which type of difference those have with each other.
RAPID POSTGRESQL LEARNING. 7
DEFAULT VALUES
We can specify a column to holds a default value if it had not a value while insert operation.
◦ How to define:
◦ CREATE TABLE test(
id INTEGER PRIMARY KEY,
test1 CHAR(5) DEFAULT ‘MASUD’,
test2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
◦ In above example we indicated that the default value of test1 column is <masud> and the default value
of test2 which it’s data type is TIMESTAMP would be CURRENT_TIMESTAMP
◦ Note that CURRENT_TIMESTAMP is a predefined constant in postgresql that shows current time stamp
RAPID POSTGRESQL LEARNING. 8
CASCADE
Have you ever tried to drop a table that has a dependency to another table?
You may checked that, but if not, I’ll tell you what would happen. If you do that you will face an
error. That error informs us that you are trying to drop a table that has a dependency on another
table.
So what is the solution?
◦ Solution would be CASCADE
◦ If we use CASCADE keyword after a command, it avoid dependencies and run the command on related
and depended tables or columns.
◦ How to use:
◦ DROP TABLE [table_name] CASCADE;
◦ Example: DROP TABLE testable CASCADE;
RAPID POSTGRESQL LEARNING. 9
CRUD
Crud stands for Create, read, update and delete.
In continue we will see how we can crud with Postgresql.
RAPID POSTGRESQL LEARNING. 10
INSERT
We can insert into table columns with the following syntax:
◦ INSERT INTO [TABLE NAME] ([COLUMNS])
VALUES ([values]);
◦ For instance we have the following table:
◦ CREATE TABLE ttst(
id SERIAL NOT NULL PRIMARY KEY
fname VARCHAR(10),
lname VARCHAR(30)
);
◦ INSERT INTO ttst (fname,lname) VALUES(‘Ali’,’MasudianPour’);
◦ INSERT INTO ttst (fname,lname) VALUES(‘Reza’,’Nejati’);
◦ INSERT INTO ttst (fname,lname) VALUES(‘Reza’,’Asi’);
◦ Now if we try to: SELECT * FROM ttst; The result would like the image:
RAPID POSTGRESQL LEARNING. 11
READ
In order to see table entries we use SELECT keyword, look at the example
◦ SELECT * FROM [TABLE NAME] ;
◦ This means that select all entries of table that we mentioned its name into square brackets.
◦ SELECT *COLUMN NAME+, *COLUMN NAME+, … FROM *TABLE NAME+
◦ This means that we can indicate which column we want to check and see related values.
◦ Example:
◦ SELECT id, fname FROM ttst;
RAPID POSTGRESQL LEARNING. 12
UPDATE
We can update columns with the following syntax:
◦ UPDATE [TABLE NAME] SET [COLUMN NAME] = [NEW VALUE]
◦ For example:
◦ UPDATE ttst SET fname=‘Masud’ WHERE id=5;
◦ After above command the select result would be similar to image
RAPID POSTGRESQL LEARNING. 13
DELETE
On order to delete a row we use DELETE Keyword.
◦ DELETE FROM [TABLE NAME] WHERE [CONDITION]
◦ For instance:
◦ DELETE FROM ttst WHERE id=6;
◦ As we talked about later, If we had some dependencies we use CASCADE keyword.
◦ For instance
◦ DELETE FROM ttst WHERE id=6 CASCADE;
RAPID POSTGRESQL LEARNING. 14
TRUNCATE
In order to empty all rows in a table we use TRUNCATE KEYWORD
◦ TRUNCATE TABLE [Table Name];
◦ For example:
◦ TRUNCATE TABLE ttst();
RAPID POSTGRESQL LEARNING. 15
Eliminate Duplications
In order to eliminate duplicate rows we use DISTINCT keyword.
◦ DISTINCT
◦ We can use DISTINCT keyword to eliminate duplications
◦ SELECT DISTINCT [*/COLUMN NAME] FROM [TABLE NAME];
◦ Example:
◦ SELECT DISTINCT * FROM ttst();
RAPID POSTGRESQL LEARNING. 16
Portion of a row
We can select data more accurate with writing conditions in WHERE clause
◦ For instance:
◦ SELECT * FROM ttst WHERE id=6
◦ SELECT * FROM ttst WHERE id>=10
◦ SELECT * FROM ttst WHERE fname=‘Ali’
◦ AND / OR
◦ SELECT * FROM ttst WHERE fname=‘Ali’ AND lname=‘MasudianPour’
◦ SELECT * FROM ttst WHERE fname =‘Reza’ OR flane=‘Ali’
RAPID POSTGRESQL LEARNING. 17
SORTING
We can sort the query result by using ORDER BY
◦ Take a look at below example
◦ SELECT * FROM ttst ORDER BY fname
◦ The result will select all values and sort them by fname and finally shows the output.
◦ ORDER BY has some switches
◦ DESC
◦ ORDER BY [column name] DESC
◦ Descending sort
◦ ASC
◦ ORDER BY [column name] ASC
◦ Ascending Sort
◦ NULLS FIRST
◦ ORDER BY [column name] DESC NULLS FIRST
◦ NULLS LAST
◦ ORDER BY [column name] DESC NULLS LAST
RAPID POSTGRESQL LEARNING. 18
ALIAS
Optionally, aliases can be declared for a column
◦ Example:
◦ WE USE AS KEYWORD TO USE ALIAS
◦ SELECT * FROM ttst AS e;
◦ Selects everything from ttst table and result will be known as e Alias
◦ SELECT fname fn, lname ln FROM ttst ORDER BY fn
◦ This example selects fname as fn and lname as ln and as you can see we used alias in ORDER BY
RAPID POSTGRESQL LEARNING. 19
LIMIT
Take a look at the below example
◦ SELECT * FROM ttst LIMIT 2
◦ The output will be limited to only 2 rows
◦ SELECT * FROM ttst LIMIT 10
◦ The output will be limited to only 10 rows
RAPID POSTGRESQL LEARNING. 20
OFFSET
AS we can limit our result with LIMIT clause, we can also set a range with OFFSET clause
◦ SELECT * FROM ttst OFFSET 4
◦ Above example selects all rows from ttst table and shows result with 4 offset. To be more clear it will avoid showing 4 first rows.
◦ SELECT * FROM ttst LIMIT 3 OFFSET 4
◦ Just like the previous example and following LIMIT
RAPID POSTGRESQL LEARNING. 21
END OF PART 2
End of Part 2
◦ In part 3 we will discuss about:
◦ COMBINING QUERIES with UNIOPN, INTERSECT and …
◦ Aggregation Functions
◦ GROUP BY and HAVING
◦ JOINS
◦ SUBQUERY EXPRESSIONS
◦ And …
RAPID POSTGRESQL LEARNING. 22

Más contenido relacionado

La actualidad más candente

BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung MosbachJohannes Hoppe
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraDeependra Ariyadewa
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-CassandraLilia Sfaxi
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)Johannes Hoppe
 
Joining tables
Joining tablesJoining tables
Joining tablespunu_82
 

La actualidad más candente (18)

BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQL
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
MongoDB-SESSION03
MongoDB-SESSION03MongoDB-SESSION03
MongoDB-SESSION03
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach
 
Django - sql alchemy - jquery
Django - sql alchemy - jqueryDjango - sql alchemy - jquery
Django - sql alchemy - jquery
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and Cassandra
 
Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
 
Msql
Msql Msql
Msql
 
Sql analytic queries tips
Sql analytic queries tipsSql analytic queries tips
Sql analytic queries tips
 
mysqlHiep.ppt
mysqlHiep.pptmysqlHiep.ppt
mysqlHiep.ppt
 
MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
 
Oracle PL-SQL
Oracle PL-SQLOracle PL-SQL
Oracle PL-SQL
 
MySql:Basics
MySql:BasicsMySql:Basics
MySql:Basics
 
Joining tables
Joining tablesJoining tables
Joining tables
 
NoSQL - Hands on
NoSQL - Hands onNoSQL - Hands on
NoSQL - Hands on
 

Destacado

Postgres rules
Postgres rulesPostgres rules
Postgres rulesgisborne
 
Rapid postgresql learning, part 4
Rapid postgresql learning, part 4Rapid postgresql learning, part 4
Rapid postgresql learning, part 4Ali MasudianPour
 
Prak4-Perintah Dasar Linux
Prak4-Perintah Dasar LinuxPrak4-Perintah Dasar Linux
Prak4-Perintah Dasar LinuxPutra Wanda
 
Prak5-Pengenalan Mikrotik
Prak5-Pengenalan MikrotikPrak5-Pengenalan Mikrotik
Prak5-Pengenalan MikrotikPutra Wanda
 
Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Ali MasudianPour
 
Tutorial Postgre SQL
Tutorial Postgre SQLTutorial Postgre SQL
Tutorial Postgre SQLHari Setiaji
 
Prak9-Bandwith Limiter
Prak9-Bandwith LimiterPrak9-Bandwith Limiter
Prak9-Bandwith LimiterPutra Wanda
 

Destacado (8)

Postgres rules
Postgres rulesPostgres rules
Postgres rules
 
Rapid postgresql learning, part 4
Rapid postgresql learning, part 4Rapid postgresql learning, part 4
Rapid postgresql learning, part 4
 
Prak4-Perintah Dasar Linux
Prak4-Perintah Dasar LinuxPrak4-Perintah Dasar Linux
Prak4-Perintah Dasar Linux
 
Prak2-Wireshark
Prak2-WiresharkPrak2-Wireshark
Prak2-Wireshark
 
Prak5-Pengenalan Mikrotik
Prak5-Pengenalan MikrotikPrak5-Pengenalan Mikrotik
Prak5-Pengenalan Mikrotik
 
Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Rapid postgresql learning, part 3
Rapid postgresql learning, part 3
 
Tutorial Postgre SQL
Tutorial Postgre SQLTutorial Postgre SQL
Tutorial Postgre SQL
 
Prak9-Bandwith Limiter
Prak9-Bandwith LimiterPrak9-Bandwith Limiter
Prak9-Bandwith Limiter
 

Similar a Rapid postgresql learning, part 2

Similar a Rapid postgresql learning, part 2 (20)

Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
8. sql
8. sql8. sql
8. sql
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
MYSQL
MYSQLMYSQL
MYSQL
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Oracle: Commands
Oracle: CommandsOracle: Commands
Oracle: Commands
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
Sql
SqlSql
Sql
 
Oracle
OracleOracle
Oracle
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
SQL
SQLSQL
SQL
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
 
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsgADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
 
Les02
Les02Les02
Les02
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
Mysql1
Mysql1Mysql1
Mysql1
 

Último

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Último (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

Rapid postgresql learning, part 2

  • 1. Rapid POSTGRESQL learning, PART-2 BY: ALI MASUDIANPOUR MASUD.AMP@GMAIL.COM RAPID POSTGRESQL LEARNING. 1
  • 2. Creating Table In psql maximum size of table is 32 terabytes. In order to Create Table in psql we will follow the following syntax ◦ CREATE TABLE [table name] ( [Column name] [Column Data type], … ); ◦ Example: CREATE TABLE sample_table ( id INTEGER, fname VARCHAR(20), lnameVARCHAR(25) ); ◦ Now if we write d sample_table we will see the table details: RAPID POSTGRESQL LEARNING. 2
  • 3. PRIMARY KEY CONSTRAINT A primary key constraint is simply a combination of an UNIQUE constraint and a NOT NULL constraint. ◦ How to define: ◦ First way ◦ CREATE TABLE test( t1 INTEGER, t2 INTEGER, PRIMARY KEY (t1) ); ◦ Second way ◦ CREATE TABLE test( t1 INTEGER PRIMARY KEY, t2 INTEGER ); RAPID POSTGRESQL LEARNING. 3
  • 4. FOREIGN KEY A foreign key is a column or a group of columns that points to the primary key or another table. ◦ HOW TO DEFINE: CREATE TABLE referenceTable( id INTEGER PRIMARY KEY, name VARCHAR(10) ); CREATE TABLE testable( id INTEGER PRIMARY KEY, ref_id INTEGER REFERENCES referenceTable(id) ); If we have a table with two or more foreign keys it would be something like bellow: ◦ CREATE TABLE referenceTable( id INTEGER, id1 INTEGER , PRIMARY KEY(id,id1)); ◦ CREATE TABLE testable(id integer PRIMARY KEY, ref_id INTEGER, ref_id1 INTEGER, FOREIGN KEY(ref_id,red_id1) REFERENCES referenceTable(id,id1)); RAPID POSTGRESQL LEARNING. 4
  • 5. Check Constraint We can use CHECK constraint when we need to check some values. ◦ HOW TO DEFINE CHECK CONSTRAINT CREATE TABLE item( id INTEGER PRIMARY KEY, name VARCHAR(15), price NUMERIC CHECK(price>0) ); RAPID POSTGRESQL LEARNING. 5
  • 6. NOT NULL Constraint NOT NULL constraint is used to force a column that should not accept null value. ◦ HOW TO DEFINE: ◦ CREATE TABLE test( M1 INTEGER NOT NULL, M2 INTEGER CHECK (M2 IS NOT NULL) ); RAPID POSTGRESQL LEARNING. 6
  • 7. UNIQUE Constraint If we need to have a column with unique values we have to use UNIQUE Constraint. ◦ How to Define ◦ CREATE TABLE test( id INTEGER PRIMARY KEY, email VARCHAR(52) UNIQUE, nationalCode INTEGER(10), CONSTRAINT national_code_unq_const UNIQUE(nationalCode) ); ◦ Above example uses 2 ways of defining UNIQUE Constraint ◦ Pay attention to the red color and blue color and you can find out which type of difference those have with each other. RAPID POSTGRESQL LEARNING. 7
  • 8. DEFAULT VALUES We can specify a column to holds a default value if it had not a value while insert operation. ◦ How to define: ◦ CREATE TABLE test( id INTEGER PRIMARY KEY, test1 CHAR(5) DEFAULT ‘MASUD’, test2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ◦ In above example we indicated that the default value of test1 column is <masud> and the default value of test2 which it’s data type is TIMESTAMP would be CURRENT_TIMESTAMP ◦ Note that CURRENT_TIMESTAMP is a predefined constant in postgresql that shows current time stamp RAPID POSTGRESQL LEARNING. 8
  • 9. CASCADE Have you ever tried to drop a table that has a dependency to another table? You may checked that, but if not, I’ll tell you what would happen. If you do that you will face an error. That error informs us that you are trying to drop a table that has a dependency on another table. So what is the solution? ◦ Solution would be CASCADE ◦ If we use CASCADE keyword after a command, it avoid dependencies and run the command on related and depended tables or columns. ◦ How to use: ◦ DROP TABLE [table_name] CASCADE; ◦ Example: DROP TABLE testable CASCADE; RAPID POSTGRESQL LEARNING. 9
  • 10. CRUD Crud stands for Create, read, update and delete. In continue we will see how we can crud with Postgresql. RAPID POSTGRESQL LEARNING. 10
  • 11. INSERT We can insert into table columns with the following syntax: ◦ INSERT INTO [TABLE NAME] ([COLUMNS]) VALUES ([values]); ◦ For instance we have the following table: ◦ CREATE TABLE ttst( id SERIAL NOT NULL PRIMARY KEY fname VARCHAR(10), lname VARCHAR(30) ); ◦ INSERT INTO ttst (fname,lname) VALUES(‘Ali’,’MasudianPour’); ◦ INSERT INTO ttst (fname,lname) VALUES(‘Reza’,’Nejati’); ◦ INSERT INTO ttst (fname,lname) VALUES(‘Reza’,’Asi’); ◦ Now if we try to: SELECT * FROM ttst; The result would like the image: RAPID POSTGRESQL LEARNING. 11
  • 12. READ In order to see table entries we use SELECT keyword, look at the example ◦ SELECT * FROM [TABLE NAME] ; ◦ This means that select all entries of table that we mentioned its name into square brackets. ◦ SELECT *COLUMN NAME+, *COLUMN NAME+, … FROM *TABLE NAME+ ◦ This means that we can indicate which column we want to check and see related values. ◦ Example: ◦ SELECT id, fname FROM ttst; RAPID POSTGRESQL LEARNING. 12
  • 13. UPDATE We can update columns with the following syntax: ◦ UPDATE [TABLE NAME] SET [COLUMN NAME] = [NEW VALUE] ◦ For example: ◦ UPDATE ttst SET fname=‘Masud’ WHERE id=5; ◦ After above command the select result would be similar to image RAPID POSTGRESQL LEARNING. 13
  • 14. DELETE On order to delete a row we use DELETE Keyword. ◦ DELETE FROM [TABLE NAME] WHERE [CONDITION] ◦ For instance: ◦ DELETE FROM ttst WHERE id=6; ◦ As we talked about later, If we had some dependencies we use CASCADE keyword. ◦ For instance ◦ DELETE FROM ttst WHERE id=6 CASCADE; RAPID POSTGRESQL LEARNING. 14
  • 15. TRUNCATE In order to empty all rows in a table we use TRUNCATE KEYWORD ◦ TRUNCATE TABLE [Table Name]; ◦ For example: ◦ TRUNCATE TABLE ttst(); RAPID POSTGRESQL LEARNING. 15
  • 16. Eliminate Duplications In order to eliminate duplicate rows we use DISTINCT keyword. ◦ DISTINCT ◦ We can use DISTINCT keyword to eliminate duplications ◦ SELECT DISTINCT [*/COLUMN NAME] FROM [TABLE NAME]; ◦ Example: ◦ SELECT DISTINCT * FROM ttst(); RAPID POSTGRESQL LEARNING. 16
  • 17. Portion of a row We can select data more accurate with writing conditions in WHERE clause ◦ For instance: ◦ SELECT * FROM ttst WHERE id=6 ◦ SELECT * FROM ttst WHERE id>=10 ◦ SELECT * FROM ttst WHERE fname=‘Ali’ ◦ AND / OR ◦ SELECT * FROM ttst WHERE fname=‘Ali’ AND lname=‘MasudianPour’ ◦ SELECT * FROM ttst WHERE fname =‘Reza’ OR flane=‘Ali’ RAPID POSTGRESQL LEARNING. 17
  • 18. SORTING We can sort the query result by using ORDER BY ◦ Take a look at below example ◦ SELECT * FROM ttst ORDER BY fname ◦ The result will select all values and sort them by fname and finally shows the output. ◦ ORDER BY has some switches ◦ DESC ◦ ORDER BY [column name] DESC ◦ Descending sort ◦ ASC ◦ ORDER BY [column name] ASC ◦ Ascending Sort ◦ NULLS FIRST ◦ ORDER BY [column name] DESC NULLS FIRST ◦ NULLS LAST ◦ ORDER BY [column name] DESC NULLS LAST RAPID POSTGRESQL LEARNING. 18
  • 19. ALIAS Optionally, aliases can be declared for a column ◦ Example: ◦ WE USE AS KEYWORD TO USE ALIAS ◦ SELECT * FROM ttst AS e; ◦ Selects everything from ttst table and result will be known as e Alias ◦ SELECT fname fn, lname ln FROM ttst ORDER BY fn ◦ This example selects fname as fn and lname as ln and as you can see we used alias in ORDER BY RAPID POSTGRESQL LEARNING. 19
  • 20. LIMIT Take a look at the below example ◦ SELECT * FROM ttst LIMIT 2 ◦ The output will be limited to only 2 rows ◦ SELECT * FROM ttst LIMIT 10 ◦ The output will be limited to only 10 rows RAPID POSTGRESQL LEARNING. 20
  • 21. OFFSET AS we can limit our result with LIMIT clause, we can also set a range with OFFSET clause ◦ SELECT * FROM ttst OFFSET 4 ◦ Above example selects all rows from ttst table and shows result with 4 offset. To be more clear it will avoid showing 4 first rows. ◦ SELECT * FROM ttst LIMIT 3 OFFSET 4 ◦ Just like the previous example and following LIMIT RAPID POSTGRESQL LEARNING. 21
  • 22. END OF PART 2 End of Part 2 ◦ In part 3 we will discuss about: ◦ COMBINING QUERIES with UNIOPN, INTERSECT and … ◦ Aggregation Functions ◦ GROUP BY and HAVING ◦ JOINS ◦ SUBQUERY EXPRESSIONS ◦ And … RAPID POSTGRESQL LEARNING. 22