SQL Nootes
SQL Nootes
DDL COMMANDS
19) Alter table customers add columns age integer not null
Alter table customers add constraints customer _age_check ( age > 13)
20) Alter table customers modify constraints users_age_check check (age>6) not
possible to edit constraints
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Session 32
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
28) Select model ,'smartphone' as 'type' from campusx --- to make the constant
column
32) Select * from smartphones where price BETWEEN 100000 and 200000
33) Select * from smartphones where price < 250000 and rating > 8000
SORTING DATA
40) Select * from campusX order by screen_size desc limit (x,y) ---- x se start y
rows print krdo
EXCEPT -- Pehli wali table ke woh element aayenge jo 2nd table me nhi h
( F J W G H S D O )
58)CTE:-
use flipkart;
with top_directors as (select director
from flipkart.movies
group by director
order by sum(gross) desc
limit 3)
60)
with top_duos as (select star ,director ,sum(gross)
from movies
group by star , director
order by sum(gross) desc limit 5)
Views ( Virtual table , not present physically on memory / logical table ) ---
Changes in table reflects on view also
Type - Single / Complex ------- On basis of no of joins
- Read only / Update view ------- On basis of if you can update (
Agar view updatable h toh view me change krne se original table me bhi change aa
jayega )
- If you have Aggregate fn / Distinct / Group by/ Having/ union /
Subquery / Certain joins then it will be read only view , you cant make this as
updatable view
- Materialized Views --- It can reduce the execution time / they
store the result ( result store krega so time reduce ho jayega )also so it reduce
the time
Advantage of materialized view fast / disadvantage - have to update
manually whenever you want the updated result kuki woh to store ho gya ab update
nhi hoga apne aap
68)
Create view joined_order_data as
Select order_id, amount, r_name , name ,date
from order_details t1
join orders t2
on t1.order_id=t2.order_id
join users t3
on t2.user_id=t3.user_id ------------------ Select * from joined_order_data (
Ab hum isse use kr sakte hai )
-- Stored Procedure ( block of sql statements and procedural procedural logic that
is stored in a database ) pehle se likhe huye sql statement and procedure that we
will use
END
-- CALL hello_world ( )
IF user_count = 0 THEN
INSERT INTO users (name,email) VALUES (input_name ,
input_email );
end
AUTOCOMMIT- Bina kisi explicit commit command diye changes ko save krna hi
autocommit h generally yahi hota hai but when you start transaction then autocommit
band ho jata h uss case me
how to turn off autocommit == SET autocommit = 0 now ab aapko khud se commit krna
padega
72)
START TRANSACTION ;
UPDATE person
set BALANCE = 4000 WHERE ID = 1;
UPDATE person
SET balance = 15000 WHERE ID = 4;
COMMIT / ROLLBACK ( rollback ulte direction me hota h )
SAVEPOINT A;
UPDATE person
set BALANCE = 4000 WHERE ID = 1;
SAVEPOINT B;
UPDATE person
SET balance = 15000 WHERE ID = 4;
ROLLBACK B;
ROLLBACK ;
1) Atomicity - all or none ( ya toh pure change shonge nahi toh koi nhi hoga )
2) Consistency- transaction only allowed if it must follow certain rules , or
constraint which ensures data integrity
3) Isolation- concurrent transaction do not interfere with each other / sab ek
doosre se isolate honge
4) Durability - agar commit ho gya toh permanent change store ho jayega
String Data Types ---
1) Char - fixed length string
--- Select name from movies like '_____' ---- Exactly 5 characterer
----Select name from movies where name like '%man%' --- kahi bhi man ho
-Upper(name)
-Lower(name)
-Concat(name,' ',director,' ',star)
-Substr(name,1,5) -- 1 se start krke 5 character dikha de (1 - position h 5- number
of character h)
-Replace (name, man, women) ---- name ke saare men ko women se replace krdo
-Reverse(name)
-char_length(name) vs length(name)
-Insert('hello world' ,7,3,'cccc') ------ hello world me 7th position se 3
character hata ke cccc replace krdo
-Left(name,3) left se 3 character dede
-Repeat(name,3) --- 3 baar name repeat krde
-Trim(name) --- space hata de
-Ltrim/ Rtrim
-Substring_index---- work as split function of python
substring_index('www.campusX.com','.',1) pehle . tk ka character de dega
5) Delete from laptops where 'index' in (Select 'index' from laptops where
column_name is null ) -------------- Delete null values
9) Update laptops l1
Set Ram = (select replace(ram,'GB','') from laptops l2 where
l2.index=l1.index)
14)