3.MySql Lab Exercise - 1
3.MySql Lab Exercise - 1
3.MySql Lab Exercise - 1
Exercise - 1
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle,
MySQL, and Microsoft Access.
A table is a collection of related data entries and it consists of columns and rows.
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
Although SQL is an ANSI (American National Standards Institute) standard, there are many different
versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands
(such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
Text types:
Note: The values are sorted in the order you enter them.
Number types:
*The integer types have an extra option called UNSIGNED. Normally, the integer goes from an negative to
positive value. Adding the UNSIGNED attribute will move that range up so it starts at zero instead of a
negative number.
Date types:
Note: Values allowed in four-digit format: 1901 to 2155. Values allowed in two-
digit format: 70 to 69, representing years from 1970 to 2069
*Even if DATETIME and TIMESTAMP return the same format, they work very differently. In an INSERT or
UPDATE query, the TIMESTAMP automatically set itself to the current date and time. TIMESTAMP also accepts
various formats, like YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD.
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or
"Orders"). Tables contain records (rows) with data.
The table above contains three records (one for each person) and five columns (P_Id, LastName, FirstName,
Address, and City).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement will select all the records in the "Persons" table:
Semicolon is the standard way to separate each SQL statement in database systems that allow more than one
SQL statement to be executed in the same call to the server.
We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL
statement, but some database programs force you to use it.
The query and update commands form the DML part of SQL:
The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies
links between tables, and imposes constraints between tables. The most important DDL statements in SQL
are:
and
and
Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
The DISTINCT keyword can be used to return only distinct (different) values.
Now we want to select only the distinct values from the column named "City" from the table above.
City
Sandnes
Stavanger
Now we want to select only the persons living in the city "Sandnes" from the table above.
This is correct:
This is wrong:
This is correct:
This is wrong:
Operator Description
= Equal
The OR operator displays a record if either the first condition or the second condition is true.
Now we want to select only the persons with the first name equal to "Tove" AND the last name equal to
"Svendson":
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola":
Now we want to select only the persons with the last name equal to "Svendson" AND the first name equal to
"Tove" OR to "Ola":
If you want to sort the records in a descending order, you can use the DESC keyword.
ORDER BY Example
The "Persons" table:
Now we want to select all the persons from the table above, however, we want to sort the persons by their
last name.