当前位置:网站首页>Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)

Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)

2022-06-25 18:26:00 Zebra and running

Catalog

1.CRUD

2. newly added (Create)

(1). Single line data + Insert all columns

(2). Multi row data + Specify column insertion

3. Inquire about (Retrieve)

(1). Full column query

(2). Specified column query

(3). The query field is an expression

(4). Alias

(5). duplicate removal :DISTINCT

(6). Sort :ORDER BY

(7). ( important ) Conditions of the query :WHERE

1). The basic query

2). Range queries

3). Fuzzy query :LIKE

4).NULL Query for :IS [NOT] NULL

(8). Use and query of dates

(9). Paging query :LIMIT(MySQL special )

eg: Use paging query to select the students with the second Chinese score

4. modify (Update)

5. Delete (Delete)

6. The main content is summary

(1). How to delete Id Maximum value

(2). Normal summary


1.CRUD

  1. notes : stay SQL Can be used in “-- Space + describe ” To indicate a comment
  2. CRUD That is, increase (Create)、 Inquire about (Retrieve)、 to update (Update)、 Delete (Delete) An acronym for four words .

2. newly added (Create)

grammar :

give an example :

(1). Single line data + Insert all columns

(2). Multi row data + Specify column insertion

1. The string in the database is a single quotation mark

2. Generally, the database should have a unique field to identify the table , stay java In order to correspond to the database , That also needs to have such an attribute

3. Write later , Try to use only the writing method of the specified column insertion , Because if you don't specify column insertion , In the future, if you add a field to your table , Then the previous statement will report an error

4. Annotation syntax :-- Space


3. Inquire about (Retrieve)

grammar :

(1). Full column query

(2). Specified column query

(3). The query field is an expression

When the query field is an expression , He'll check later , Calculate by yourself and then output

(4). Alias

Specify aliases for columns in query results , Represents the returned result set , Use alias as the name of the column .

grammar : 

  • Tips: Alias “ Total score ” I can add as
  • notes : Here you can see , The data we show here is the result set returned to us by the database . The black box here is the client , The database is the server

(5). duplicate removal :DISTINCT

Use DISTINCT Keyword to de duplicate a column of data :

distinct Put in front of the field to be specified , And only one field can be added

(6). Sort :ORDER BY

Java Sort :(1) Algorithmic sorting (2)java Compare interfaces for sorting

grammar :

1. No, ORDER BY Clause , The order of return is undefined , Never rely on this order

2. NULL Data sorting , Treat as less than any value , The ascending order appears at the top , Descending order appears at the bottom

3. Use expressions and aliases to sort

4. You can sort multiple fields , The order of priority follows the order of writing

5.order by In general, and limit Use it together , Used to select the first three elements .

(7). ( important ) Conditions of the query :WHERE

Comparison operator :

The comparison operators and java The comparison operators inside are different

  1. = It's a comparison operator , Means to judge equal , No more java Inside == 了
  2. As long as you know = Can , It doesn't mean just knowing != Just go

Logical operators :

1). The basic query

  • Be careful WHERE Conditions can be expressed as , But you can't use aliases .

2). Range queries

 --BETWEEN ... AND ...

--IN

3). Fuzzy query :LIKE

  1. % The match is 0 Characters or more , For example, there is only one ‘ Grandchildren ’, That's only ‘ Grandchildren %’ Can match
  2. _ The match is a character

4).NULL Query for :IS [NOT] NULL

(8). Use and query of dates

1. Create a table with dates

  1. Here we need to set a default value for the date , But I don't have to set the default value myself .
  2. Set the default value to null That's all right.

2.mysql Default date format :yyyy-MM-dd HH:mm:ss

Insert data with date

3. Query date field

Like other fields , You can also query between-and.

4.

(9). Paging query :LIMIT(MySQL special )

grammar :

  1. s It's the offset ( Just offset a few first ),n Indicates the number of filter results
  2. the second LIMIT s,n Means screening n Bar result , From s Data starts
  3. Third LIMIT n OFFSET s Means screening n Bar result , From s Data starts
  4. This is not to suggest a third , The second kind can also , It depends on which one you use more smoothly , One is to s after n, The other is to n after s, I suggest the third , Because no s The time is LIMIT n
  5. Order by In general, and limit Use it together

eg: Use paging query to select the students with the second Chinese score

select * from exam_result order by chinese desc limit 1 offset 1


4. modify (Update)

grammar :

  1. stay set When , If the length of the value exceeds the length given when creating the table , Will report a mistake .
  2. Like the last set chinese = chinese * 2; You may report an error
  3. If not where,order by And so on , That is, the values of the whole column of elements are modified

5. Delete (Delete)

grammar :

Be careful delete from table_name, There's nothing in the back , That is, the whole table is deleted , Be sure to pay attention to . To add where,order by,limit etc.


6. The main content is summary

(1). How to delete Id Maximum value

mysql in You can't specify target table for update in FROM clause The wrong meaning is to say , Not first select Give some values in the same table , Again update This table ( In the same sentence ).

error :

  correct :

(2). Normal summary

newly added :

Inquire about :

modify :

Delete :

原网站

版权声明
本文为[Zebra and running]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190531314978.html