EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL drop foreign key
Secondary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • PostgreSQL ANY
    • Entity Framework PostgreSQL
    • PostgreSQL UPSERT
    • PostgreSQL ON CONFLICT
    • PostgreSQL Port
    • Postgres List Schemas
    • PostgreSQL Update
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • PostgreSQL Case Insensitive
    • PostgreSQL Timestamp with Timezone
    • PostgreSQL Array Functions
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL? String Contains
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL?String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL LEAD()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL?Describe Table
    • PostgreSQL?Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL?Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL?TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL DELETE JOIN
    • PostgreSQL Float
    • PostgreSQL?OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL?MD5
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL?enum
    • PostgreSQL?OR
    • PostgreSQL Wal
    • PostgreSQL?NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL? Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL?Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL? flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL? Partition
    • PostgreSQL? column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL? Character Varying
    • PostgreSQL? Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
    • PostgreSQL drop foreign key
    • PostgreSQL export database
    • PostgreSQL Export CSV
    • PostgreSQL Performance Tuning
    • PostgreSQL Unnest
    • PostgreSQL show version
    • PostgreSQL Full Text Search
    • PostgreSQL max connections
    • Postgres Switch Database
    • PostgreSQL STRING_AGG()
    • PostgreSQL Date Format
    • PostgreSQL SUM()
    • PostgreSQL FORMAT
    • BETWEEN in PostgreSQL
    • PostgreSQL EXISTS
    • PostgreSQL Synonyms
    • PostgreSQL REGEXP_REPLACE
    • PostgreSQL group by month
    • UNION in PostgreSQL
    • PostgreSQL POSITION()
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQLTrigger Functions
    • PostgreSQL jsonb
    • PostgreSQL Table Schema
    • PostgreSQL group by day
    • Postgres ALTER TABLE
    • Postgres Connect to Database
    • PostgreSQL Numeric
    • PostgreSQL dblink
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Superuser
    • PostgreSQL ALL
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL? ORDER BY? Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query

PostgreSQL drop foreign key

PostgreSQL drop foreign key

Introduction to PostgreSQL drop foreign key

The following article provides an outline for PostgreSQL drop foreign key. PostgreSQL provides different types of keys to the user; basically, a foreign key is the one of the types of key in the PostgreSQL database. The foreign key is used to reference the table or column with the help of the primary key, and drop foreign key means we drop the foreign key from the table or columns. When we use a drop cascade in a statement, it means it will automatically delete all referenced rows from the table as this type of foreign key we called a dependent foreign key. Another way to drop foreign keys is to delete foreign keys, which does not depend on any other column or table.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Alter table table_name drop constraint key name;

Explanation:

  • In the above syntax, we use the alter table statement to drop foreign key constraint where table_name means specified table name from database and drop is used to delete the foreign key constraint from a database table. Also, the key name means actual foreign key name from the table we won’t drop.

How to drop foreign key in PostgreSQL?

  • We must install PostgreSQL in our system.
  • We require basic knowledge about PostgreSQL and key constraints of the database table.
  • We must require a database table to perform foreign key constraints.
  • We just need basic knowledge about the foreign key that means how it is used.
  • We can perform different operations on tables with the help of psql and pgAdmin.

Let’s see how we can implement foreign key constraints, and we will also see how to drop foreign key from database tables.

First, we need a foreign key to perform a drop foreign key constraint so let’s see how to create a foreign key as follows.

Example #1

Code:

CREATE TABLE sample(
emp_id INT GENERATED ALWAYS AS IDENTITY,
emp_name VARCHAR(255) NOT NULL,
PRIMARY KEY(emp_id)
);

Explanation:

  • In the above example, we use a create table to create a sample table with two attributes such as emp_id and emp_name, and here we assign the primary key to emp_id as shown in the statement.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

PostgreSQL drop foreign key 1

Now we insert a record into the sample table by using the following statement as follows.

Code:

INSERT INTO sample(emp_name)
VALUES('John'),
('Samm');

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,700 ratings)

Explanation:

  • With the help of the above statement, we insert two records into a sample table.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

insert a record into the sample table

Now we create another table by using the following statement as follows.

Code:

CREATE TABLE details (
cust_id INT GENERATED ALWAYS AS IDENTITY,
emp_id INT,
cust_name VARCHAR(255) NOT NULL,
cust_phone VARCHAR(15),
cust_email VARCHAR(100),
PRIMARY KEY(cust_id),
CONSTRAINT fk_sample
FOREIGN KEY(emp_id)
REFERENCES sample(emp_id)
ON DELETE CASCADE
);

Explanation:

  • With the help of the above statement, we created details tables with different attributes such as cust_id, cust_name, cust_phone, cust_email and here, we created a foreign key name as emp_id with reference to the sample table.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

PostgreSQL drop foreign key 3

Now we insert records into the details table by using the following statement as follows.

Code:

insert into details(emp_id, cust_name, cust_phone, cust_email)
values(1,'John','(202)-555-4321','john.123@gmail.com'),
(1,'John','(202)-555-4321','john.123@gmail.com'),
(2,'Sam','(101)-444-6541','sam.123@gmail.com');
select * from details;

Explanation:

  • With the help of the above insert statement, we insert records into the details table. See here we insert the same records into the table but as shown in the statement.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

insert records into the details

See in the above example we insert two same records into the table but violate the foreign key constraint. So we delete the first record by using the following statement as follows.

Code:

delete from details
where cust_id = 1;

In the above both examples, we created two tables such as sample and details for implementation of foreign key constraint.

So now, let’s see how we can drop the foreign key constraint by using the following statement as follows.

Example #2

Code:

alter table details drop constraint fk_sample;

Explanation:

  • In the above example, we use an alter table statement to drop foreign key constraints from the database table. In this example, table name details and foreign key name is fk_sample that we need to drop from the database table.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

Alter Table

Another way to delete foreign keys is that you can drop tables if they exist.

Syntax:

Drop table if exists table name;

Explanation:

  • In the above syntax, we drop table statements to drop the foreign key constraint where if exists means if a specified table is present in a database and table name means specified table name from the database.
  • See here when we use the above syntax, it deletes tables from databases, not particular foreign key columns, but when we use alter table statements in which we can delete specific foreign key constraints.

Let see a different example of a drop table as follows.

Example #3

Code:

drop table if exists sample;

Explanation:

  • In the above example, we use drop table statements to drop foreign key constraints in which that sample is the specified table name from the database that we need to drop. In this example, the sample table does not contain any foreign key; we just show how to drop the foreign key constraints by using a drop table statement.
  • Drop table statement deletes the whole table from the database, which means it directly deletes the table.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

PostgreSQL drop foreign key 6

Similarly, we will drop our second table by using a drop table statement, and that table contains foreign key constraint as follows.

Example #4

Code:

drop table if exists details;

Explanation:

  • In the above example, we use a drop table if it exists. In this example, we try to drop a details table that contains foreign key constraints, and it gives the reference to the sample table.
  • Illustrate the end result of the above declaration by using the following snapshot.

Output:

PostgreSQL drop foreign key 7

Conclusion

From the above article, we have seen the basic syntax drop foreign key constraint. We have also seen how we can implement them in PostgreSQL with different examples of each operation. From this article, we have seen how we can handle drop foreign key constraints in PostgreSQL.

Recommended Articles

This is a guide to PostgreSQL drop foreign key. Here we discuss the introduction and how to drop foreign key in PostgreSQL? You may also have a look at the following articles to learn more –

  1. PostgreSQL Clustered Index
  2. PostgreSQL group_concat
  3. PostgreSQL Float
  4. PostgreSQL DELETE
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more