You are on page 1of 4

MySql coursera’s course

Boussekine Wissal
February 17, 2024

1 DML1 statements
ˆ Select: Used to read Data

– Select statement ⇐⇒ Query


– Result from the Query ⇐⇒ Result set
– (
Select * From <tablename>
Syntax
Select <co1>,<col2> . . . <coln>From <tablename>

– Restrect the result set ⇐⇒ Select * From <tablename> WHERE predicate




 = ⇐⇒ equal to
< ⇐⇒ lesser than





> ⇐⇒ greater than



predicate ≥ ⇐⇒ greater than or equal to

≤ ⇐⇒lesser than or equal tol








 <> ⇐⇒ not equal to
in(val1 ,. . . ,valn ) ⇐⇒ appartient

– 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

– Definition: used to add data


– Syntax: Insert INTO (tablename) <(column1 ),(column2 ),. . . ,(columnn )> VALUES (val1 ,val2 ,. . . valn );
– We can insert one or multiple rows at the same time! you must just gives their values ⇐⇒ nb( values) = nb( rows)

ˆ Update

– Definition: used to modify data


– Syntax: Update (tablename) SET (ColumnN ame1 = V alue1 ,. . . ,ColumnN amen = V aluen ) WHERE (con-
dition);
– When using the UPDATE statement, if you do not specify the WHERE clause, all the rows in the table are
updated.
1 Data Manipulation Language used to read and modify data

1
ˆ Delete

– Definition: used to remove data


– Syntax: DELETE (tablename) WHERE (condition);
– When using the DELETE statement, if you do not specify the WHERE clause, all the rows in the table are
deleted.

2 DDL2 statements
ˆ CREATE TABLE

– Definition: used to create tables in relational database


– Syntax ⇐⇒ CREATE TABLE table name
(
Column name1 Data type PrIMARY KEY not null,
Column name2 Data type Optional parameters,
..
.
Column namen Data type Optional parameters,
)

– Data types: numeric , char , varchar , integer, BIGINT . . .


– Optional parameters: Primary key , not null . . .
ˆ ALTER 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

– Definition: used to remove a table in the DataBase


– Syntax: DROP (tablename);

ˆ TRUNCATE

– Definition: used to remove the content of a table in the DataBase


– Syntax: TRUNCATE (tablename) IMMEDIATE;

2 Data definition language: Define, change or drop data

We don’t need this paragraph since we have already the data

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

You might also like