From 62c951cac84957ba63bd3bc150746921f14aa832 Mon Sep 17 00:00:00 2001 From: Abdul <skabdulkhadeer343@gmail.com> Date: Fri, 29 Nov 2024 17:15:51 +0530 Subject: [PATCH] Sql Assignment-1 Submission --- .../AbdulShaik_1546_Assignment1.sql | 266 ++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 Abdul/SqlAssignments/AbdulShaik_1546_Assignment1.sql diff --git a/Abdul/SqlAssignments/AbdulShaik_1546_Assignment1.sql b/Abdul/SqlAssignments/AbdulShaik_1546_Assignment1.sql new file mode 100644 index 0000000..901d238 --- /dev/null +++ b/Abdul/SqlAssignments/AbdulShaik_1546_Assignment1.sql @@ -0,0 +1,266 @@ +Create Database AssignmentDB1 + +---------------------------Question1------------------- +--Create a table with Check, Default constraints + +USE AssignmentDB1; +CREATE TABLE GovernmentEmployee ( + GovernmentEmployeeID int PRIMARY KEY IDENTITY(1,1), + Name varchar(100) NOT NULL, + Age int CHECK (Age >= 18 AND Age <= 65), + JoiningDate date DEFAULT GETDATE() +); +---------------------------Question2------------------- +/*Create a table with all the below data types and insert records +INT, Varchar, Char, Nvarchar, Float, Double, Numeric, Date, Datetime*/ + +USE AssignmentDB1; +CREATE TABLE Item ( + ItemID int PRIMARY KEY IDENTITY(1,1), + Name varchar(50), + Status char(10), + Description nvarchar(100), + Price float, + Discount double precision, + TaxRate numeric(5, 2), + CreatedDate date, + LastUpdated datetime +); + +---------------------------Question 3------------------------- +/*Create table with Identity and do below operations +DELETE few rows and then Insert some records. +Observe the Identity value and same process do it for TRUNCATE +*/ +USE AssignmentDB1; +CREATE TABLE Student ( + StudentID int IDENTITY(1,1), + Name nvarchar(50) +); + +use AssignmentDB1; +INSERT INTO Student VALUES +('Khadeer'), +('Abdul'), +('Vamsi'), +('Raju'), +('Sathvik'); + +-- It will be deleting last row +DELETE FROM Student WHERE StudentID = IDENT_CURRENT('Student'); +-- Deleting the name with Sathvik +DELETE from Student where Name = 'Raju'; + +--when I Inserted new data after delete, its identity column will update from last identity +INSERT into Student Values +('Vamsi'), +('Sathvik'); + +--Truncating table +TRUNCATE TABLE Student + +--when I Inserted new data after truncating, its identity column will reset to initial state. +INSERT INTO Student VALUES +('Khadeer'), +('Abdul'); + +-----------------------Question 4-------------------------- +/*Create a table with Computed column*/ + +--Created Computed Column +USE AssignmentDB1; +CREATE TABLE ComputedColumnDemo ( + ItemPrice money, + TaxRate float, + TotalPrice as (ItemPrice + (ItemPrice * TaxRate / 100)) +); + +----------------------Question 5----------------------------- +/*Create two tables one with Primary key and other with foreign key +Try to insert the record which is not there in primary key into the table where Foreign key created +Try to insert the NULL values into foreign key table*/ + +CREATE TABLE Department ( + DepartmentID int PRIMARY KEY IDENTITY(1,1), + Name nvarchar(50) NOT NULL +); + +CREATE TABLE Employee ( + EmployeeID int PRIMARY KEY IDENTITY(1,1), + Name nvarchar(50) NOT NULL, + DepartmentID int NOT NULL, + FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID) +); + +--Try to insert the record which is not there in primary key into the table where Foreign key created +INSERT INTO Employee (Name, DepartmentID) VALUES ('Ganesh', 10); +--Result +/*The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Employee__Depart__534D60F1". +The conflict occurred in database "AssignmentDB1", table "dbo.Department", column 'DepartmentID'.*/ + + +-- Trying to enter null in foreign key +INSERT INTO Employee VALUES ('Ganesh', NULL); +--Result +/*Cannot insert the value NULL into column 'DepartmentID', table 'AssignmentDB1.dbo.Employee'; +column does not allow nulls. INSERT fails.*/ + +--Altering table to insert null values +ALTER TABLE Employee +ALTER COLUMN DepartmentID int NULL; + +INSERT INTO Employee VALUES ('Ganesh', NULL); + +-------------------------Question 6---------------------------------------------- +/*Practice Cascade command*/ + + +--Find Constraint Related to Employee Table +SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'Employee'; + +-- Drop the existing foreign key constraint +ALTER TABLE Employee +DROP CONSTRAINT FK_Employee_Department; + +-- Add a new foreign key constraint with cascading updates and deletes +ALTER TABLE Employee +ADD CONSTRAINT FK_Employee_Department +FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID) +ON UPDATE CASCADE +ON DELETE CASCADE; + +--Inserting Department (Parent Table) Data +INSERT INTO Department VALUES +('CSE'), +('CSIT'), +('ECE'), +('EEE'), +('MECH'); + +--Inserting Employee (Child Table) Data +INSERT INTO Employee VALUES +('Abdul Shaik',1), +('Vamsi',1), +('Laxman',1), +('Harsha',1), +('Tejesh',2), +('Prasanth',2), +('Nandini',2), +('Chandu',3), +('Lokesh',4), +('Deekshitha',5); + + +DELETE FROM Department WHERE DepartmentID = 2; + +--Doing Update Cascading + +--Find Constraint Related to Department Table +SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'Department'; + +--droping column because it is Identity coulmn +ALTER TABLE Department +DROP COLUMN DepartmentID; + +TRUNCATE TABLE Department + +ALTER TABLE Department +ADD DepartmentID int PRIMARY KEY; + +INSERT INTO Department ( DepartmentID, Name ) +VALUES +(1,'CSE'), +(2,'CSIT'), +(3,'ECE'), +(4,'EEE'), +(5,'MECH'); + +ALTER TABLE Employee +ADD CONSTRAINT FK_Employee_Department +FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID) +ON UPDATE CASCADE +ON DELETE CASCADE; + +--checking update cascading +UPDATE Department SET DepartmentID = 10 where DepartmentID = 1; + +-------------------Question 7----------------------------- +/*Alter table: +Do column data type and length change +Rename the column name +Add new column +Drop existing column +Remove Constraint*/ + + +-- Change column data type and length +ALTER TABLE Employee ALTER COLUMN Name nvarchar(200); + +-- Rename a column +EXEC sp_rename 'Employee.Name', 'FullName', 'COLUMN'; + +-- Add a new column +ALTER TABLE Employee +ADD Address nvarchar(255); + +-- Drop an existing column +ALTER TABLE Employee +DROP COLUMN Address; + +--Find Constraint Related to Employee Table +SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'Employee'; + +-- Removing a Foreign Key constraint +ALTER TABLE Employee +DROP CONSTRAINT FK_Employee_Department; + + +-----------------------Question 8----------------------- +/*Check what all properties are inheriting when we do SELECT * INTO <<new table>> FROM <<oldtable>>*/ + +-- Create Category table +CREATE TABLE Category ( + CategoryID int PRIMARY KEY IDENTITY(1,1), + CategoryName nvarchar(100) NOT NULL, + Description nvarchar(255) +); + +-- Create Product table +CREATE TABLE [Product] ( + ProductID int PRIMARY KEY IDENTITY(1,1), + ProductName nvarchar(100) NOT NULL, + Price decimal(10, 2) NOT NULL, + StockQuantity int DEFAULT 0, + CategoryID int NOT NULL, + CONSTRAINT FK_Category FOREIGN KEY (CategoryID) REFERENCES Category(CategoryID) +); + +-- Insert sample data into Category +INSERT INTO Category (CategoryName, Description) VALUES +('Electronics', 'Devices and gadgets'), +('Books', 'Various genres of literature'), +('Clothing', 'Apparel and accessories'); + +-- Insert sample data into Product +INSERT INTO Product (ProductName, Price, StockQuantity, CategoryID) VALUES +('Laptop', 999.99, 10, 1), +('Smartphone', 499.99, 25, 1), +('Novel', 19.99, 100, 2), +('T-Shirt', 14.99, 50, 3); + +-- Create a new table based on Product +SELECT * INTO NewProduct FROM [Product]; + +EXEC sp_help 'NewProduct'; + +SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'NewProduct'; + + +INSERT INTO NewProduct VALUES +('Tab', 999.99, NUll, 1); + +--Successfully Copied from one table to another +--Identity, Nullable(NOT NULL/ NULL) + +--Failed to copy from one table to another +--Foreign Key, Default, Unique,Primary key \ No newline at end of file -- GitLab