0% found this document useful (0 votes)
2 views

SQL class notes

The document outlines SQL commands for creating and managing databases and tables, including creating a new database, defining table structures, and performing CRUD operations. It also covers advanced topics such as backup, restore, and transaction management, as well as the use of the MERGE statement for data integration. Additionally, it includes comments on SQL syntax and data types, along with examples of various SQL operations.

Uploaded by

tejamente123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL class notes

The document outlines SQL commands for creating and managing databases and tables, including creating a new database, defining table structures, and performing CRUD operations. It also covers advanced topics such as backup, restore, and transaction management, as well as the use of the MERGE statement for data integration. Additionally, it includes comments on SQL syntax and data types, along with examples of various SQL operations.

Uploaded by

tejamente123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

-- Single Line comment

-- Query to create new Database


CREATE DATABASE DA_FEB_2025

/*
Multiple Comment
SQL Language is very easy to learn.
SQL Language is not case sensitive
*/

------------------- DDL ---------------------


---CREATE (It will be used to create the new object like table, view,index,
synonyms, function, store procedure
--- Rule to give the identifier name

CREATE TABLE customer_info


(
customer_id int,
customer_name varchar(200),
customer_city varchar(200),
customer_phonenumber varchar(200)
)

select * from customer_info

select customer_id,customer_name,customer_city,customer_phonenumber from


customer_info

INSERT INTO customer_info


(customer_id,customer_name,customer_city,customer_phonenumber)
VALUES
(101,'Pravin','Pune','9890899078')

101 Pravin Pune 9890899078


102 Natraj Hyderabad 236655778
103 Hema Mumbai 9878906758

-------------------------------

Create 5 Tables
Student_info
Employee_info
Customer_info
Sales_Info
Product_Info

Finish your installation

Create 5 Tables
Student_info
Employee_info
Customer_info
Sales_Info
Product_Info
--- Identifier Rules
/*
--Data Types
NUMERIC
SMALLINT
INT
BIGINT
DECIMAL
FLOAT
MONEY

CHARACTER
CHAR
VARCHAR
NCHAR
NVARCHAR
TEXT

DATE
DATE
TIME
DATETIME
DATETIME2
*/

-----------------------------------------------------------------

select * from customer_info

select customer_id,customer_name,customer_city,customer_phonenumber from


customer_info

--INSERT SINGLE LINE VALUE


INSERT INTO customer_info
(customer_id,customer_name,customer_city,customer_phonenumber)
VALUES
(101,'Pravin','Pune','9890899078')

--INSERT MULTIPLE LINE VALUE


INSERT INTO customer_info
(customer_id,customer_name,customer_city,customer_phonenumber)
VALUES
(104,'HARSHA','Pune','+91-9999999999'),
(102, 'Natraj', 'Hyderabad','+21-236655778'),
(103,'Hema','Mumbai','9878906758')

---- How to create a copy/Clone/Backup of existing table? / How to create backup


table

select customer_id,customer_name,customer_city,customer_phonenumber from


customer_info

SELECT * INTO customer_info_bkp_20250218


from customer_info

select customer_id,customer_name,customer_city,customer_phonenumber from


customer_info_bkp_20250218
select * from customer_info_bkp_20250218_1

SELECT customer_id,customer_name INTO customer_info_bkp_20250218_1


from customer_info

-----------DELETE

SELECT * FROM customer_info_bkp_20250218


-- It will delete all the records/Values from the table
Delete from customer_info_bkp_20250218

---How to delete specific records/values from the table

Delete from customer_info_bkp_20250218 WHERE customer_city = 'Hyderabad'

----------- How to insert the data from one table to another table.

INSERT INTO customer_info_bkp_20250218


(customer_id,customer_name,customer_city,customer_phonenumber)
SELECT customer_id,customer_name,customer_city,customer_phonenumber FROM
customer_info

INSERT INTO customer_info_bkp_20250218


SELECT * FROM customer_info

---------------------------------------------

SELECT * FROM customer_info_bkp_20250218 WHERE customer_city = 'Hyderabad'

---------------------------------

SELECT * FROM customer_info_bkp_20250218_1

DELETE FROM customer_info_bkp_20250218_1

--------- Remove/Drop entire table from database

DROP TABLE employee_info;

---------------------------------------- Update Statment

select * from employee_info

select * into employee_info_20250118 from employee_info

SELECT * FROM employee_info_20250118

---Update all rows/records/values


UPDATE employee_info_20250118 SET JOB_ID='Sales'

---Update specific rows/records/values

UPDATE employee_info_20250118 SET JOB_ID='Marketing' WHERE EMP_ID = 130


select * from employee_info_20250118

UPDATE employee_info_20250118 SET JOB_ID='Marketing' WHERE EMP_ID > 130

------------------------------- List of Operators


/*
1. Arithmetic Operator (+,-,/,*,%)
2.Comparison Operator(=,(<> !=),<=,>=,<,>)
3.Logical Operator (AND , OR, NOT)
4.Other (LIKE, BETWEEN,IS NULL, IN, ANY, ALL,EXISTS)
*/

SELECT 100+450

select SALARY, SALARY + SALARY * .1 AS 'SAL_WITH_HIKE' from employee_info_20250118

SELECT * FROM employee_info_20250118

-- Update the values for multiple columns

UPDATE employee_info_20250118 SET JOB_ID='IT',SALARY=99999 WHERE EMP_ID =121

------------ALTER STATEMENT
-- ADD Column
SELECT * FROM employee_info_20250118

ALTER TABLE employee_info_20250118 ADD location varchar(50)

SELECT * FROM Student_Info

-- Drop Column

ALTER TABLE employee_info_20250118 drop column location

--- Modify the column

ALTER TABLE employee_info_20250118 alter column first_name varchar(100)


ALTER TABLE Student_Info alter column Student_Class varchar(100)

INSERT INTO Student_Info (Student_Class) values


('Fifth Standard')

SELECT * FROM Student_Info

--------------------- Rename the table

EXEC sp_rename 'Student_Info', 'Student_Data'

SELECT * FROM Student_Data


--------------------- Rename the Column

EXECUTE sp_rename 'Student_Data', 'Student_Info'

SELECT * FROM Student_Info;

--------------------

ALTER TABLE Student_Info alter column Student_Class varchar(100)

-------

--- 50K

-------
/*
--Development Life Cycle Environments
1.Dev --
2.QA/ TESTING
3.UAT (User Acceptance Testing)
4.PRODUCTION
*/

--------------------- Rename the Column

SELECT * FROM Student_Info;

--UPDATE student_info SET Student_Date_Of_Birth='1991-04-12'

Exec sp_rename 'dbo.student_info.student_date_of_birth', 'date_of_birth'

------------------------------------- TRUNCATE

SELECT * FROM [dbo].[customer_info_bkp_20250218]


SELECT * FROM [dbo].[customer_info_bkp_20250219]

DELETE FROM [dbo].[customer_info_bkp_20250218]

TRUNCATE TABLE [dbo].[customer_info_bkp_20250219]

INSERT INTO [dbo].[customer_info_bkp_20250219] SELECT * FROM [dbo].[customer_info]

SELECT * FROM [dbo].[customer_info_bkp_20250218]


SELECT * FROM [dbo].[customer_info_bkp_20250219]

DELETE FROM [dbo].[customer_info_bkp_20250218] WHERE customer_city='Pune'

TRUNCATE TABLE [dbo].[customer_info_bkp_20250219] WHERE customer_city='Pune'


/*
DELETE
1.We can delete specific records
2.DELETE statement will delete the records one by one.
3.It will slower than TRUNCATE

TRUCATE
1.We can't delete specific records
2.TRUNCATE statement will remove all the records at one go.
3.It will faster than DELETE

DROP
1. DROP statment will remove entire table from database itself.

*/

-------------------------COMMIT/ROLLBACK/SAVEPOINT
---ROLLBACK: It undo the changes of any given statement including TRUCNATE/DROP .
-- to rollback the transaction we must to BEGIN THE transation using BEGIN
TRANSACTION clause

--COMMIT-- Save the data permernatly in the database which we cant rollback.
SELECT * FROM [dbo].[customer_info_bkp_20250218]

BEGIN TRANSACTION

DELETE FROM [customer_info_bkp_20250218]

SELECT * FROM [customer_info_bkp_20250218]

ROLLBACK

SELECT * FROM [customer_info_bkp_20250218]

-----------------------------------

BEGIN TRANSACTION

DROP TABLE customer_info_bkp_20250218

SELECT * FROM [customer_info_bkp_20250218]

ROLLBACK

---------------------

BEGIN TRANSACTION

SELECT * FROM [customer_info_bkp_20250219]

TRUNCATE TABLE customer_info_bkp_20250219

SELECT * FROM [customer_info_bkp_20250219]

ROLLBACK
SELECT * FROM [customer_info_bkp_20250219]

----------------------------------------------

BEGIN TRANSACTION

SELECT * FROM [customer_info_bkp_20250219]

TRUNCATE TABLE customer_info_bkp_20250219

SELECT * FROM [customer_info_bkp_20250219]

commit

rollback

----------- SAVEPONIT -----> Home work

------------ MERGE STATEMENT

CREATE TABLE [source_customer_info](


[s_customer_id] [int] ,
[s_customer_name] [varchar](200) ,
[s_customer_city] [varchar](200) ,
[s_customer_phonenumber] [varchar](200),
s_prod_purchase_date date
)

CREATE TABLE [dw_customer_info](


[t_customer_id] [int] ,
[t_customer_name] [varchar](200) ,
[t_customer_city] [varchar](200) ,
[t_customer_phonenumber] [varchar](200),
t_prod_purchase_date date
)

SELECT * FROM [source_customer_info];


SELECT * FROM [dw_customer_info];

INSERT INTO source_customer_info VALUES


(101, 'Pravin', 'Pune', '9890899078','2025-02-15'),
(102, 'Natraj', 'Hyderabad', '9089675643' ,'2025-02-15'),
(103, 'Hema', 'Mumbai', '9878906758', '2025-02-15')

MERGE INTO dw_customer_info


USING source_customer_info
ON s_customer_id= t_customer_id
WHEN MATCHED THEN
UPDATE SET t_customer_city=s_customer_city,
t_customer_phonenumber=s_customer_phonenumber
WHEN NOT MATCHED THEN
INSERT
(t_customer_id,t_customer_name,t_customer_city,t_customer_phonenumber,t_prod_purcha
se_date)
VALUES
(s_customer_id,s_customer_name,s_customer_city,s_customer_phonenumber,s_prod_purcha
se_date);

SELECT * FROM [source_customer_info];


SELECT * FROM [dw_customer_info];

TRUNCATE TABLE source_customer_info

INSERT INTO source_customer_info VALUES


(101, 'Pravin', 'Surat', '9890899078','2025-02-16'),
(104, 'KrushMurthy', 'Hyderabad', '777789789' ,'2025-02-16'),
(105, 'Heena', 'Mumbai', '7890675698', '2025-02-16')

101 Pravin Surat 9890899078 16-Feb-25


104 KrushMurthy Hyderabad 777789789 16-Feb-25
105 Heena Mumbai 7890675698 16-Feb-25

UPDATE [dw_customer_info] SET ID=40

TRUNCATE TABLE source_customer_info

SELECT * FROM [source_customer_info];


SELECT * FROM [dw_customer_info];

INSERT INTO source_customer_info VALUES


(106, 'Saritha', 'Kochi', '8888888888','2025-02-17'),
(102, 'Natraj', 'Hyderabad', '9999999999' ,'2025-02-17'),
(105, 'Heena', 'Pune', '7890675698', '2025-02-17')

Customer_ID Customer_Name Customer_City Customer Phone Number


Prod_Purchase_Date
106 Saritha Kochi 8888888888 17-Feb-25
102 Natraj Hyderabad 9999999999 17-Feb-25
105 Heena Pune 7890675698 17-Feb-25

-------------------------------------

Source System

Flat file ---- table

20Lac

---- Presentation skills & 3 Technical Demo session

Tomorrow 10 am to 2.00 pm

You might also like