[nodejs-05] dark horse nodejs study notes 05-basic database operations 01

3. Basic usage of MySQL

3.1 Use MySQL Workbench to manage databases

1. Connect to the database

Open workbence and enter password

2. Understand the components of the main interface

insert image description here

3. Create a database

insert image description here

DataType data type:
int : integer
varchar(len) : string
tinyint(1) : boolean value

Special identifier of the field
PK (Primary Key) : primary key, unique identifier
NN (Not Null) : the value is not allowed to be empty
UQ (Unique) : The value is unique
AI (Auto Increment) : The value increases automatically

5. Write data to the table

insert image description here

3.2 Using SQL to manage databases

1. What is SQL?

SQL (English full name: Structured Query Language) is a structured query language , a programming language specially used to access and process databases . It allows us to manipulate the data in the database in the form of programming .

Three key points:
① SQL is a database programming language
② Code written in SQL language is called SQL statement
③ SQL language can only be used in relational databases (such as MySQL, Oracle, SQL Server). Non-relational databases (such as Mongodb) do not support the SQL language

2. What SQL can do

① Query data from the database ② Insert new data
into the database Update data in the database Delete data from the database ⑤ Can create a new database ⑥ Can create a new table in the database ⑦ Can create stored procedures, views in the database ⑧ etc …





3. SQL Learning Objectives

Focus on mastering how to use SQL from the data table:
query data (select), insert data (insert into), update data (update), delete data (delete)
4 additional SQL grammars that need to be mastered:
where conditions, and and or operations character, order by sorting, count(*) function

3.3 SQL SELECT statement

1. Syntax The SELECT statement is used to query
data from a table . The results of the execution are stored in a result table (called a result set). The syntax format is as follows:

-- 这是注释,--后面要加一个空格才能注释完全
 -- 从 FROM 指定的[表中],查询出[所有的]数据。*表示[所有列]
  SELECT * FROM 表名称
 -- 从 FROM 指定的[表中],查询出指定 列名称 (字段)的数据。
  SELECT 列名称 FROM 表名称

Note : Keywords in SQL statements are not case sensitive . SELECT is equivalent to select, and FROM is equivalent to from.

2. SELECT * Example
Through the above steps, we can see that there is a Tables named users in the my-db-01 library. Click the table icon on the right side of the directory to write code and
insert image description here
enter the content. After clicking the little lightning, you can query all the tables in the table. List
insert image description here

3. Example SELECT column names
To get the contents of columns named "username" and "password" (from a database table named "users"), use the following SELECT statement:

select username,password from users

insert image description here

3.4 INSERT INTO statement of SQL

1. Syntax The INSERT INTO statement is used to insert new data rows
into the data table , and the syntax format is as follows:

-- 语法解读:向指定的表中,插入如下几列数据,列的值通过values——指定
-- 注意:列和值要一一对应,多个列和多个值之间使用英文的逗号隔开
insert into 表名 (列1,列2...values (1,值2...)

2. INSERT INTO example

Insert a user data whose username is tony stark and password is 098123 into the users table. The example is as follows:

insert into users (username,password) values ('tony stark','098123')

3.5 UPDATE statement of SQL

1. Syntax
Update statement is used to modify the data in the table. The syntax format is as follows:

-- 语法解读:
-- 1.用 UPDATE 指定要更新哪个表中的数据
-- 2.用 SET 指定列对应的新值
-- 3.用 WHERE 指定更新的条件
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 某值

2. UPDATE example - update a column in a row
Update the password of the user whose id is 3 in the users table to 888888. Examples are as follows:

update users set password='888888' where id=3

3. UPDATE example - update several columns in a row
Update the password and user status of the user whose id is 2 in the users table to admin123 and 1 respectively. Examples are as follows:

update users set password='88888',status=1 where id=2

3.6 SQL DELETE statement

1. Syntax
The DELETE statement is used to delete rows in a table. The syntax format is as follows:

-- 语法解读:
-- 从指定的表中根据where条件,删除对应的数据行
delete from 表名称 where 列名称=

2. DELETE example
Delete the user whose id is 4 from the users table, the example is as follows:

delete from users where id=4

3.7 SQL's WHERE clause

1. Syntax
The WHERE clause is used to limit the selection criteria . In SELECT, UPDATE, and DELETE statements, the WHERE clause can be used to limit the selection criteria.

-- 查询语句中的where条件
select 列名称 from 表名称 where 列 运算符 值
-- 更新语句中的where条件
update 表名称 set=新值 where 列 运算符 值
-- 删除语句中的where条件
delete from 表名称 where 列 运算符 值

2. Operators that can be used in the WHERE clause

operator describe
= equal
<> not equal to
> more than the
< less than
>= greater or equal to
<= less than or equal to
BETWEEN within a certain range
LIKE search for a pattern

Note: In some versions of SQL, the operator <> can be written as !=

3. Example of WHERE clause
The query condition of SELECT can be limited by WHERE clause:

-- 查 status 为1 的所有用户
SELECT * FROM users WHERE status=1
-- 查询 id 大于 2 的所有用户
SELECT * FROM users WHERE id>2
-- 查询 username 不等于 admin 的所有用户
SELECT * FROM users WHERE username<>'admin!

3.8 SQL's AND and OR operators

1. Syntax
AND and OR can combine two or more conditions in the WHERE clause . AND means that multiple conditions must be met at the same time , which is equivalent to the && operator in JavaScript, for example, if (a !== 10 && a !== 20) OR means that as long as any one condition is satisfied , it is equivalent to || in JavaScript Operators, such as if(a !== 10 || a !== 20)

2. AND operator example
Use AND to display all users whose status is 0 and whose id is less than 3

select * from user where status=0 and id<3

2. OR operator example
Use OR to display all users whose status is 1 or whose username is zs

select * from users where status=1 or username='zs'

3.9 ORDER BY clause in SQL

1. Grammar

The ORDER BY statement is used to sort the result set according to the specified column . The ORDER BY statement sorts records in ascending order by default . If you wish to sort the records in descending order , you can use the DESC key

2. ORDER BY clause - sorting in ascending order
Sort the data in the users table in ascending order according to the status field, examples are as follows:

select * from users order by status
-- 或者
select * from users order by status asc

3. ORDER BY clause – sort in descending order
Sort the data in the users table in descending order according to the id field, examples are as follows:

select * from users order by id desc

4. ORDER BY clause – multiple ordering

For the data in the users table, sort in descending order according to the status field first, and then sort in ascending order according to the alphabetical order of username. The example is as follows:

select * from users order by status desc,username asc

3.10 SQL's COUNT(*) function

1. Syntax
The COUNT(*) function is used to return the total number of data items in the query result , and the syntax format is as follows:

select count(*) from 表名车给

2. COUNT(*) Example
Query the total number of data items whose status is 0 in the users table:

select count(*) from users where status=0

2. Use AS to set an alias for the column.
If you want to set an alias for the queried column name, you can use the AS keyword. The example is as follows:

select count(*) as total from users where status=0

Guess you like

Origin blog.csdn.net/liqiannan8023/article/details/129039401