Get ready for your next interview with our comprehensive question library
ACID properties ensure database reliability:
Example:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT; -- Both operations succeed or both fail
PostgreSQL supports various data types:
Both ensure uniqueness, but with key differences:
PRIMARY KEY:
UNIQUE:
CREATE TABLE users (
id SERIAL PRIMARY KEY, -- Cannot be NULL
email VARCHAR(255) UNIQUE, -- Can have one NULL
username VARCHAR(50) UNIQUE -- Multiple unique constraints allowed
);
DELETE FROM users WHERE age < 18; -- Removes specific rows
TRUNCATE TABLE users; -- Removes all rows
DROP TABLE users; -- Removes table entirely
A foreign key establishes and enforces a link between data in two tables. It prevents actions that would destroy links between tables.
CREATE TABLE departments (
dept_id SERIAL PRIMARY KEY,
dept_name VARCHAR(100)
);
CREATE TABLE employees (
emp_id SERIAL PRIMARY KEY,
emp_name VARCHAR(100),
dept_id INTEGER REFERENCES departments(dept_id)
);
Foreign key constraints ensure referential integrity by preventing:
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime