A complete collection of practical database operations

1. Database
1. Create database
create database database name;
2. Use database
use database name;
3. Display database
show databases;
4. Delete database
drop database database name;

2. Table operations
1. Create a table
create table table name (column name type, column name type...);
2. Display the table;
show tables;
3. See the table structure
desc table name;
4. Delete the table
drop table table name ;

3. Query the table
1. View the entire table
select * from exam_result;
2. Insert table values
​​insert into table name values ​​(value, value, value...);
Special instructions
insert into table name values ​​("2019-10-23 10: 10:10");
3. Insert into the specified column
into table name (column name, column name) values ​​('Tang Dan', 93.0);
4. Insert multiple insert
into table name values ​​(1, 'Zhang San') , (2,'李思');
5. Specify column query
select column name from exam_result;
6. Alias ​​query
select name, math+chinese as total from exam_result;
7. Deduplication query
select distinct column name from table name;
8 .Sequential query
select column name from table name order by desc (asc);

4. Conditional query
1. For students whose math score is equal to 80
, select name,math from exam_result where math =80;
2. For students whose math score is greater than 80,
select name,math from exam_result where math >80;
3. For students whose math score is between 80 and 100 Students whose scores are 27, 48, 90, and 78 select name, math from exam_result where math in (
27, 48, 90, 78);

5. Fuzzy search
select name from exam_result where name like 'Tang%';

select name from exam_result where name like ‘孙__’;

6. Use <=> to find
select name from exam_result where math <=> NULL;

7. Paging query
select column name from exam_result limit N offset M; start querying from M records, and return at most N records.

Limit, order by, where are used together

8. Modify the data of a certain row table
update table name set column name = value, column name = value where clause;

9. Delete the data of a certain row table
delete from table name where condition;

Constraints create table
1. NOT NULL
create table student (id int not null, name varchar(20));
2.default
create table student (id int default 0, name varchar(20) default 'unnamed');
3.primary key

4.unique
create table student(id int unique, name varchar(20));
5.froeign key
创建父表
create table class(classid int primary key,classname name varchar(20));
创建子表
create table student(sno int primary key , sname varchar, classid int ,foreign key (classid) references class(classid));

Aggregation query
insert into table name select * from table name; //Execute the query statement first, and then put the results into the inserted table name
1.count() //Calculate the number of rows
select count( ) from table name; //Required Know that count( ) will also add null values, count(column name) will not add null values
​​2.max() //Calculate the largest value in a column
3.min()//Calculate the smallest value in a column Value
4.avg()//Calculate the average of a column
5.sum()//Calculate the sum of a column
2345 Numbers only

Group query


  1. group by can group and display select gender, avg (score) from student group by gender according to the specified column name ;
  2. You can filter first and then group the query
    to select genderselect,avg (score) from student where name! ='Zhang San' group by gender;
    3. You can group first and then query
    select gender, avg(score) from student group by gender having avg(score) >80;
    4. You can also use where +group by + having

Cartesian product
means to jointly query multiple tables for display query.
In theory, the number of rows in a Cartesian table formed by two tables is the sum of the products of the two tables, and the sum of the number of columns in the two tables is Cartesian. The number of columns in the table.

Database design
1) Find entities
2) Clarify the relationship between entities
About multi-table query
select column name from table 1 join table 2 join table 3 on connection conditions.
There is no need to add where in the connection condition.

Here we talk about why it takes a long time to complete the Cartesian product of three tables.
The reason is that the display is too slow.

Inner join: This is the query statement we used above, which is the common information contained in the two tables.
So, as the name suggests, we can think of outer joins. There are two types of outer joins

  1. Left outer join
    means that the list on the left side of the join is the main one, and it does not exist in the right table, so NULL is filled in;

  2. Right outer join
    means that the list on the right side of the join is the main one, and it does not exist in the left table, so NULL is filled in;

Multiple join
table one join table two left join table three; // Based on the Cartesian product of table one and table two.
Table 1 right join Table 2 left join Table 3; // Table 1 and Table 2 first perform a right outer join, and the obtained results are then used as a left outer join with Table 3.

In addition to internal and external connections, there are also self-connections, but this is rarely used and belongs to specific uses in specific scenarios.
The function is to convert rows into columns.
Instance:
displays all the score information of "Computer Principles" with higher scores than "Java".
This is on the row from the beginning. We cannot compare the information of the two scores, so we can use this self-join.
The ID of Computer Principles is 3, and the ID of Java is 1;

  • Preliminary screening, there are many unnecessary permutations and combinations
    -Insert image description here
  • In the second step, the students’ IDs must be consistent.Insert image description here
  • In the third step, as long as the records of s1.course_id =3 and s2.course_id=1 and s1.score>s2.score are found.
    Insert image description here
    Subquery
    queries the Chinese and English scores of all students . Merge query

    combines
    the query results of the two tables. Records are collected and merged together.
    Insert image description here
    union // If there are duplicate results, they will be deleted.
    union all // If there are duplicate results, they will not be deleted.

Database index (directory)
1. View the index
show index from table name;

2. Create index

create index index name on table name (column name);
3. Delete index
drop index index name on table name;
the primary key of a table will automatically be indexed, unique and foreign key constrained columns will also be automatically indexed, create Indexing and deleting indexes are also dangerous operations, especially when operating on a table containing a large amount of data.

Guess you like

Origin blog.csdn.net/m0_55634684/article/details/125732082