Quickly master SQL syntax in one hour

1. Connect to the database and create a database and data table

This step guides you how to connect to the MySQL database on the ECS instance and create the database and data tables required for the experiment.

Run the following command to log in to the database.

mysql -uroot -p

Enter the root user login password, the password is t2+Ekyrh1 ;)R.

img

The returned result is as follows, indicating that the login is successful.

img

Run the following command to change the root user password to Test123!.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Test123!' PASSWORD EXPIRE NEVER;

Create a database.

  • Syntax for creating a database:
create DATABASE 数据库名;
  • Example of creating a database:

Execute the following SQL statement to create the database Test.

create DATABASE Test;

Select a database.

  • Syntax for selecting a database:
use 数据库名;
  • Example of selecting a database:

Execute the following SQL statement and use the database Test.

use Test;

Create a data table.

  • Syntax to create a data table:
CREATE TABLE table_name(column_name column_type);
  • Example of creating a data table:

Execute the following SQL statement to create the data table Websites in the Test database.

CREATE TABLE Websites (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
  `url` varchar(255) NOT NULL DEFAULT '',
  `rank` int(11) NOT NULL DEFAULT '0' COMMENT '排名',
  `country` varchar(255) NOT NULL DEFAULT '',
   PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

2. Addition, deletion, modification and query of SQL

This step takes you to learn the basic syntax of SQL to add, delete, modify, and query.

INSERT INTO statement.

INSERT INTO statement is used to insert new records into the table.

  • Syntax of INSERT INTO statement:

The INSERT INTO statement can be written in two forms.

  • The first form does not need to specify the column name to insert data, just provide the value to be inserted.
INSERT INTO table_name
VALUES (value1,value2,value3,...);
  • The second form requires specifying the column name and the value to be inserted.
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
  • Example of an INSERT INTO statement:

Execute the following SQL statement to insert five pieces of data into the Websites table.

INSERT INTO Websites VALUES ('1', '阿里云', 'https://www.aliyun.com/','12','CN'), ('2', '淘宝', 'https://www.taobao.com/','1','CN'), ('3', '帮助中心', 'https://help.aliyun.com/','112','CN'), ('4', '开发者社区', 'https://developer.aliyun.com/','213','CN'), ('5', '云起实验室', 'https://developer.aliyun.com/adc/',20,'CN');

SELECT statement.

The SELECT statement is used to select data from the database, and the results are stored in a result table, called a result set.

  • The syntax of the SELECT statement:
    • The first form selects some columns from the table_name table.
SELECT column_name,column_name
FROM table_name;
  • The second form selects all columns from the "table_name" table.
SELECT * FROM table_name;
  • Example of a SELECT statement:

Execute the following SQL statement to select all columns from Websites.

SELECT * FROM Websites;

img

UPDATE statement.

The UPDATE statement is used to update existing records in the table.

  • The syntax of the UPDATE statement:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
  • Example of an UPDATE statement:

Execute the following SQL statement to update the rank column of Yunqi Lab to 3, and then read the Websites table.

UPDATE Websites SET rank='3' WHERE name='云起实验室';
SELECT * FROM Websites;

img

DELETE statement.

DELETE statement is used to delete rows in a table.

  • The syntax of the DELETE statement:
DELETE FROM table_name
WHERE some_column=some_value;
  • Example of DELETE statement:

Execute the following SQL statement to delete the website named Developer Community from the Websites table. Then read the Websites table.

DELETE FROM Websites WHERE name='开发者社区';
SELECT * FROM Websites;

img

3. SELECT DISTINCT statement

In a table, a column may contain multiple duplicate values, and sometimes you may wish to list only distinct (distinct) values.

The SELECT DISTINCT statement is used to return uniquely distinct values.

  • Syntax of SELECT DISTINCT statement:
SELECT DISTINCT column_name,column_name
FROM table_name;
  • Example of a SELECT DISTINCT statement:

Execute the following SQL statement, first insert a piece of data, and then select the only different value from the country column of the Websites table, that is, remove the duplicate value of the country column.

INSERT INTO Websites VALUES ('6', 'Google', 'https://www.Google.com/','12','USA');
SELECT DISTINCT country FROM Websites;

me;




- SELECT DISTINCT语句的示例:

执行如下SQL语句,先插入一条数据,然后从Websites表的country列中选取唯一不同的值,也就是去掉country列重复值。

INSERT INTO Websites VALUES (‘6’, ‘Google’, ‘https://www.Google.com/’,‘12’,‘USA’);
SELECT DISTINCT country FROM Websites;




![img](https://img-blog.csdnimg.cn/img_convert/072c3527e6c574c764be466a6e7b2eec.png)

Guess you like

Origin blog.csdn.net/m0_55877125/article/details/131516258