Professional Documents
Culture Documents
My SQL
My SQL
Boussekine Wissal
February 17, 2024
1 DML1 statements
Select: Used to read Data
– Functions
* count and AVG
· Definition: Built-in function that retrieves the number of rows matching the query criteria
· Syntax: Select count(<column name>) From <tablename> WHERE predicate ;
* Distinct
· Definition:Remove duplicate values from a result set
· Syntax: Select Distinct <column naem> From <tablename> WHERE predicate ;
* Limit
· Definition:Restrict the number of rows retrieved from the database
· Syntax: Select <column naem> From <tablename> WHERE predicate LIMIT number OFFSET initial indice-1
;
Insert
Update
1
Delete
2 DDL2 statements
CREATE TABLE
– Definition: used to add , modify the data type or remove a columns from the table.
– Syntax ⇐⇒
ALTER TABLE <TableName>
ADD < ColumnN ame1 > DataType
ADD ..
.
ADD < ColumnN amen > DataType ;
ALTER TABLE <TableName>
Update data type ALTER COLUMN < ColumnN amei >
SET DATA TYPE DataType ;
(
ALTER TABLE <TableName>
Drop
DROP COLUMN < ColumnN amei > ;
(
ALTER TABLE <TableName>
CHANGE
RENAME COLUMN < OldColumnName > < NewColumnName > ;
DROP
TRUNCATE
2
3 Refining your result
String Patterns and Ranges
– Forget the whole name of the query ⇐⇒ . . . WHERE <the attribut> LIKE ’. . . %’
– Range ⇐⇒ . . . WHERE <the attribut> BETWEEN A( min) AND A( max)
– Set of values ⇐⇒ . . . WHERE <the attribut> IN ( ’A1 ’ , ’A2 ’ , . . . ′ A′n )
– Of course we can use: AND and OR in WHERE condition
Sorting result set
– Ascending order ⇐⇒ SELECT * FROM <TableName> ORDER BY <NameOfColumn>
– Descending order ⇐⇒ SELECT * FROM <TableName> ORDER BY <NameOfColumn> DESC
– we can use column number e.g. SELECT * FROM <TableName> ORDER BY 2 i.e. the second column
Grouping result set
– Syntax ⇐⇒ SELECT * FROM <TableName> GROUP BY <NameOfColumn>
– Common exemple ⇐⇒ SELECT COUNT(*) AS <NameOfCountColumn> FROM <TableName> GROUP
BY <NameOfColumn>
– We can use HAVING to put condition e.g. . . . HAVING count(*)> b
Remarks
– To order the result of a Result Set, We can use ORDER BY multiple times e.g. ORDER BY <col1 > , . . . <coln >
Built-in DataBase functions
– Aggregate functions
Collection of values Single value
===========⇒ Agregate functions =======⇒
SUM()
MIN()
MAX()
AVG()
ROUND()
..
.
Used like count() function
– String functions
* LCASE()
* UCASE()
We can uses them as COUNT() or with WHERE when the data type of the column is VARCHAR or CHAR
Data and Time Built-in functions
YEAR()
MONTH()
DAY()
DAYOFMONTH()
DAYOFWEEK()
DAYOFYEAR()
YEAR()
WEEK()
HOUR()
MINUTE()
SECOND()
3
– We use them as COUNT() function or with WHERE
– We can use date arithmetic e.g. (giving date - n DATE) FROM . . .
– We can use the special registers: CURRENT DATE and CURRENT TIME