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

Datamansam Database-Concepts

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

Datamansam Database-Concepts

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

DataBase Concepts Cheat Sheet

by datamansam via cheatography.com/139410/cs/29820/

DDL DQL (cont)

TABLE COLUMN Sorts the results of a column alphab​eti​‐ SELECT name_o​f_c​‐


Creation cally or numeri​cally, ascending by default olumn1, name_o​f_c​‐
olumn2 FROM
CREATE TABLE table_name ALTER TABLE
name_o​f_table
table_name
WHERE condition1
column​_name1, data_t​ype​(size), ADD column​_name
OR condit​ion2...
column​_name2 data_t​ype​(size), column​‐ datatype
ASC/DESC;
_name3 data_t​ype​(size),
Contains
Renaming
SELECT * FROM TableName WHERE Country IN
RENAME old_ta​ble​_name ALTER TABLE
('val1', 'val2', 'val3');
table_name
CURSOR
TO new_ta​ble​_name; RENAME old_co​lum​‐
A database cursor is an object that enables traversal over the rows of
n_name TO new__c​olu​‐
a result set. It allows you to process individual row returned by a
mn_​name;
query
Deleting
DECLARE all variables you’ll need
DROP TABLE Table_Name ALTER TABLE
DECLARE … CURSOR FOR SELECT DECLARE cursor​‐
Table_Name DROP
query, where you’ll declare a cursor and _name CURSOR FOR
COLUMN column​_name;
also define the query related to (popul​‐ select​_st​ate​ment;
ating) that cursor
DML
OPEN the cursor and FETCH NEXT from OPEN cursor​_na​‐
ADDING ROWS DELETING ROWS
the cursor me;....FETCH NEXT
INSERT INTO table_name (column1, DELETE FROM FROM cursor INTO
column2, column3, ...) table_name variab​le_​list;
VALUES (value1, value2, value3, ...); WHERE condition ( eg , In the WHILE loop you’ll test the WHILE @@FETC​H_S​‐
column2 = value2,; @@FETC​H_S​TATUS variable (WHILE TATUS = 0 BEGIN
UPDATING ROWS @@FETC​H_S​TATUS = 0). If the FETCH NEXT FROM
UPDATE table_name SET column1 = , column2 = value2, ... condition holds, you’ll enter the loop cursor​_name; END;
value1 WHERE condition; BEGIN … END block and perform
statements inside that block
DQL CLOSE the cursor and DEALLOCATE it. CLOSE cursor​_name;
DEALLOCATE cursor​‐
Wildcard Pattern Matching
_name;
SELECT column2, FROM table_name
CASE
column1,
WHERE '_r%' Finds any values 'a%' Finds any values
column2 that have "​r" in the that start with "​a".
LIKE pattern; second position
'a%o' Finds any values '%or%' Finds any
that start with "​a" and values that have "​or" in
ends with "​o" any position.
Sorting

By datamansam Published 19th November, 2021. Sponsored by Readable.com


Last updated 28th March, 2022. Measure your website readability!
Page 1 of 3. https://readable.com

cheatography.com/datamansam/
DataBase Concepts Cheat Sheet
by datamansam via cheatography.com/139410/cs/29820/

DQL (cont) DQL example (cont)

Goes through conditions and CASE WHEN condition1 THEN SELECT name_o​f_c​olumn1,
returns the value corres​ponding result1 WHEN condition2 THEN name_o​f_c​olumn2 FROM name_o​‐
to the first true condition (like an result2 WHEN conditionN THEN f_table WHERE condition1 OR
if-the​n-else statement) resultN ELSE result END; condit​ion2... ASC/DESC;
Contains
DDL Examples
SELECT * FROM Customers SELECT * FROM Customers
TABLE COLUMN WHERE Country IN ('Germ​‐ WHERE Country NOT IN ('Germ​‐
Creation any', 'France', 'UK'); any', 'France', 'UK');

CREATE TABLE ALTER TABLE Friends D CURSOR


friends Allows us to update one row at a time or perform an admini​str​ative
name varcha​r(100), ADD id int; process such as SQL Server database backups in a sequential
manner.
age int );
DECLARE all variables we DECLARE @produ​ct_name
Renaming
need VARCHA​R(MAX), @list_​price
RENAME friends ALTER TABLE fam
DECIMAL;
TO fam; RENAME name TO first_​name;
DECLARE … CURSOR FOR DECLARE cursor​_pr​oduct
Deleting SELECT naming our cursor CURSOR FOR SELECT produc​‐
DROP TABLE Fam; ALTER TABLE Fam DROP COLUMN and the query to find the t_name, list_price FROM produc​‐
age; values it will contain tio​n.p​rod​ucts;
OPEN the cursor and FETCH OPEN cursor​_pr​oduct; FETCH
DML Example NEXT from the cursor NEXT FROM cursor​_pr​oduct INTO
ADDING ROWS DELETING ROWS @produ​ct_​name, @list_​price;

INSERT INTO fam (id, name, age) DELETE FROM fam


VALUES (1, 'Ross', 31); WHERE condition (id =
1);
UPDATING ROWS
UPDATE fam SET name = 'Rachel WHERE id = 2;
Geller'

DQL example

Wildcard Pattern Matching


SELECT column2, FROM table_name
column1,
WHERE '_r%' Finds any values 'a%' Finds any values
column2 that have "​r" in the that start with "​a".
LIKE pattern; second position
'a%o' Finds any values '%or%' Finds any
that start with "​a" and values that have "​or" in
ends with "​o" any position.
Order By

By datamansam Published 19th November, 2021. Sponsored by Readable.com


Last updated 28th March, 2022. Measure your website readability!
Page 2 of 3. https://readable.com

cheatography.com/datamansam/
DataBase Concepts Cheat Sheet
by datamansam via cheatography.com/139410/cs/29820/

DQL example (cont)

The WHILE loop to test the weather WHILE @@FETC​H_S​‐


our condition returned values, when TATUS = 0 BEGIN PRINT
0 (meaning rows were returned), we @produ​ct_name + CAST(@​‐
fetch the specified values lis​t_price AS varchar);
FETCH NEXT FROM cursor​‐
_pr​oduct INTO @produ​ct_​‐
name, @list_​price; END;
CLOSE the cursor and CLOSE cursor​_pr​oduct;
DEALLOCATE it. DEALLOCATE cursor​_pr​‐
oduct;
Case
SELECT OrderID, Quantity, CASE SELECT Custom​erName,
WHEN Quantity > 30 THEN 'The City, Country FROM
quantity is greater than 30' WHEN Customers ORDER BY
Quantity = 30 THEN 'The quantity is (CASE WHEN City IS NULL
30' ELSE 'The quantity is under 30' THEN Country ELSE City
END AS Quanti​tyText FROM END);
OrderD​etails;

By datamansam Published 19th November, 2021. Sponsored by Readable.com


Last updated 28th March, 2022. Measure your website readability!
Page 3 of 3. https://readable.com

cheatography.com/datamansam/

You might also like