Documente Academic
Documente Profesional
Documente Cultură
ALTER TABLE -> Add/remove column; change column data type; change column
constraints
example: ALTER TABLE
tableName
ADD CONSTRAINT
FK_columnName
FOREIGN KEY
(columnName)
REFERENCES
table2Name
(column2Name)
USE dataBaseName; -> means you want to query through dataBaseName database
SELECT columnName FROM tableName -> return columnName date from table
INSERT INTO tableName(columnName1 , columnName2 ) VALUES (value1, value2)
UPDATE tableName SET columnName1 = value WHERE condition(for example id=1)
DELETE FROM tableName WHERE condition(for example id=1)
SELECT DISTINCT columnName FROM tableName -> returns only unique data from
column columnName
Boolean Operators:
AND a AND b
OR a OR b
BETWEEN BETWEEN a AND b
LIKE firstName LIKE "as%" -> first name starts with as
firstName LIKE "%as%" -> first name contains letters as
in it
firstName LIKE "%as" -> first name ends with as
IN firstName IN ('Igor','Brance') -> first name is Igor or
Brance
IS firstName IS NULL -> checks if firstname is NULL
IS NOT firstName IS NOT NULL
Shaping resuts:
ORDER BY
SET FUNCTIONS:
COUNT -> SELECT COUNT(columnName) FROM tableName -> counts the
number of rows in column columnName
MAX -> SELECT MAX(columnName) FROM tableName -> selects MAX
number from column columnName
MIN -> SELECT MIN(columnName) FROM tableName -> selects MIN
number from column columnName
AVG -> SELECT AVG(columnName) FROM tableName -> selects AVG of
all numbers from column columnName
SUM -> SELECT SUM(columnName) FROM tableName -> selects SUM of
all numbers from column columnName
GROUP BY -> groups additional columns to the SET FUNCTIONS
example:
SELECT COUNT(a.first_name), a.first_name
FROM actor a
GROUP BY a.first_name;
HAVING -> works like WHERE against SELECT
example:
SELECT COUNT(a.first_name), a.first_name
FROM actor a
GROUP BY a.first_name
HAVING a.first_name
LIKE 'A%'; - this will present all the
names which starts with A, and give count how many of each names are there in table
CROSS JOIN -> returns All rows from both tables, No where clause
example: SELECT
a.first_name, c.first_name
FROM
actor a, customer c;
USING KEYWORD:
SELECT s.StudentName, sc.ClassId = SELECT s.StudentName, sc.ClassId
From Students s From Students s
LEFT JOIN LEFT JOIN
StudentClass sc StudentClass sc
USING (StudentID); ON s.StudentID = sc.StudentID;
UNION KEYWORD:
Combines results from 2 or more SELECT statements. All Select statements must
return same number of columns
SCALAR FUNCTIONS:
STRING FUNCTIONS: MANIPULATES WITH STRINGS
NUMERIC FUNCTIONS: MANIPULATES WITH NUMBERS(MATH FUNCIONS)
DATE/TIME FUNCTIONS: MANIPULATES WITH DATE/TIME
CREATE FUNCTION -> The same as PROCEDURE, but can not modify any data, i.e. can
only return values