SlideShare una empresa de Scribd logo
1 de 58
SQL Fundamentals
Part - 3
Topics
1. Data Types
2. Primary Keys & Foreign Keys
3. Constraints
4. Insert, Update, Delete
5. Alter Table
6. Drop Table
7. Check Constraint
Big Picture
Why we need Data Types ?
Data Type
Data Types
Data Types
● On Creating the Database & Tables, Carefully consider which data type should
be used for the data to be stored.
● For Eg :
○ I want to store a phone number
○ Which Data type , it should be ?
Which data type should we use ?
Why Bother Numerics at all ?
Data Types
● No Arithmetic Operation
Data Types
● Take time to Plan
● Modifying Table Later will be painful
Primary Keys
● A primary key is a column or a group of columns used to identify
the row uniquely in a table.
● Primary Keys helps in Joining the table.
Primary Key
Why we need Primary Key ?
Foreign Key
● A foreign key is a field in a table that uniquely identifies a row in another table.
● A foreign key is defined in a table that references to the primary key of the
other table.
Why we need Foreign Key ?
Create Table
● To create Table.
Constraints
1. Rules enforced on data columns on table..
2. Used to prevent invalid data from being entered in the database.
3. Ensures accuracy and reliability of data.
Create Table
● To create Table.
CREATE TABLE table_name (
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
table_constraints
);
Create Table
CREATE TABLE table_name (
CREATE TABLE table_name (
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
table_constraints
);
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
table_constraints
);
CREATE TABLE players (
player_id SERIAL PRIMARY KEY,
age int NOT NULL
);
Serial
● It will create a sequence of the number in the increasing order.
● Used as a primary key.
Create Table
CREATE TABLE table_name (
CREATE TABLE table_name (
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
table_constraints
);
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
table_constraints
);
CREATE TABLE players (
player_id SERIAL PRIMARY KEY,
age int NOT NULL
);
Restaurant Database
1. Customer Table
2. Food Table
3. Order Table
Customer Table
CREATE TABLE customer (
customer_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
email VARCHAR(50) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL
);
Food Table
CREATE TABLE food (
food_id
name
type
created_on
);
Food Table
CREATE TABLE food (
food_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
type VARCHAR NOT NULL,
created_on TIMESTAMP NOT NULL
);
Order Table
CREATE TABLE orders (
customer_id ,
food_id ,
Order_time
);
Order Table
CREATE TABLE orders (
customer_id INTEGER REFERENCES customer(customer_id),
food_id INTEGER REFERENCES food(food_id),
order_time TIMESTAMP
);
INSERT
● Insert allows to add in rows to a table.
● No need to provide values for serial column.
Customer Table
INSERT INTO customer(name, age, email, created_on)
VALUES(‘Shravan’,24,’shravan@gmail.com’,CURRENT_TIMESTAMP);
Food Table
INSERT INTO food(name, type, created_on)
VALUES(‘Dosa’,’veg’,CURRENT_TIMESTAMP);
Order Table
INSERT INTO orders(customer_id, food_id, order_time)
VALUES(1,1,CURRENT_TIMESTAMP);
Update
● It allows for the changing of value of the columns in a table.
Update
● Update ALL Rows.
UPDATE CUSTOMER
SET age = 50 ;
Update
● Update specific row.
UPDATE CUSTOMER
SET age = 50
Where customer_id = 1 ;
Update
● Update specific row.
UPDATE CUSTOMER
SET created_on = CURRENT_TIMESTAMP
Where customer_id = 1 ;
Returning
UPDATE CUSTOMER
SET age = 50
Where customer_id = 1 ;
UPDATE CUSTOMER
SET created_on = CURRENT_TIMESTAMP
Where customer_id = 1 ;
RETURNING customer_id,age,email;
Delete
● It is used to remove all rows from a table.
DELETE FROM customer ;
Delete
● It is used to remove rows from a table.
DELETE FROM customer
Where customer_id = 1 ;
Delete + Returning
● It is used to remove rows from a table.
DELETE FROM customer
Where customer_id = 1
RETURNING customer_id, age, email;
Alter Table
1. Adding Dropping and Renaming Columns.
2. Changing a Column Data Type.
3. Set Default Values for a column.
4. Add Check Constraints.
5. Rename Table.
General Syntax
1. Adding Column
General Syntax
2. Removing Column
General Syntax
3. Alter Constraints
General Syntax
3. Alter Constraints
Drop Table
● The CASCADE option allows you to remove the table and its
dependent objects.
● The RESTRICT option rejects the removal if there is any object
depends on the table. The RESTRICT option is the default.
Drop Multiple Table
Check Constraint
Thank You

Más contenido relacionado

La actualidad más candente

Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
Vivek Singh
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE
Amrit Kaur
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 

La actualidad más candente (20)

Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
ADBMS Unit-II c
ADBMS Unit-II cADBMS Unit-II c
ADBMS Unit-II c
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
 
Data integrity
Data integrityData integrity
Data integrity
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
Dml and ddl
Dml and ddlDml and ddl
Dml and ddl
 
Sql DML
Sql DMLSql DML
Sql DML
 
SQL : Structured Query Language
SQL : Structured Query LanguageSQL : Structured Query Language
SQL : Structured Query Language
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Data integrity
Data integrityData integrity
Data integrity
 
Ddl commands
Ddl commandsDdl commands
Ddl commands
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 

Similar a Bootcamp sql fundamentals crud_part3

Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
Syed Zaid Irshad
 
e computer notes - Creating and managing tables
e computer notes -  Creating and managing tablese computer notes -  Creating and managing tables
e computer notes - Creating and managing tables
ecomputernotes
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
sagaroceanic11
 
sql_server_2016_history_tables
sql_server_2016_history_tablessql_server_2016_history_tables
sql_server_2016_history_tables
arthurjosemberg
 

Similar a Bootcamp sql fundamentals crud_part3 (20)

SQL
SQLSQL
SQL
 
Database
Database Database
Database
 
ADV PPT 2 LAB.pptx
ADV PPT 2 LAB.pptxADV PPT 2 LAB.pptx
ADV PPT 2 LAB.pptx
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Les09
Les09Les09
Les09
 
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
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
Les09
Les09Les09
Les09
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Mssql
MssqlMssql
Mssql
 
e computer notes - Creating and managing tables
e computer notes -  Creating and managing tablese computer notes -  Creating and managing tables
e computer notes - Creating and managing tables
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
 
sql_server_2016_history_tables
sql_server_2016_history_tablessql_server_2016_history_tables
sql_server_2016_history_tables
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3
 
Less08 Schema
Less08 SchemaLess08 Schema
Less08 Schema
 
Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
 

Último

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 

Último (20)

💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 

Bootcamp sql fundamentals crud_part3