Anda di halaman 1dari 31

MySQL Introduction

Topic

About MySQL.
Introduction to MySQL.

Explanation

MySQL is the most popular open source SQL database management system
(DBMS), is developed, distributed, and supported by MySQL AB. MySQL AB is a
commercial company, founded by the MySQL developers.

MySQL is a Relational Database Management System (RDBMS) and it was originally


developed to handle large databases much faster than the exiting solutions and has
been successfully used in highly demanding production environments for several
years.

A short definition of Relational Database Management System (RDBMS) may be a


DBMS in which data is stored in the form of tables and the relationship among the data
is also stored in the form of tables.

Although under constant development, MySQL Server today offers a rich and useful
set of functions. Its connectivity, speed, and security make MySQL Server highly
suited for accessing databases on the Internet.

Now let's go step by step into MySQL from Installation to Advanced concepts. In the
next page we can see How to install MySQL.

MySQL is the most popular open source Relational database Management system
(RDBMS). Being a open source anyone can use and change the software for their
needs.

Overview of Database

Topic

What is Database?
Overview of Relational Database

Explanation

Database :

A database is a collection of data that is organized so that its contents can be easily
accessed, managed and updated. The software used to manage and query a database
is known as a Database Management System (DBMS). Then came the concept of
Relational Database Management System(RDBMS). Relational database is a
database where data are stored in more than one table, each one containing different
types of data. The different tables can be linked so that information from the separate
files can be used together. This is explained below using an example.

Example :

Consider the Student's personal information and the test marks in a school.
Suppose the student's infomation and test results are stored seperately, we can get
information regarding the student's personal information like Address from the first
file. And also a student's mark at a test can be obtained from the other file.

But consider a situation where we want to get the Address of a student as well as
his marks. These things become hard when we have a large volume of data. If we
have a studentID stored in two files then we can easily relate the details and recollect
them.

In relational databases, a table is a set of data elements(cells) that is organized,


defined and stored using a model of horizontal rows and vertical columns. A table has
a specified number of columns but can have any number of rows(i.e should have
specified structure of date but can have any no. of data). Here every column is known
as a field, every row is called as record.

MySQL

MySQL is one of the popular Relational Database Management System. Now let us
see an example for a simple database which consists of a table. Consider the same
example we took earlier, a student database. The table may have different fields such
as StudID, Name, Marks, Address, Phone. These five fields constitutes a table named
as student.StudID, Name are fileds and the particular row is a record.

StudID Name Marks Address Phone


1 steve 100 5th cross street 2456987

Now we slightly move to MySQL and see how to create database, use database and
remove database

Starting MySQL Database

Topic

How to start MySQL?


Starting MySQL.

Explanation

The following will not be needed if you have installed MySQL as a service.

Starting MySQL using command line:

Let’s see how to start MySQL from the windows command line manually.

To start the mysqld from the command line, first you should open a console
window i.e., Start -> Run.., type cmd or command to open the console window. After
opening the console window, enter the path where your MySQL is installed. For
example:

C:\> "C:Program Files\MySQL\MySQL Server 4.1\bin"

After giving the path, start the MySQL as given below:

C:\Program Files\MySQL\MySQL Server 4.1\bin> mysqld

The version depends upon the mysql server you have installed. The path may also
vary depending on the MySQL installation on your system.

You can stop the MySQL server using the below command:

C:\> "C:\Program Files\MySQL\MySQL Server 4.1\bin\mysqladmin" -u root shutdown

The above commands will help you to start and stop the MySQL server.

Connecting MySQL Database


Topic

How to connect MySQL server?


Connecting MySQL Database.

Explanation

Connecting MySQL server :

There are three ways to connect to a MySQL server. They are:

 Command Prompt
 MySQL Command Line Client
 External MySQL Tools

Command Prompt :

You can connect MySQL from your Console window i.e., Start -> Run.., type cmd or
command to open the Command prompt window.

After opening the console window, enter the path where your MySQL is installed. For
example:

C:\> "C:Program Files\MySQL\MySQL Server 4.1\bin"

After giving the path, enter the below command to connect to MySQL server:

C:\Program Files\MySQL\MySQL Server 4.1\bin> mysql.exe -u root

The path may vary depending on the MySQL installation on your system. Instead of
root you can also connect by giving your username.

MySQL Command Line Client :

To connect a MySQL server using the command line client, go to


Start -> Programs -> MySQL -> MySQL Server 4.1 -> MySQL Command Line Client.
The command line client window will be opened and enter the password to start your
queries.

External MySQL Tools :

You can also get connected to MySQL, using external tool like MySQL Query
Browser.

Create/ Show Database in MySQL

Topic

How to create/ creating a database in MySQL?

How to Show the Mysql databases?

Explanation

Before going to create a database check whether there is any database with the
name you are going to create. Check this by the following SHOW statement:

mysql> show databases;


This query will list the available databases. Please note that MySQL is case
insensitive. So you can give the query with different cases also. So show dataBASES; ,
SHOW dataBASES; will also work.

Once you have confirmed that you don't have a database with the name you
intended to create, then you can create your own database by,

mysql> create database sample;

Please note that only in Unix the database name is case sensitive. The above query
will create an empty database and it wouldn't contain any tables.

Difference between Use database and select database

Topic

Selecting or using a Database.


How to select a database in MySQL?

Explanation

If you want to create tables for a database first you have to select the database. For
selecting a database you have to enter the following query :
mysql> USE sample;
Database changed
Here sample is the database you want to select. The USE command doesn't need
a semicolon at the end of the query.

You can use the following command to view the current database that you're
connected to:

mysql> select database();


+------------+
| database() |
+------------+
| sample |
+------------+
Understand the difference between Use database and select database() as the
former is selecting a database and the later one is displaying the currently selected
one. After selecting the database you can create tables and other such operations.

Note : You have to select the database using the USE command everytime you are
entering into Mysql server or when you want to change the database.

If you type the following query you can see an information like Empty set (ie.,)
there are no tables in the selected database.

mysql> show tables;


Empty set (0.00 sec)

Remove or Delete MySQL Databases by drop command

Topic

DROP Statement.
How to remove/ delete a database in MySQL?

Explanation

Database can be removed or deleted using the DROP command. The


following example deletes the database sample.
mysql> drop database sample;
Query OK, 1 row affected (0.05 sec)

This query will delete the database sample. The query will permanently
remove the database.

DROP DATABASE drops all tables in the database and deletes the database.
Once the DROP command is used, then we cannot use that database. So, we
should be careful with this command.

MySQL Data types Introduction

Topic

Data type - Introduction

Explanation

Data types :

Definition : Data type is the characteristic of columns and variables that defines what
types of data values they can store. The characteristic indicating whether a data item
represents a number, date, character string, etc.

Data types are used to indicate the type of the field we are creating into the table.
MySQL supports a number of datatypes in three important categories:

 Numeric types
 Date and Time types
 String(Character) types

Before creating a table, identify whether a column should be a text, number, or


date type. Each column in a table is made of a data type. The size of the value should
be the smallest value depending upon the largest input value.

For example, if the number of students in a school are in hundreds set the column
as an unsigned three-digit SMALLINT(allowing for up to 999 values).

We should be concise in inserting a string of five characters long into a char(3) field,
the final two characters will be truncated. It is better to set the maximum length for
text and number columns as well as other attributes such as UNSIGNED.

Square brackets ('[' and ']') indicate optional parts of type definitions.

Now we slightly move to the overview of MySQL datatypes.

MySQL Numeric data types

Topic

What are the Numeric data types in Mysql?

Explanation

MySQL Numeric Datatypes :


The numeric data types are as follows:

 BIT  TINYINT  BOOLEAN


 SMALLINT  MEDIUMINT  INT
 INTEGER  BIGINT  FLOAT
 DOUBLE  DECIMAL

Lets see the numeric datatypes briefly.

BIT :

BIT is a synonym for TINYINT(1).

TINYINT[(M)] :

A very small integer. The signed range is -128 to 127. The unsigned range is 0 to
255.

BOOL, BOOLEAN :

These types are synonyms for TINYINT(1). A value of zero is considered false.
Non-zero values are considered true.

SMALLINT :

A small integer. The signed range is -32768 to 32767. The unsigned range is 0 to
65535.

MEDIUMINT :

A medium-sized integer. The signed range is -8388608 to 8388607. The unsigned


range is 0 to 16777215.

INT :

A normal-size integer. The signed range is -2147483648 to 2147483647. The


unsigned range is 0 to 4294967295.

INTEGER :

This type is a synonym for INT.

BIGINT :

A large integer. The signed range is -9223372036854775808 to


9223372036854775807. The unsigned range is 0 to 18446744073709551615.

FLOAT :

A small(single-precision) floating-point number. The values are from


3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to
3.402823466E+38.

DOUBLE :

A normal-size(double-precision) floating-point number. The values are from


1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and
2.2250738585072014E-308 to 1.7976931348623157E+308.

DECIMAL :

The maximum number of digits(M) for DECIMAL is 64.

MySQL Data Types: String

Topic
What are the String/Text data types?

Explanation

String data types :

 CHAR  VARCHAR  TINYTEXT


 TEXT  BLOB  MEDIUMTEXT
 LONGTEXT  BINARY  VARBINARY
 ENUM  SET
CHAR() :

It is a fixed length string and is mainly used when the data is not going to vary
much in it's length. It ranges from 0 to 255 characters long. While storing CHAR
values they are right padded with spaces to the specified length. When retrieving the
CHAR values, trailing spaces are removed.

VARCHAR() :

It is a variable length string and is mainly used when the data may vary in length.
It ranges from 0 to 255 characters long. VARCHAR values are not padded when they
are stored.

TINYTEXT, TINYBLOB :

A string with a maximum length of 255 characters.

TEXT :

TEXT columns are treated as character strings(non-binary strings). It contains a


maximum length of 65535 characters.

BLOB :

BLOB stands for Binary Large OBject. It can hold a variable amount of data. BLOB
columns are treated as byte strings(binary strings). It contains a maximum length of
65535 characters.

MEDIUMTEXT, MEDIUMBLOB :

It has a maximum length of 16777215 characters.

LONGTEXT, LONGBLOB :

It has a maximum length of 4294967295 characters.

BINARY :

The BINARY is similar to the CHAR type. It stores the value as binary byte strings
instead of non-binary character strings.

VARBINARY :

The VARBINARY is similar to the VARCHAR type. It stores the value as binary
byte strings instead of non-binary character strings.

ENUM() :

An enumeration. Each column may have one of a specified possible values. It can
store only one of the values that are declared in the specified list contained in the ( )
brackets. The ENUM list ranges up to 65535 values.

SET() :

A set. Each column may have more than one of the specified possible values. It
contains up to 64 list items and can store more than one choice. SET values are
represented internally as integers.

If CHAR and VARCHAR options are used in the same table, then MySQL will
automatically change the CHAR into VARCHAR for compatability reasons. The ( )
bracket allows to enter a maximum number of characters that will be used in the
column.

Create MySQL Database Table

Topic

How to create a Mysql Database table?

Explanation

Creating tables :

Once you have selected the database, we can start creating tables. The CREATE
statement is used to create a table in MySQL with constraint. A Constraint is restriction
to the behavior of a variable.

The Create syntax is

CREATE TABLE tableName


(
fieldName1 dataType(size) [NULL | NOT NULL]
fieldName2 dataType(size) [NULL | NOT NULL]
);

If NULL is specified, the field is allowed to be left empty. If NOT NULL is specified,
the field must be given a value. In the absence of either a NULL or NOT NULL, NULL
is assumed.

The below example query will help you in creating table:

CREATE TABLE student


(
studID INT(5),
name VARCHAR(30),
);

The above query will create the table student with fields ID and Name.

PRIMARY KEY :

A PRIMARY KEY is a field in a table that uniquely identifies a record. This attribute
is used to define the field name to create a primary key.

Example :

fieldName INT UNSIGNED AUTO_INCREMENT PRIMARY KEY

The PRIMARY KEY is specified after defining the fields in the below example:

CREATE TABLE student


(
studID INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(30),
PRIMARY KEY(studID)
);

We can also create a compound primary key. A compound primary key is where
more than one field is used to uniquely identify a record.
Let’s create a table for holding student details in a class.

mysql> create table student(studid int(10), name varchar(20), address varchar(40),


phone int(10));
Query OK, 0 rows affected (0.05 sec)

Mysql Database Table Description

Topic

How to display the structure of a table?


How to list the tables description from Mysql database?

Explanation

Desc table :

We can examine the structure of a table using the DESCRIPTION or DESC


statement. The following query describes the structure of the student table.
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
We can also use the SHOW FIELDS FROM statement to display the same
structure.

mysql> SHOW FIELDS FROM student;

Listing Tables :

We can list all the tables in the database using SHOW TABLES query. The following
query will list the tables in the current database.
mysql> show tables;
+--------------------+
| Tables_in_sample |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)

Deleting a Mysql Database Table

Topic

How to delete or drop a Mysql database table?

Explanation

Deleting tables :

The DROP statement is used to delete one or more tables completely from a
database.
The syntax is
DROP TABLE tbl_name
The following example deletes the student table.

mysql> drop table student;


Query OK, 0 rows affected (0.00 sec)

This query will permanently remove or delete the table student.

DROP TABLE query drops all fields in the table and deletes the table. Once the
DROP TABLE statement is used, we cannot use that table. So, we should be careful
with this statement.

Renaming Mysql Database Table

Topic

How to rename a Mysql database table?

Explanation

Renaming tables:

The RENAME statement is used to rename one or more tables in a database.


The syntax is
RENAME TABLE tbl_name TO new_tbl_name
[, tbl_name2 TO new_tbl_name2] ...
The following example query renames the student table as class table.

mysql> rename table student to class;


Query OK, 0 rows affected (0.00 sec)

Now we can view the table whether the name is changed by the following query.

mysql> show tables;


+--------------------+
| Tables_in_sample |
+--------------------+
| class |
+--------------------+
1 row in set (0.00 sec)
If the query renames more than one table, renaming operations are done from left
to right.

We can also swap two table names. Let us assume tmp table which does not exists.

Example :
RENAME TABLE emp1 TO tmp,
emp2 TO emp1,
tmp TO emp2;
We can also use RENAME TABLE to move a table from one database to another.

Example :

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

ALTER Mysql database Table

Topic

Altering database tables.


How to rename a Mysql database table using ALTER?
How to add a field column to a database table?

Explanation

ALTER TABLE:

ALTER TABLE is used to change the structure of an existing table. We can add or
delete columns, change the type of existing columns, or rename columns or the table
itself. We can also change the comment for the table and type of the table.

The Syntax is

ALTER TABLE tbl_name alter_specification [, alter_specification] ...

The below table will describe the alter specification :

Alter Specification Description


Rename Rename a Table name
Add Add a new column, key, index
Add First Add a column First
Add After Add a column After
Drop Drop a column, Index, key
Change Change a column name
Change Type Change a column type
Modify Modify a column type

Renaming a Table:

We can also RENAME the table using ALTER TABLE. The following example query
renames the table student to class.

mysql> ALTER TABLE student RENAME class;

The above query will change the table name.

Adding a column to a table:

The ADD COLUMN modifier is used to add a column to a table. The following
example query adds a field called marks to the student table.

mysql> ALTER TABLE student ADD COLUMN marks INT(10);


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
| marks | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
Next we move to the alterations in displaying the tables.

ALTER Mysql database Table: Insert New Field Column

Topic
Altering Mysql Database tables
How to insert a new database field column at the beginning?
How to insert a new database field column next to the specified field?

Explanation

Add a column First:

We can position the field using FIRST and AFTER modifiers. The following example
query will place the new field as the first field in the table.

mysql> ALTER TABLE student ADD COLUMN marks INT(10) FIRST;


Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| marks | int(10) | YES | | NULL | |
| studid | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
Add a column After:

We can also place the new field next to any of the field. The following example
query will place the new field immediately after the field name.

mysql> ALTER TABLE student ADD COLUMN marks INT(10) AFTER names;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| marks | int(10) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
Next we can see how to delete and change a field.

ALTER Mysql database Table: Delete Field Column

Topic

Altering Mysql database tables


How to delete an existing database field column?
How to modify or change a database field column?

Explanation

Delete a column:

The DROP COLUMN is used to delete a column from the table.


The syntax is
ALTER TABLE tbl_name DROP col_name;

The following query drops the field marks.

mysql> ALTER TABLE student DROP COLUMN marks;


Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Change a column name:

When we modify a column, we have to specify the attribute of the column again.
The following example renames the name field to stud_name in the student table.
mysql> ALTER TABLE student CHANGE name stud_name VARCHAR(20);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| stud_name | varchar(20) | YES | | NULL | |
| marks | int(10) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
Change a column name:

If we want to change the attribute alone, we can use the same column as in the
following example.

mysql> alter table student change name name varchar(40);


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(40) | YES | | NULL | |
| marks | int(10) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

ALTER Mysql Database Table: Modify Field Column Type

Topic

Altering Mysql database tables


How to modify database field column type?

Explanation
Modify a column type:

The modify statement is also used to change the column type in a table, as the
previous example. The below query will modify the column type.

mysql> alter table student modify name varchar(40);


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(40) | YES | | NULL | |
| marks | int(10) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Inserting Row into a Mysql Database Table

Topic

How to insert data or add a new row into a database table?


How to use INSERT, SET queries to add a new row in a Mysql Database?

Explanation
INSERT STATEMENT:

INSERT query is used for inserting new rows or data into an existing table.
The Insert syntax is

INSERT INTO tbl_name VALUES[(col_name,...)];

If the datatype is not mentioned it will consider NULL values.

The following example query will add the values like studid, name, marks, address
and Phone number into the table student.

mysql> insert into student values(1, "steve", 100, "5th cross street", 2456987);
Query OK, 1 row affected (0.01 sec)

INSERT...SET STATEMENT :

The INSERT...SET is also used to insert values the using the column name.
The syntax is

INSERT INTO tbl_name SET col_name = expr,.....;

Lets take the same values inserting into the table student.

mysql> insert into student set studid=1, name='steve', marks=100, address='5th


cross street', phone=2456987;
Query OK, 1 row affected (0.03 sec)

Inserting Multiple rows into a Mysql Database


Topic

How to insert multiple rows into a database table?

Explanation
INSERT STATEMENT FOR MULTIPLE ROWS:

We can insert multiple rows into a table using a single INSERT statement.

The Syntax is

INSERT INTO tbl_name(col_name1, col_name2,...) VALUES(expr1, expr2,.....;),


(expr1a, expr2a,.....;)

Example :

mysql> insert into student(studid,name,marks,address,phone)


values(3,'michael',75,'edinburgh',2598234),
(4,'jack',82,'victoria street',2436821),
(5,'anne',100,'downing street',2634821);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

The above query will insert the three students details into the table student. This
query will be useful while inserting large amount data into a specific table.

Insert Auto Increment Field into a Database Table

Topic

How to add an autoincrement field with MySQL?


Auto Increment using Insert statement

Explanation
Auto Increment:

The auto increment attribute is used to generate a unique identity for the inserting
rows. Let’s see an example using auto increment.

mysql> create table stud(id bigint not null unique auto_increment,


name char(20));
Query OK, 0 rows affected (0.03 sec)
In the above query, we have created a table stud and we have assigned
autoincrement to the field name id. Now we can insert the values for the field name
alone as given in the below query.

mysql> insert into stud(name) values ('anne'),('michael'),('james'),


('rajesh'),('harry');
Query OK, 5 rows affected (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 0

Now if we select the table, the field name id will be automatically incremented as
shown below.
mysql> select * from stud;
+----+---------+
| id | name |
+----+---------+
| 1 | anne |
| 2 | michael |
| 3 | james |
| 4 | rajesh |
| 5 | harry |
+----+---------+
5 rows in set (0.00 sec)

Mysql Database Rows Counting

Topic

How to count the number of rows using select query in the database table?

Explanation

Counting Rows:

COUNT(*) counts the number of rows in a table.

The syntax is

SELECT COUNT(*) from tbl_name;

Example :
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
The above query will list the number of rows in the student table.

Mysql Database: SELECT Query

Topic

How to retrieve data from the database table?


How to select a column from the database table?

Explanation

After inserting datas into the table, we probably want to check the datas are stored
correctly. To do so, we use the SELECT query.

The Select syntax is

SELECT what_to_select from tbl_name;

To view all the data from the table, we use the below query.

mysql> select * from student;


+--------+---------+-------+------------------+---------+
| studid | name | marks | address | phone |
+--------+---------+-------+------------------+---------+
| 1 | steve | 100 | 5th cross street | 2456987 |
| 2 | david | 98 | welling street | 547896 |
| 3 | michael | 75 | edinburgh | 2598234 |
| 4 | jack | 82 | victoria street | 2436821 |
| 5 | anne | 100 | downing street | 2634821 |
| 6 | steve | 75 | downing street | 2874698 |
| 7 | anne | 80 | edinburgh | 2569843 |
| 8 | mille | 98 | victoria street | 1236547 |
+--------+---------+-------+------------------+---------+
8 rows in set (0.00 sec)

The above example query will list the complete details of the student table. Here *
will select all the columns from the table.

Mysql Database LIMIT Clause

Topic

How to retrieve data using limit from the database table?

Explanation

LIMIT:

The LIMIT clause can be used to constrain the number of rows returned by the
SELECT statement. It takes one or two numeric arguments, which must both be non-
negative integer constants.

Let’s see an example query for SELECT LIMIT statement.

mysql> select * from student limit 2,5;


+--------+---------+-------+-----------------+---------+
| studid | name | marks | address | phone |
+--------+---------+-------+-----------------+---------+
| 3 | michael | 75 | edinburgh | 2598234 |
| 4 | jack | 82 | victoria street | 2436821 |
| 5 | anne | 100 | downing street | 2634821 |
| 6 | steve | 75 | downing street | 2874698 |
| 7 | anne | 80 | edinburgh | 2569843 |
+--------+---------+-------+-----------------+---------+
5 rows in set (0.00 sec)
Here the first argument of th query specifies the offset of the first row to return,
and the second specifies the maximum number of rows to return. Therefore it
retrieves the rows from 3 - 7 from the student table.

We can also return the values from the beginning of the result set by specifying the
number of rows in a single argument as follows.

mysql> select * from student limit 5;


+--------+---------+-------+------------------+---------+
| studid | name | marks | address | phone |
+--------+---------+-------+------------------+---------+
| 1 | steve | 100 | 5th cross street | 2456987 |
| 2 | david | 98 | welling street | 547896 |
| 3 | michael | 75 | edinburgh | 2598234 |
| 4 | jack | 82 | victoria street | 2436821 |
| 5 | anne | 100 | downing street | 2634821 |
+--------+---------+-------+------------------+---------+
5 rows in set (0.08 sec)
The above query will retrieve the first five rows of the student table.

Select columns from a Mysql Database Table

Topic

How to select a particular column from a Mysql database?


How to select multiple columns from a Mysql database?
Explanation

We can select a particular column to display, regretting the entire rows. Suppose
you want to see the name of the students alone we can use the below query.

mysql> select name from student;


+---------+
| name |
+---------+
| steve |
| david |
| michael |
| jack |
| anne |
| steve |
| anne |
| mille |
+---------+
8 rows in set (0.27 sec)
We can also select multiple columns, separated by commas as given in the below
query.
mysql> select name, marks from student;
+---------+-------+
| name | marks |
+---------+-------+
| steve | 100 |
| david | 98 |
| michael | 75 |
| jack | 82 |
| anne | 100 |
| steve | 75 |
| anne | 80 |
| mille | 98 |
+---------+-------+
8 rows in set (0.03 sec)
In the above query, we have selected both name and marks from the table student.

Sorting of Database Rows using 'order by' clause

Topic

How to select the database rows in an ordered manner?


What is the use of sorting of rows?
How to use Order By Clause?

Explanation

In the preceding examples, the query for selected rows are displayed in no
particular order. We can also select the rows to display in an ordered format using
ORDER BY Clause.

The following example query will sort the rows in an ascending order based on the
marks.
mysql> select name, marks from student order by marks;
+---------+-------+
| name | marks |
+---------+-------+
| michael | 75 |
| steve | 75 |
| anne | 80 |
| jack | 82 |
| david | 98 |
| mille | 98 |
| steve | 100 |
| anne | 100 |
+---------+-------+
8 rows in set (0.03 sec)
We can also sort the orders in descending order. In the below example query the
marks are sorted in descending order.

mysql> select name, marks from student order by marks desc;


+---------+-------+
| name | marks |
+---------+-------+
| steve | 100 |
| anne | 100 |
| david | 98 |
| mille | 98 |
| jack | 82 |
| anne | 80 |
| michael | 75 |
| steve | 75 |
+---------+-------+
8 rows in set (0.00 sec)
Next we can see how to sort the multiple columns.

Sort of multiple rows using 'order by' Clause

Topic

How to select the multiple rows and columns in an ordered manner?


How to sort multiple rows and columns?

Explanation

We can also sort multiple columns in different directions as given in the below
query.

mysql> select name, marks, address from student order by name,


marks desc;
+---------+-------+------------------+
| name | marks | address |
+---------+-------+------------------+
| anne | 100 | downing street |
| anne | 80 | edinburgh |
| david | 98 | welling street |
| jack | 82 | victoria street |
| michael | 75 | edinburgh |
| mille | 98 | victoria street |
| steve | 100 | 5th cross street |
| steve | 75 | downing street |
+---------+-------+------------------+
8 rows in set (0.00 sec)
Here we have selected three columns name, marks and address. In this query we
have sorted the column name alone in ascending order and we have additionally
mentioned marks in descending order.

So if there are same names, the highest mark will be taken as the first priority. In
the above example query, there are 2 anne, so the anne with highest mark will be
displayed first.

Mysql Database Pattern Matching

Topic

Mysql database Pattern Matching


How to select columns which has a pattern?
How to use LIKE or NOT LIKE operator?
Explanation

Sometimes we may need to look for the table with a certain matching character. In
MySQL we use LIKE or NOT LIKE operator for comparison. In MySQL the patterns are
case-insensitive by default.

Let us consider an example query to display the student names starting with the
letter M.

mysql> select * from student where name like 'm%';


+--------+---------+-------+-----------------+---------+
| studid | name | marks | address | phone |
+--------+---------+-------+-----------------+---------+
| 3 | michael | 75 | edinburgh | 2598234 |
| 8 | mille | 98 | victoria street | 1236547 |
+--------+---------+-------+-----------------+---------+
2 rows in set (0.01 sec)
In the above example query, it will list all the names that starts with the letter M
from the table student.

The following example query will list the names that ends with letter e.

mysql> select * from student where name like '%e';


+--------+-------+-------+------------------+---------+
| studid | name | marks | address | phone |
+--------+-------+-------+------------------+---------+
| 1 | steve | 100 | 5th cross street | 2456987 |
| 5 | anne | 100 | downing street | 2634821 |
| 6 | steve | 75 | downing street | 2874698 |
| 7 | anne | 80 | edinburgh | 2569843 |
| 8 | mille | 98 | victoria street | 1236547 |
+--------+-------+-------+------------------+---------+
5 rows in set (0.00 sec)
We can also list the names that contains a specific letter anywhere. The following
example query will list the names that contains "a".

mysql> select * from student where name like '%a%';


+--------+---------+-------+-----------------+---------+
| studid | name | marks | address | phone |
+--------+---------+-------+-----------------+---------+
| 2 | david | 98 | welling street | 547896 |
| 3 | michael | 75 | edinburgh | 2598234 |
| 4 | jack | 82 | victoria street | 2436821 |
| 5 | anne | 100 | downing street | 2634821 |
| 7 | anne | 80 | edinburgh | 2569843 |
+--------+---------+-------+-----------------+---------+
5 rows in set (0.00 sec)
Suppose if we want to find the names that contain exactly five characters, we use a
special character "_"(underscore). The following query will list all the five letter names
from the table student.

mysql> select * from student where name like '_____';


+--------+-------+-------+------------------+---------+
| studid | name | marks | address | phone |
+--------+-------+-------+------------------+---------+
| 1 | steve | 100 | 5th cross street | 2456987 |
| 2 | david | 98 | welling street | 547896 |
| 6 | steve | 75 | downing street | 2874698 |
| 8 | mille | 98 | victoria street | 1236547 |
+--------+-------+-------+------------------+---------+
4 rows in set (0.00 sec)

Sorting of Database Datas by Group by Clause

Topic
Query using "Group by" query
How to select the columns using Groupby clause?

Explanation

Group By:

The Group by clause is used to display the rows and columns grouped by selective
columns. It can be used to perform the aggregate functions, such as count().

The following example query will list the name of the student and also count the
repeative names using Group By clause in the select statement.

mysql> select name, count(name) from student group by name;


+-------+-------------+
| name | count(name) |
+-------+-------------+
| anne | 2 |
| david | 1 |
| jack | 1 |
| mille | 1 |
| steve | 2 |
+-------+-------------+
5 rows in set (0.03 sec)
The below query will display the name and sum of marks of the student using
groupby clause.
mysql> select name,sum(marks),count(*) from students group by name;
+----------+------------+----------+
| name | sum(marks) | count(*) |
+----------+------------+----------+
| anne | 175 | 2 |
| maichael | 82 | 1 |
| mike | 182 | 2 |
| rock | 100 | 1 |
| steve | 175 | 2 |
+----------+------------+----------+
5 rows in set (0.00 sec)

Mysql UPDATE Query

Topic

How to use UPDATE query?


How to modify or change an existing database column or row?
Update Data in MySQL

Explanation

UPDATE Statement:

The UPDATE query is used to change or modify the existing values in a table.

The Update Syntax is

UPDATE tbl_name SET


col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_condition];
The UPDATE query updates the columns of existing rows in a table with the new
values. The SET clause is used to indicate which columns to be modified. The WHERE
clause is used to specify the conditions that identify which rows to be updated.
The following example will set the address of the student to a new address.

mysql> update student set address='welling street' where


address='victoria street';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
But this will set all the address of the students who ever lives in victoria street will
be changed to welling street.

Suppose if we want to set the address of a single student to a new address then we
can choose the below option.

mysql> update student set address='welling street' where name='jack';


Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
If we want to change a students mark we can use the below statement.

mysql> update student set marks=100 where name='david';


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
This can also be rewritten as the following.

mysql> update student set marks=marks+2 where name='david';


Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
In UPDATE statement we can also use the arithmetic operations

DELETE or Deleting Statement in Mysql

Topic

How to remove or delete a record from the database table?


How to delete all the records in a table?
Delete Query in MySQL
How to delete rows and columns of database table?

Explanation

DELETE Statement:

The deleting query is used to delete the values from a table.

The syntax is

DELETE FROM tbl_name


[WHERE where_condition];
The DELETE statement deletes the rows from tbl_name and returns the number of rows
deleted. The WHERE clause is used to specify the conditions that identify which rows to be
deleted. If the DELETE statement contains without WHERE clause, all rows will be deleted.

Now lets see an example for DELETE statement.

mysql> delete from student where name='michael';


Query OK, 1 row affected (0.00 sec)
The above example will delete the record of the student Michael from the table.

We can also delete all the values in the table as the following query.

mysql> delete from student;


Query OK, 8 rows affected (0.00 sec)
The above example will delete all the records from the student table.
Overview of Mysql Database Operators

Topic

Operators in MySQL.
Operator Precedence in MySQL.

Explanation

Operator Precedence:

Operators are used to operate with two operands. Wide collection of Operators are
available in MySQL. The operator precedences are shown below in the table.

Highest Precedence :=
1 ||, OR, XOR
2 &&, AND
3 BETWEEN, CASE, WHEN, THEN, ELSE
4 =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
5 |
6 &
7 <<, >>
8 -, +
9 *, /, DIV, %, MOD
10 ^
11 - (unary minus), ~ (unary bit inversion)
12 !, NOT
Lowest BINARY, COLLATE

If a statement contains paranthesis, then the operations inside the paranthesis are
performed first. These operators will be explained in the following sections.

Type Conversion in Expressions

Topic

What is meant by Type Conversion?


How type conversion and casting takes place?
How to convert string to integer and integer to string?

Explanation

Type Conversion:

Type conversion takes place when operators are used with different types of
operands in an expression. Some conversions are done implicitly and some need
explicit conversions.

In MySQL the numbers are converted to strings and sometimes strings to numbers
depending upon the condition.

Let us consider an example for converting a string to an integer.

mysql> Select 1+'11';


--> 12
Here the string '11' is converted to a number and the result of the expression is also
a number.
Lets see another example for converting an integer to a string.
mysql> select concat(1, ' HIOX');
--> '1 HIOX'
We can convert or casting a number to a string explicitly. Here we use CAST() or
CONCAT() function.

mysql> select 12, cast(12 as char);


--> 12, '12'
mysql> select 12, concat(12);
--> 12, '12'

Mysql Database Logical Operations

Topic

How to select data from MySQL using Logical Operators?


How to use logical operator in MySQL?
What are the logical operations?

Explanation

Logical Operator :

MySQL supports the following logical operations :

 AND(&&) Operator
 OR(||) Operator
 NOT(!) Operator

AND(&&) Operator :

The logical AND(&&) operator indicates whether the both operands are true. Lets
see a statement using AND operator.

mysql> select studid, name from student where marks > 80


and marks < 100;
(or)
mysql> select studid, name from student where marks > 80
&& marks < 100;
+--------+-------+
| studid | name |
+--------+-------+
| 4 | jack |
| 8 | mille |
+--------+-------+
2 rows in set (0.00 sec)
In the above example it will list the studid and name of the student who have
secured more than 80 and less than 100.

OR(||) Operator :

The logical OR(||) operator indicates whether either operand is true. Lets see a
statement using OR operator.

mysql> select name, marks, address from student where


name like 'a%' or name like 's%';
(or)
mysql> select name, marks, address from student where
name like 'a%' || name like 's%';
+-------+-------+------------------+
| name | marks | address |
+-------+-------+------------------+
| steve | 100 | 5th cross street |
| anne | 100 | downing street |
| steve | 75 | downing street |
| anne | 80 | edinburgh |
+-------+-------+------------------+
4 rows in set (0.00 sec)
In the above statement it will list the name, marks and address of the student
whose name starts with the letter A and S.

NOT(!) Operator :

The logical NOT(!) operator have only one operand and it returns the inverse of the
value.

mysql> select * from student where not (studid=1);


(or)
mysql> select * from student where ! (studid=1);
+--------+-------+-------+-----------------+---------+
| studid | name | marks | address | phone |
+--------+-------+-------+-----------------+---------+
| 2 | david | 100 | welling street | 547896 |
| 4 | jack | 82 | welling street | 2436821 |
| 5 | anne | 100 | downing street | 2634821 |
| 6 | steve | 75 | downing street | 2874698 |
| 7 | anne | 80 | edinburgh | 2569843 |
| 8 | mille | 98 | victoria street | 1236547 |
+--------+-------+-------+-----------------+---------+
6 rows in set (0.00 sec)
It will list all the student details except the studid 1.

Comparison Operators

Topic

How to select data from MySQL using Comparison Operators?


How to use Comparison operations in MySQL?

Explanation

Comparison Operator :

Comparison operator is used to compare expressions or values. The result of the


comparison will be either True(1) or False(0). MySQL supports the following
comparison operators :

 EQUAL(=)  LESS THAN(<)


 LESS THAN OR EQUAL(<=)  GREATER THAN(>)
 GREATER THAN OR EQUAL(>=)  NOT EQUAL(<>,!=)
 BETWEEN  GREATEST

=:

Equal.

mysql> select 1 = 0;
--> 0

mysql> select 0.0 = 0;


--> 1
<:

Less than.
mysql> select 4.5 < 5;
--> 1

mysql> select 1.1 < 1;


--> 0
<= :

Less than or equal.


mysql> select 2.2 <= 2.2;
--> 1

mysql> select 2.2 <= 2.1;


--> 0
>:

Greater than.
mysql> select 7 > 2;
--> 1

mysql> select 4 > 4.1;


--> 0
>= :

Greater than or equal.


mysql> select 10 >= 10;
--> 1

mysql> select 4.4 >= 4.5;


--> 0
<>, != :

Not equal.
mysql> select 8 <> 8;
--> 0

mysql> select 7 != 7.7;


--> 1
expr BETWEEN min AND max :

If expr is greater than or equal to min and expr is less than or equal to max,
BETWEEN returns 1, otherwise it returns 0.
mysql> select 5 between 5 and 6;
--> 1

mysql> select 'N' between 'M' and 'O';


--> 1
This is same for the expr NOT BETWEEN min AND max, but Not.

GREATEST(value1,value2,...) :

This operator returns the largest argument, compared with two or more arguments.
mysql> select greatest('N', 'M', 'O');
--> O

mysql> select greatest(1, 2);


--> 2
The same rule is applied in finding the LEAST().

Mysql Numeric Mathematical functions

Topic

Numeric functions in MySQL.


What are the Arithmetic Operators?

Explanation

Numeric Functions :
Numeric function consists of two main sections. They are :

 Arithmetic Operations
 Mathematical Functions

Now, lets first discuss about Arithmetic operations.

Arithmetic Operations :

In MySQL, we have the usual Arithmetic operations. Lets see the arithmetic
operators one by one with an example.

Addition (+) :
mysql> select 5+5;
--> 10
Subtraction (-) :
mysql> select 25-18;
--> 7
Multiplication (*) :
mysql> select 4*4;
--> 16
Division (/) :
mysql> select 5/3;
--> 1.67
Next lets move to the Mathematical functions.

Mysql Numeric Math functions

Topic

Numeric functions in MySQL.


What are the Math Functions in MySQL?

Explanation

Mathematical Functions :

 ABS  ACOS  ASIN  ATAN  CEIL


 COS  COT  DEGREES  EXP  FLOOR
 FORMAT  LN  LOG  LOG(B,X)  LOG2
 LOG10  MOD  PI  POWER  RADIANS
 RAND  ROUND  SIGN  SIN  SQRT
 TAN  TRUNCATE

Now lets see some Mathematical functions with an example.

ABS(X) :

This function returns the Absolute value of the given value.

mysql> select abs(5);


--> 5

mysql> select abs(-25);


--> 25
ACOS(X) :

Returns the arc Cosine value of the given number.


mysql> select acos(0);
--> 1.5707963267949

mysql> select acos(1);


--> 0
ASIN(X) :
Returns the arc Sine value of the given number.
mysql> select asin(1);
--> 1.5707963267949

mysql> select asin(0);


--> 0
ATAN(X) :

Returns the arc Tangent value of the given number.


mysql> select atan(1);
--> 0.78539816339745

mysql> select atan(2);


--> 1.1071487177941
CEIL(X) or CEILING(X) :

Returns the smallest integer nearest to the given value but not less than that.
mysql> select ceil(1.14);
--> 2

mysql> select ceiling(-1.14);


--> -1
COS(X) :

Returns the Cosine of X, where X is given in radians.


mysql> select cos(pi());
--> -1

mysql> select cos(0);


--> 1
COT(X) :

Returns the cotangent value of X.


mysql> select cot(1);
--> 0.64209261593433

mysql> select cot(45);


--> 0.61736962378356

String Function in MySql

Topic

What is meant by Functions?


What are the available string functions?

Explanation

String Function :

Functions are predefined set of instructions that returns a value. Functions which
involves strings are called as String functions. There are different types of functions
availble in MySQL.

The important string functions are,

 CHAR_LENGTH  CONCAT  CONCAT_WS  FORMAT


 LCASE  LENGTH  LOCATE  REPEAT
 REPLACE  REVERSE  SUBSTRING

CHAR_LENGTH(str) or CHARACTER_LENGTH(str) :

This string function returns the length of the string.


mysql> select char_length("hioxindia");
--> 9

mysql> select character_length("easycalculation");


--> 15
CONCAT(str1,str2,...) :

Returns the concatenated string of the given arguments.


mysql> select concat('hiox','india');
--> 'hioxindia'
CONCAT_WS() :

It stands for Concatenate With Separator and is a special form of CONCAT function.
Returns the concatenated string of the given arguments seperated by given seperator.
mysql> SELECT CONCAT_WS('!','One','Two','Three');
--> 'One!Two!Three'
( Here '!' is the seperator)
FORMAT() :

Formats the given no and rounds to the given digits after decimal point.
mysql> SELECT FORMAT(12332.123456, 4);
--> '12,332.1235'
LCASE(str) or LOWER() :

Returns the lowercase of the given string.


mysql> select lcase('HIOX');
--> 'hiox'

mysql> select lower('EASYCALCULATION');


--> easycalculation

Like wise UPPER or UCASE returns the uppercase of the given string.
LENGTH(str) :

Returns the length of the given string in bytes. If there is a 2 byte character the
length is calculated as 2. Whereas the CHAR_LENGTH calculates only the character
length.
mysql> select length('HIOXINDIA');
--> 9
LOCATE(substr,str) or POSITION(substr IN Str) :

Returns the position of the first occurance of the substring in the string.
mysql> select locate('ind','hioxindia');
--> 5

mysql> select position('cul' in 'easycalculation');


--> 8
REPEAT(str,count) :

The given string is repeated for the given count.


mysql> select repeat('HIOX',2);
--> 'HIOXHIOX'
REPLACE(str,from_str,to_str) :

In the given string 'str' the 'from_str' is replaced by the 'to_str' string.
mysql> select replace('MyMYSql','My','you');
--> youMYSql
The given 'from_str' is case sensitive. Here in the above example the first 'My' is
changed but not the second('MY').

REVERSE(str) :

The given string is reversed and returned.


mysql> select reverse('HIOX');
--> 'XOIH'
SUBSTRING(str,pos) :

The function returns a substring from the string 'str' starting at position 'pos'.
mysql> select substring('EASYCALCULATION', 5);
--> 'CALCULATION'
Date and Time Functions in MySQL

Topic

Date and Time Functions in MySQL.


What are the Date Time Functions?

Explanation

Date and Time Functions :

This function is used to manipulate the display format of a date and time. Lets see some
basic functions for date and time.

 CURDATE  CURTIME  DATEDIFF  DATE_ADD


 DAYNAME  DAYOFMONTH  DAYOFWEEK  DAYOFYEAR
 HOUR  MINUTE  MONTH  MONTHNAME
 NOW

CURDATE() :

This date function returns the current date in the format 'YYYY-MM-DD' or 'YYYYMMDD'.

mysql> select curdate();


--> 2007-01-03
CURTIME() :

Returns the current time in the format 'HH:MM:SS' or 'HHMMSS'.


mysql> select curtime();
--> 17:33:07
DATEDIFF(expression1,expression2) :

expression1 and expression2 are date or date-and-time expressions. This function


returns expression1 � expression2 expressed as a value in days from one date to the
other. Here only the date parts will be considered for calculation.
mysql> select datediff('2007-2-6 17:33:25','2007-1-1');
--> 36
DATE_ADD(datetime, INTERVAL expression datetimetype) :

This date function adds the expression to the datetime supplied.


mysql> select date_add('2007-1-14', interval 15 day);
--> 2007-01-29
This function is same for DATE_SUB, but subtracting will take place instead of adding.

DAYNAME(date) :

Returns the name of the day for the specified date.


mysql> select dayname('2007-01-04');
--> Thursday
DAYOFMONTH(date) or DAY(date) :

Returns the date for the day of the month in the range of 1 to 31. DAY() is a synonym
for DAYOFMONTH().
mysql> select dayofmonth('2007-01-04');
--> 4
DAYOFWEEK(date) :

Returns the day of the week in the numeric format as 1 for Sunday to 7 for Saturday.
mysql> select dayofweek('2007-01-04');
--> 5
DAYOFYEAR(date) :

Returns the day of the year for given date in the numeric format, in the range 1 to 366.
mysql> select dayofyear('2007-07-09');
--> 190
HOUR(time) :

Returns the hour of the specified time in the numeric format from 0 to 23.
mysql> select hour('14:46:12');
--> 14
MINUTE(time) :

Returns the minute of the specified time in the numeric format from 0 to 59.
mysql> select minute('14:46:12');
--> 46
MONTH(date) :

Returns the month for the given date in the numeric format, in the range 0 to 12.
mysql> select month('2007-07-09');
--> 7
MONTHNAME(date) :

Returns the name of the month for the specified date.


mysql> select monthname('2007-07-09');
--> July
NOW() :

This date time function returns the current date and time in the format 'YYYY-MM-DD
HH:MM:SS' or YYYYMMDDHHMMSS.
mysql> select now();
--> 2007-01-04 14:56:15

Anda mungkin juga menyukai