MySQL Study Notes: Query
Chapter 3 1. When we do not know what database names can be used? You can use the following command to View Details:
`SHOW DATABASES;`
The result is as follows:
2. When we know the databases that can be used, such as mysql, information_schema, and test, how can we access the test database?
Run the following command: USE test;
3. After we enter a database, we can run the following command to check which tables can be used in the database ):
Show tables;
4. When we want to know the structure of a table, use the following command to view it:
`DESC user;`
5. view the content of a table. What is the command?
For example, the command to view the table user in the database is as follows:
Select * from user;
6. All the columns are directly displayed with the * sign above. What should we do if we only want to see the column of the ID number?
`select id from user;`
7. The command format is as follows:
`select id ,name from user;`
As you can see, the column is displayed in front of it. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoNCBpZD0 = "insert"> insert
To facilitate the explanation of other query commands, some data is inserted into the table for the following operations.
The INSERT command is as follows:
Syntax
Insert into table name VALUES (value 1, value 2 ,....)
You can also specify the columns to insert data:
Insert into table_name (column 1, column 2 ,...) VALUES (value 1, value 2 ,....)
8. When we only want to display the first few rows in a table, or from the row to the row, we need to use the keyword
Limit.
The command format is as follows:
Select * from table_name limit N; // limit N indicates that MySQL can display up to N rows of data, starting from scratch by default.
Select * from table_name limit M, N; // limit M, N indicates N rows of MySQL Data starting from Row M. Note: The row number starts from scratch.
For example:
Select * from user limit 2; // only displays the content of the first two rows in the table
Select * from user limit 2, 3; // only three entries starting from row 2nd in the table are displayed.
9. When we want to make the displayed content different from each other, or filter out the same row data, we should use the DISTINCT keyword.
`select distinct name from user;`
Note: The distinct keyword is applied to all columns, not only the pre-defined columns. If select distinct id, name from user is given, all rows will be retrieved unless the IDs and names of the specified two columns are different.
10. Use a fully qualified table name
In the preceding example, columns are referenced by column names. You can also use a fully qualified name to reference columns (both the table name and column name are used), as shown below:
Select tableName. colName from tableName;