0% found this document useful (0 votes)
0 views5 pages

SQL Set Operations and Constraints

The document outlines SQL set operations, including UNION, UNION ALL, INTERSECT, and EXCEPT, with examples for each. It also describes various SQL constraints such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and AUTO_INCREMENT, along with their implementation in table creation. Additionally, sample tables and data insertion commands for customers, suppliers, orders, and products are provided.

Uploaded by

maazrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

SQL Set Operations and Constraints

The document outlines SQL set operations, including UNION, UNION ALL, INTERSECT, and EXCEPT, with examples for each. It also describes various SQL constraints such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and AUTO_INCREMENT, along with their implementation in table creation. Additionally, sample tables and data insertion commands for customers, suppliers, orders, and products are provided.

Uploaded by

maazrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Set Operations and Constraints with Examples

1. SQL SET OPERATIONS

These combine results from multiple SELECT queries.

A. UNION (removes duplicates)

Example:

SELECT city FROM customers

UNION

SELECT city FROM suppliers;

B. UNION ALL (includes duplicates)

SELECT city FROM customers

UNION ALL

SELECT city FROM suppliers;

C. INTERSECT (common rows)

-- In PostgreSQL:

SELECT city FROM customers

INTERSECT

SELECT city FROM suppliers;

-- In MySQL:

SELECT city FROM customers

WHERE city IN (SELECT city FROM suppliers);


D. EXCEPT / MINUS (difference)

-- In PostgreSQL:

SELECT city FROM customers

EXCEPT

SELECT city FROM suppliers;

-- In MySQL:

SELECT city FROM customers

WHERE city NOT IN (SELECT city FROM suppliers);

2. SQL CONSTRAINTS

A. NOT NULL

CREATE TABLE users (

id INT NOT NULL,

name VARCHAR(50) NOT NULL

);

B. UNIQUE

CREATE TABLE users (

email VARCHAR(100) UNIQUE

);

C. PRIMARY KEY

CREATE TABLE users (

id INT PRIMARY KEY


);

D. FOREIGN KEY

CREATE TABLE orders (

id INT,

user_id INT,

FOREIGN KEY (user_id) REFERENCES users(id)

);

E. CHECK

CREATE TABLE employees (

age INT CHECK (age >= 18)

);

F. DEFAULT

CREATE TABLE products (

price DECIMAL(10,2) DEFAULT 0.00

);

G. AUTO_INCREMENT

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY

);

3. SAMPLE TABLES AND DATA INSERT


-- Customers Table

CREATE TABLE customers (

id INT PRIMARY KEY,

name VARCHAR(50),

city VARCHAR(50)

);

INSERT INTO customers VALUES (1, 'Ali', 'Lahore');

INSERT INTO customers VALUES (2, 'Sara', 'Karachi');

INSERT INTO customers VALUES (3, 'John', 'Lahore');

-- Suppliers Table

CREATE TABLE suppliers (

id INT PRIMARY KEY,

name VARCHAR(50),

city VARCHAR(50)

);

INSERT INTO suppliers VALUES (101, 'Ahmed Traders', 'Lahore');

INSERT INTO suppliers VALUES (102, 'Global Trade', 'Islamabad');

INSERT INTO suppliers VALUES (103, 'City Mart', 'Karachi');

-- Orders Table with FOREIGN KEY

CREATE TABLE orders (

order_id INT PRIMARY KEY,

customer_id INT,

amount DECIMAL(10,2),
FOREIGN KEY (customer_id) REFERENCES customers(id)

);

INSERT INTO orders VALUES (1001, 1, 500.00);

INSERT INTO orders VALUES (1002, 2, 700.50);

-- Products Table with DEFAULT

CREATE TABLE products (

product_id INT PRIMARY KEY,

name VARCHAR(50),

price DECIMAL(10,2) DEFAULT 0.00

);

INSERT INTO products (product_id, name) VALUES (1, 'Pen');

INSERT INTO products VALUES (2, 'Notebook', 50.00);

You might also like