Common operations of the database

1. Origin of technology

Database operations, no matter the server , front-end , or mobile end , more or less involve data storage, query, and modification . Therefore, as a developer, database operation is also a necessary skill for development.

The full name of SQL is Structured Query Language. After translation, it is structured query language . It is a database query and design language for accessing data and querying, updating and managing relational database systems .

Common databases include MySQL , SQLServer , ORACLE , DB2 and so on.

2. Database foundation

Database operation overview diagram:

insert image description here
The basic operation steps of the database:

  • 1. Create a database
  • 2. Connect (open) the database
  • 3. Create a table
  • 4. Add data to the table
  • 5. Update data, query data, delete data
  • 6. Disconnect (close) the database

1, CREATE TABLE

CREATE TABLE emp 
( id int NOT NULL PRIMARY KEY, //添加主键
  name varchar(20),
  gender varchar(2),
  performance int,
  salary double
)

If you forget to add the primary key or foreign key after creating the table, you can use ALERT to add it.

ALERT TABLE emp ADD PRIMARY KEY(id); //添加主键
ALERT TABLE orders ADD FOREIGN KEY (e_id) REFERENCE emp(id); //添加外键

2. INSERT (insert)

Add data to the table.

//向emp 表中插入一条数据,插入字符串时使用''
INSERT INTO emp VALUES(1, 'yijie', 'male', 85, 18000.0);

The standard format for inserting data into a table is:

insert into tableName(column1, column2...) values('value1', 'value2'...)

3. UPDATE

Update data in the table.

update emp set salary=20000 where name='yijie';

4. DELETE (delete)

delete from emp where id=8;//删除表中的某条数据,where后面的为条件
delete * from emp;//删除表中的所有数据,清空表
drop table 表名称; //删除某张表

Note: When using delete to delete data in a table, if the table is associated with other tables, such as foreign keys , you must first delete the foreign keys in the associated table.

5. DISTINCT (deduplication)

After a table is operated for a period of time, data duplication is unavoidable. Duplicated data is not only meaningless, but also takes up storage space. At this time distinct quietly debuted. distinct is used to remove duplicate content in the table according to conditions.

//查询emp中的name,返回唯一的名字
select distinct name from emp;

6. Select (query)

Queries are the most commonly used operations in database operations, but also the hardest. The select statement is used to query data from a table, and the results are stored in a result table (called a result set ).

SELECT syntax:
SELECT column name FROM table name; // query a certain column data in the table
SELECT * FROM table name; // query the entire table
and more complex conditional query.

3. Basic functions

The database also provides us with some functions to facilitate our database operations. These basic functions basically have column names as function parameters and return the calculation result of a certain column.

1. AVG() average value

avg() is used to return the average value of a column, NULL is not included in the calculation.

select AVG(salary) as avg_salary form emp; //查询员工的平均薪水

2.COUNT()

The COUNT function is used to return the number of rows matching the specified criteria.

select COUNT(*) from emp; //返回表的记录数

3.MAX()

The MAX function returns the maximum value of the specified column, NULL words are not included in the calculation.

4.MIN()

The MIN function returns the minimum value of the specified column, NULL words are not included in the calculation.

5.SUM()

The SUM function returns the total for a specified column.

6.ROUND()

The ROUND function is used to round a numeric field to a specified number of decimal places.

select ROUND(salary,1) as n_salary from emp; //将salary保留一位小数

select ROUND(column_name,decimals) from table_name;

parameter describe
column_name the field to round
decimals Specifies the number of decimal places to return

7.FORMAT()

FORMAT is used to format the display of the specified field .
SELECT FROMAT(column_name, format) FROM table_name;

parameter describe
column_name the field to format
format specified format

4. Advanced usage

There are also some advanced usages of SQL, such as pagination , fuzzy matching , sorting and so on.

1. Pagination (LIMIT)

Pagination query is to return the data of the page corresponding to the current page number.
The basic formula of pagination query: (page - 1) * pageSize + the number of data items to be displayed on the current page

select * from emp limit 4; //返回前4条数据

2. Fuzzy matching (LIKE)

Fuzzy matching is used in conjunction with where conditions.

//%可以理解为定义通配符
select * from emp where name like 'a%';  //返回以a开头的所有姓名

3.IN

Returns all data in a collection for a specific column.

select * from emp where name in ('AA', 'BB'); //返回name为AA、BB的所有数据。

4.JOIN

The join table operator JOIN is used to associate two or more tables and query data from these tables.
Several commonly used connection methods:

  • INNER JOIN: inner connection.
  • LETF JOIN: returns all rows from the left table even if there is no match in the right table.
  • RIGHT JOIN: Returns all rows from the right table even if there is no match in the left table.
  • FULL JOIN: Return as long as a table exists.

5.UNION

The UNION operator is used to combine the result sets of two or more SELECT statements.

The SELECT statements inside the UNION must have the same number of columns, and the columns must also have similar data types. Also, the order of the columns in each SELECT statement must be the same.

6.AUTO_INCREMENT (self-increment)

It is generally used to modify the primary key to keep it from increasing.

7. ORDER BY (sorting)

Use order by to sort the query results, the default is ascending .

  • ASC: ascending order (from small to large)
  • DESC: descending order (from largest to smallest)
select * from emp order by name;

8.GROUP BY

Usually matching aggregate functions are used to group by one or more queued result sets.

9.HAVING

Used to set conditions for grouping.

10.DEFAULT

The default constraint is used to insert a default value into the column.

write at the end

This article is a summary of some writing methods and functions that are often used in the database, so that they can be quickly queried when they are used in the future.
Digression: Mainly because when I went to the interview some time ago, when I was asked how to write a statement to modify a piece of data, I didn’t come back to it, so I decided to make a summary of the common operations of the database.

Guess you like

Origin blog.csdn.net/u010389309/article/details/88523982