?? Day 35: Exploring Practical SQL Implementation - Part 7??
JIGNESH KUMAR
Electrical & Instrumentation Engineer at SIC | Data Science Enthusiastic | ICE' 2024 SLIET
A. Create Table based Queries in SQL:-
1. Create Table:
CREATE TABLE employees (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
birthdate DATE
);
Explanation:
Real-Life Example:
2. Create Table with Constraints:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
product_id INT,
quantity INT,
CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(product_id),
CONSTRAINT chk_quantity CHECK (quantity > 0)
);
Explanation:
Real-Life Example:
3. Create Temporary Table:
CREATE TEMPORARY TABLE temp_sales AS
SELECT product_name, sales_quantity
FROM daily_sales
WHERE sales_date = CURRENT_DATE;
Explanation:
Real-Life Example:
4. Drop Table:
DROP TABLE outdated_data;
Explanation:
Real-Life Example:
B. Alter Table based Queries in SQL:
1. Add Column:
ALTER TABLE employees
ADD department VARCHAR(30);
Explanation:
Real-Life Example:
2. Drop Column:
ALTER TABLE orders
DROP COLUMN discount;
Explanation:
领英推荐
Real-Life Example:
3. Modify Column:
ALTER TABLE products
MODIFY COLUMN price DECIMAL(10, 2);
Explanation:
Real-Life Example:
4. Rename Column:
ALTER TABLE customers
CHANGE COLUMN email_address contact_email VARCHAR(100);
Explanation:
Real-Life Example:
5. Add Constraint:
ALTER TABLE products
ADD CONSTRAINT chk_price CHECK (price > 0);
Explanation:
Real-Life Example:
6. Drop Constraint:
ALTER TABLE employees
DROP CONSTRAINT fk_department;
Explanation:
Real-Life Example:
7. Rename Table:
ALTER TABLE old_table
RENAME TO new_table;
Explanation:
Real-Life Example: