0% found this document useful (0 votes)
8 views2 pages

SQL Queries Tables Operations

The document outlines various SQL operations for managing tables and data, including creating, altering, and deleting tables such as Products, Employees, and Orders. It also details modifications to existing tables, such as adding and renaming columns, as well as deleting records based on specific criteria. Additionally, it includes the creation of new tables for attendance and project assignments while enforcing constraints like NOT NULL on certain columns.

Uploaded by

sopoy59260
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)
8 views2 pages

SQL Queries Tables Operations

The document outlines various SQL operations for managing tables and data, including creating, altering, and deleting tables such as Products, Employees, and Orders. It also details modifications to existing tables, such as adding and renaming columns, as well as deleting records based on specific criteria. Additionally, it includes the creation of new tables for attendance and project assignments while enforcing constraints like NOT NULL on certain columns.

Uploaded by

sopoy59260
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/ 2

SQL Queries: Table Management and Data Operations

1. Table and Query Operations


-- Create Products Table --
CREATE TABLE Products (
product_id INTEGER PRIMARY KEY,
product_name VARCHAR(50),
category VARCHAR(30),
price DECIMAL(10, 2),
stock_quantity INTEGER
);
-- Add Column to Customer_Details --
ALTER TABLE Customer_Details ADD email VARCHAR(50);
-- Modify phone_number Column Length --
ALTER TABLE Customer_Details MODIFY phone_number VARCHAR(20);
-- Delete Products with Low Stock --
DELETE FROM Products WHERE stock_quantity < 5;
-- Drop Orders Table --
DROP TABLE Orders;

2. Employee Table and Modifications


-- Create Employees Table --
CREATE TABLE Employees (
employee_id INTEGER PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
department VARCHAR(30),
salary DECIMAL(10, 2),
date_of_joining DATE
);
-- Add shipment_status Column to Orders --
ALTER TABLE Orders ADD shipment_status VARCHAR(20);
-- Modify price Column Precision in Products --
ALTER TABLE Products MODIFY price DECIMAL(12, 4);
-- Delete Employees Joined Before 2015 and Salary < 3000 --
DELETE FROM Employees WHERE date_of_joining < DATE '2015-01-01' AND salary < 3000;

3. Order Details and Alterations


-- Create Order_Details Table --
CREATE TABLE Order_Details (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES Customer_Details(customer_id),
product_id INTEGER REFERENCES Products(product_id),
order_date DATE,
quantity INTEGER
);
-- Modify department Column Length in Employee Table --
ALTER TABLE Employees MODIFY department VARCHAR(50);
-- Drop middle_name Column from Customer_Details --
ALTER TABLE Customer_Details DROP COLUMN middle_name;
-- Delete Orders Before 2020 --
DELETE FROM Orders WHERE order_date < DATE '2020-01-01';

4. Attendance Table and Keys


-- Create Attendance Table --
CREATE TABLE Attendance (
attendance_id INTEGER PRIMARY KEY,
employee_id INTEGER REFERENCES Employees(employee_id),
attendance_date DATE,
status VARCHAR(10),
remarks VARCHAR(100)
);
-- Rename quantity to order_quantity in Order_Details --
ALTER TABLE Order_Details RENAME COLUMN quantity TO order_quantity;
-- Add Foreign Key supplier_id to Products --
ALTER TABLE Products ADD supplier_id INTEGER REFERENCES Suppliers(supplier_id);
-- Delete Duplicate Customers by Name --
DELETE FROM Customer_Details
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM Customer_Details
GROUP BY customer_name
);

5. Project Assignments and Constraints


-- Create Project_Assignments Table --
CREATE TABLE Project_Assignments (
assignment_id INTEGER PRIMARY KEY,
project_id INTEGER REFERENCES Projects(project_id),
employee_id INTEGER REFERENCES Employees(employee_id),
role VARCHAR(50),
start_date DATE,
end_date DATE
);
-- Set phone_number Column NOT NULL in Customer_Details --
ALTER TABLE Customer_Details MODIFY phone_number VARCHAR(20) NOT NULL;
-- Delete Products Supplied by New York Suppliers --
DELETE FROM Products
WHERE supplier_id IN (
SELECT supplier_id FROM Suppliers WHERE location = 'New York'
);
-- Drop Attendance Table --
DROP TABLE Attendance;

You might also like