Basic grammar related to mysql database
Disclaimer: Do not use the relevant technologies in this article to engage in illegal testing. Any direct or indirect consequences and losses caused by the dissemination and use of the information or tools provided in this article shall be borne by the user himself. Adverse consequences have nothing to do with the article author. This article is for educational purposes only.
It is only used to master the basic use of network security SQL injection, and the detailed version and advanced version are additional guidance
1. Create a database:
CREATE DATABASE database_name;
Second, display all databases
SHOW DATABASES;
3. Delete the database
DROP DATABASE database_name;
Fourth, use the database
USE database_name;
5. Create a table
To create a new table in the database, a MySQL CREATE TABLE
statement can be used. CREATE TABLE
statement is one of the most complex statements in MySQL.
CREATE TABLE
The following illustrates the syntax of the statement in simple form :
CREATE TABLE [IF NOT EXISTS] table_name(
column_list
) engine=table_type;
Let's look at its syntax in more detail:
- First, specify
CREATE TABLE
the name of the table to be created after the clause. Table names must be unique within the database.IF NOT EXISTS
is an optional part of the statement that allows you to check whether the table being created already exists in the database. If this is the case, MySQL will ignore the entire statement and will not create any new tables. It is strongly recommended to use it in everyCREATE TABLE
statementIF NOT EXISTS
to prevent errors from creating new tables that already exist. - Next,
column_list
specify the list of tables in the section. Columns of fields are,
separated by commas ( ). We'll show you how to define columns (fields) in more detail in the next section. - Third, you need to specify the storage engine
engine
for the tables in the clause . Any storage engine can be used such as: InnoDB , MyISAM , HEAP , EXAMPLE , CSV , ARCHIVE , MERGE , FEDERATED or NDBCLUSTER . If you don't explicitly declare a storage engine, MySQL will use InnoDB by default .
Note: InnoDB has become the default storage engine since MySQL 5.5 . The InnoDB table type brings many benefits of relational database management systems such as ACID transactions, referential integrity, and crash recovery. In previous versions, MySQL used MyISAM as the default storage engine.
To CREATE TABLE
define columns for a table in a statement, use the following syntax:
column_name data_type[size] [NOT NULL|NULL] [DEFAULT value]
[AUTO_INCREMENT]
SQL
The most important components of the above syntax are:
column_name
Specifies the name of the column. Each column has a specific data type and size, for example:VARCHAR(255)
.NOT NULL
orNULL
indicates whether the column acceptsNULL
a value.DEFAULT
Value is used to specify the default value for the column.AUTO_INCREMENT
Indicates that the column's value is automatically incremented whenever a new row is inserted into the table. Each table has one and only oneAUTO_INCREMENT
column.
If you want to set a specific column of a table as the primary key, then use the following syntax:
PRIMARY KEY (col1,col2,...)
SQL
MySQL CREATE TABLE statement example
tasks
Now let's practice an example by creating a new table named as follows in the sample database (testdb) :
CREATE TABLE
This table can be created using the statement tasks
as follows:
CREATE TABLE IF NOT EXISTS tasks (
task_id INT(11) NOT NULL AUTO_INCREMENT,
subject VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
) ENGINE=InnoDB;
SQL
In this tutorial, you have learned how to use MySQL CREATE TABLE
statements to create new tables in the database.
CREATE TABLE table_name (column_name1 data_type1, column_name2 data_type2, …);
6. Delete the specified table of the specified database
USE your_database_name;
DROP TABLE users; delete the specified table in the specified database
7. Add data:
INSERT INTO users (id, username, password) VALUES (1, ‘John Doe’, ‘[email protected]’);
CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50),age INT,email VARCHAR(100));
ALTER TABLE users ADD COLUMN address VARCHAR(100);
8. Query data:
SELECT DATABASE(); query the current database
Introduction to MySQL SELECT statement
Use SELECT
statements to get data from a table or view . A table consists of rows and columns, like a spreadsheet. Often, we only want to see a subset of rows, a subset of columns, or a combination of both. SELECT
The result of a statement is called the result set, which is a list of rows, each row consisting of the same number of columns.
See the structure of yiibaidb
the following tables in the sample database ( ) employees
. It has 8
columns: number of employees, last name, first name, extension, email, office code, report, position, etc.
SELECT
The statement controls which columns and rows to look at. For example, if you are only interested in the first name, last name, and title of all employees, or if you only want to see information for each employee whose title is a sales representative, statements can help you do this SELECT
.
Let's look at SELECT
the syntax of the statement:
SELECT
column_1, column_2, ...
FROM
table_1
[INNER | LEFT |RIGHT] JOIN table_2 ON conditions
WHERE
conditions
GROUP BY column_1
HAVING group_conditions
ORDER BY column_1
LIMIT offset, length;
SQL
SELECT
A statement consists of several clauses as described in the following list:
SELECT
This is followed by a list of comma-separated columns or an asterisk (*
), indicating that all columns are to be returned.FROM
Specifies the table or view for which to query data.JOIN
Get data from other tables based on some join conditions.WHERE
Filter rows in the result set.GROUP BY
Combines a set of rows into subgroups and applies an aggregate function to each subgroup.HAVING
Filters are based onGROUP BY
small groupings defined by clauses.ORDER BY
Specifies a list of columns to use for sorting.LIMIT
Limit the number of returned rows.
SELECT
The sum statement in the statement FROM
is required, and the other parts are optional.
You will learn about each clause in more detail in subsequent tutorials. In this tutorial, we will focus on
SELECT
the simple form usage of the statement.
MySQL SELECT statement example
SELECT
statement allows to SELECT
query part of the data of a table by specifying a list of comma-separated columns in the clause. For example, if you want to see only the employee's first name, last name, and title, use the following query:
SELECT
lastname, firstname, jobtitle
FROM
employees;
SQL
Even though there are many columns in the employee table, SELECT
the statement returns only three columns of data for all rows in the table, as shown in the following figure:
mysql> SELECT lastname, firstname, jobtitle FROM employees;
+-----------+-----------+----------------------+
| lastname | firstname | jobtitle |
+-----------+-----------+----------------------+
| Murphy | Diane | President |
| Patterson | Mary | VP Sales |
| Firrelli | Jeff | VP Marketing |
| Patterson | William | Sales Manager (APAC) |
| Bondur | Gerard | Sale Manager (EMEA) |
| Bow | Anthony | Sales Manager (NA) |
| Jennings | Leslie | Sales Rep |
| Thompson | Leslie | Sales Rep |
| Firrelli | Julie | Sales Rep |
| Patterson | Steve | Sales Rep |
| Tseng | Foon Yue | Sales Rep |
| Vanauf | George | Sales Rep |
| Bondur | Loui | Sales Rep |
| Hernandez | Gerard | Sales Rep |
| Castillo | Pamela | Sales Rep |
| Bott | Larry | Sales Rep |
| Jones | Barry | Sales Rep |
| Fixter | Andy | Sales Rep |
| Marsh | Peter | Sales Rep |
| King | Tom | Sales Rep |
| Nishi | Mami | Sales Rep |
| Kato | Yoshimi | Sales Rep |
| Gerard | Martin | Sales Rep |
+-----------+-----------+----------------------+
23 rows in set
Shell
Note the difference in comparing the columns returned by the following two statements −
statement-1
SELECT lastname, firstname, jobtitle FROM employees;
SQL
statement-2
SELECT * FROM employees;
SQL
If you want to get employees
the data of all columns in the table, you can list SELECT
all the column names in the clause, or just use an asterisk ( *
) to indicate that you want to get data from all the columns of the table, query as follows:
mysql> SELECT * FROM employees;
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
| employeeNumber | lastName | firstName | extension | email | officeCode | reportsTo | jobTitle |
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
| 1002 | Murphy | Diane | x5800 | [email protected] | 1 | NULL | President |
| 1056 | Patterson | Mary | x4611 | [email protected] | 1 | 1002 | VP Sales |
| 1076 | Firrelli | Jeff | x9273 | [email protected] | 1 | 1002 | VP Marketing |
| 1088 | Patterson | William | x4871 | [email protected] | 6 | 1056 | Sales Manager (APAC) |
| 1102 | Bondur | Gerard | x5408 | [email protected] | 4 | 1056 | Sale Manager (EMEA) |
| 1143 | Bow | Anthony | x5428 | [email protected] | 1 | 1056 | Sales Manager (NA) |
| 1165 | Jennings | Leslie | x3291 | [email protected] | 1 | 1143 | Sales Rep |
| 1166 | Thompson | Leslie | x4065 | [email protected] | 1 | 1143 | Sales Rep |
| 1188 | Firrelli | Julie | x2173 | [email protected] | 2 | 1143 | Sales Rep |
| 1216 | Patterson | Steve | x4334 | [email protected] | 2 | 1143 | Sales Rep |
| 1286 | Tseng | Foon Yue | x2248 | [email protected] | 3 | 1143 | Sales Rep |
| 1323 | Vanauf | George | x4102 | [email protected] | 3 | 1143 | Sales Rep |
| 1337 | Bondur | Loui | x6493 | [email protected] | 4 | 1102 | Sales Rep |
| 1370 | Hernandez | Gerard | x2028 | [email protected] | 4 | 1102 | Sales Rep |
| 1401 | Castillo | Pamela | x2759 | [email protected] | 4 | 1102 | Sales Rep |
| 1501 | Bott | Larry | x2311 | [email protected] | 7 | 1102 | Sales Rep |
| 1504 | Jones | Barry | x102 | [email protected] | 7 | 1102 | Sales Rep |
| 1611 | Fixter | Andy | x101 | [email protected] | 6 | 1088 | Sales Rep |
| 1612 | Marsh | Peter | x102 | [email protected] | 6 | 1088 | Sales Rep |
| 1619 | King | Tom | x103 | [email protected] | 6 | 1088 | Sales Rep |
| 1621 | Nishi | Mami | x101 | [email protected] | 5 | 1056 | Sales Rep |
| 1625 | Kato | Yoshimi | x102 | [email protected] | 5 | 1621 | Sales Rep |
| 1702 | Gerard | Martin | x2312 | [email protected] | 4 | 1102 | Sales Rep |
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
23 rows in set
Shell
It returns employees
all columns and rows in the table. An asterisk ( *
) should be used for testing. It is recommended to explicitly get the columns of the data for the following reasons:
- Using an asterisk (
*
) may return data for unused columns. It generates unnecessary I/O disk and network traffic between the MySQL database server and the application. - If the columns are specified explicitly, the result set is more predictable and easier to manage. Imagine when you use an asterisk (
*
) and someone changes the table data by adding more columns, you will get a different result set than expected. - Using an asterisk (
*
) may expose sensitive information to unauthorized users.
In this tutorial, you have understood and become familiar with SELECT
the usage of MySQL statements, and used SELECT
them to query data from MySQL tables.