11mysqlnotespdf
11mysqlnotespdf
INTRODUCTION TO MYSQL
What is MySQL?
MySQL is an open source Relational Database Management System. MySQL is very fast reliable and
flexible Database Management System. It provides a very high performance and it is multi-threaded
and multi user Relational Database management system.
MySQL Features
1. MySQL are very fast and much reliable for any type of application.
2. MySQL is very Lightweight application.
3. MySQL command line tool is very powerful and can be used to run SQL queries against
database.
4. MySQL supports indexing and binary objects.
5. It is allow changes to structure of table while server is running.
6. MySQL has a wide user base.
7. It is a very fast thread-based memory allocation system.
10. MySQL is available as a separate program for use in a client/server network environment.
Advantages of MySQL:
Reliability and Performance: MySQL is very reliable and high performance relational database management system. It
can used to store many GB's of data into database.
Availability of Source: MySQL source code is available that's why now you can recompile the source code.
Cross-Platform support: MySQL supports more then twenty different platform including the major Linux distribution.
Mac OS X, Unix and Microsoft windows.
Large pool of Trained and Certified Developers: MySQL is very popular and it is world most popular open source
Database. So it is easy to find high quality staff around the world.
Powerful Uncomplicated software: The MySQL has most capabilities to handle most corporate database application and
used to very easy and fast
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.
Below is an example of a table called "Persons":
The table above contains three records (one for each person) and five columns (P_Id, LastName, FirstName, Address, and
City).
RDBMS
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all
modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. The
data in RDBMS is stored in database objects called tables. A table is a collection of related data
entries and it consists of columns and rows.
DML commands: A DML is a language that enables users to access on manipulates data as organized by the appropriate
data model. The query and update commands form the DML part of SQL:
DDL commands:The DDL commands, as the name suggests, allow you to perform tasks related to data definition. The
DDL part of SQL permits database tables to be created or deleted. The most important DDL statements in SQL are:
TCL commands: The TCL commands used to manage and control the transactions of data in
database. The most important TCL commands are:
ROLLBACK– it undoes all changes since the beginning of the transaction or since save point.
SAVEPOINT– it marks a point upto successfully completed transaction.
SET TRANSACTION– it establish properties for the current transaction.
ENUM(x,y,z,etc.) Let you enter a list of possible values. You can list up to
65535 values in an ENUM list. If a value is inserted that
is not in the list, a blank value will be inserted.
Note: The values are sorted in the order you enter them.
You enter the possible values in this format:
ENUM('X','Y','Z')
Number types:
Data type Description
*The integer types have an extra option called UNSIGNED. Normally, the integer goes from a 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:
Data type Description
*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.
and
SELECT * FROM table_name
Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.
We use the following SELECT statement:
SELECT LastName, FirstName FROM Persons
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table. We use the following SELECT statement:
SELECT * FROM Persons
Now we want to select only the distinct values from the column named "City" from the table above.
We use the following SELECT statement:
SELECT DISTINCT City FROM Persons
Now we want to select only the persons living in the city "Sandnes" from the table above.We use the following SELECT
statement:
SELECT * FROM Persons
WHERE City='Sandnes'
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
= Equal
IN If you know the exact value you want to return for at least one of the
columns
Now we want to select only the persons with the first name equal to "Tove" AND the last name equal to "Svendson":
We use the following SELECT statement:
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":
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Tove'
OR FirstName='Ola'
ORDER BY Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select all the persons from the table above, however, we want to sort the persons by their last name.
We use the following SELECT statement:
SELECT * FROM Persons
ORDER BY LastName
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
5 Tjessem Jakob
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should
be updated. If you omit the WHERE clause, all records will be updated!
5 Tjessem Jakob
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should
be deleted. If you omit the WHERE clause, all records will be deleted!
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
DELETE FROM Persons
WHERE LastName='Tjessem' AND FirstName='Jakob'
Note: Be very careful when deleting records. You cannot undo this statement!
Try it Yourself
Test your SQL Skills
On this page you can test your SQL skills.
We will use the Customers table in the Northwind database:
CompanyName ContactName Address City
To preserve space, the table above is a subset of the Customers table used in the example below.
To see how SQL works, you can copy the SQL statements below and paste them into the text area, or you can make your
own SQL statements.
SELECT * FROM customers
When using SQL on text data, "alfred" is greater than "a" (like in a dictionary).
SELECT CompanyName, ContactName
FROM customers
WHERE CompanyName > 'g'
AND ContactName > 'g'
SQL Constraints
Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created
(with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). We will focus on
the following constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
Address varchar(255),
City varchar(255)
)
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons" table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy link between tables.
The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key column, because it has to be
one of the values contained in the table it points to.
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL
syntax:
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT GETDATE()
)
To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type specifies what type of
data the column can hold. For a complete reference of all the data types available in MS Access, MySQL, and SQL Server,
go to our complete Data Types reference.
The "Persons" table will now like this:
P_Id LastName FirstName Address City DateOfBirth
Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a two-digit or four-digit format.