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;

insert image description here

Second, display all databases

SHOW DATABASES;

insert image description here

3. Delete the database

DROP DATABASE database_name;

insert image description here

Fourth, use the database

USE database_name;

insert image description here

5. Create a table

To create a new table in the database, a MySQL CREATE TABLEstatement can be used. CREATE TABLEstatement is one of the most complex statements in MySQL.

CREATE TABLEThe 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 TABLEthe name of the table to be created after the clause. Table names must be unique within the database. IF NOT EXISTSis 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 every CREATE TABLEstatement IF NOT EXISTSto prevent errors from creating new tables that already exist.
  • Next, column_listspecify 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 engineengine 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 TABLEdefine 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_nameSpecifies the name of the column. Each column has a specific data type and size, for example: VARCHAR(255).
  • NOT NULLor NULLindicates whether the column accepts NULLa value.
  • DEFAULTValue is used to specify the default value for the column.
  • AUTO_INCREMENTIndicates that the column's value is automatically incremented whenever a new row is inserted into the table. Each table has one and only one AUTO_INCREMENTcolumn.

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

tasksNow let's practice an example by creating a new table named as follows in the sample database (testdb) :

CREATE TABLEThis table can be created using the statement tasksas 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 TABLEstatements to create new tables in the database.

CREATE TABLE table_name (column_name1 data_type1, column_name2 data_type2, …);

insert image description here
insert image description here

6. Delete the specified table of the specified database

USE your_database_name;
DROP TABLE users; delete the specified table in the specified database

insert image description here
insert image description here

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);

insert image description here
insert image description here

8. Query data:

SELECT DATABASE(); query the current database

insert image description here

Introduction to MySQL SELECT statement

Use SELECTstatements 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. SELECTThe 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 yiibaidbthe following tables in the sample database ( ) employees. It has 8columns: number of employees, last name, first name, extension, email, office code, report, position, etc.

img

SELECTThe 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 SELECTthe 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

SELECTA statement consists of several clauses as described in the following list:

  • SELECTThis is followed by a list of comma-separated columns or an asterisk ( *), indicating that all columns are to be returned.
  • FROMSpecifies the table or view for which to query data.
  • JOINGet data from other tables based on some join conditions.
  • WHEREFilter rows in the result set.
  • GROUP BYCombines a set of rows into subgroups and applies an aggregate function to each subgroup.
  • HAVINGFilters are based on GROUP BYsmall groupings defined by clauses.
  • ORDER BYSpecifies a list of columns to use for sorting.
  • LIMITLimit the number of returned rows.

SELECTThe sum statement in the statement FROMis 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 SELECTthe simple form usage of the statement.

MySQL SELECT statement example

SELECTstatement allows to SELECTquery 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, SELECTthe 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 employeesthe data of all columns in the table, you can list SELECTall 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 employeesall 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 SELECTthe usage of MySQL statements, and used SELECTthem to query data from MySQL tables.

Guess you like

Origin blog.csdn.net/holyxp/article/details/132067034