(Original) SQL Summary (1)

I have just finished brushing SQL and I must know it. I walked through the code and it is not difficult. The main thing is that there are many places to pay attention to, so I will record what I need now.
Just follow the order of the table of contents in the book.

Database Basics

Following the previous essay, we have generated the database and corresponding tables. Here, the database is orderdb, and the table is customers, orders, orderitems, etc.

concept

Database: A container that holds organized data, note that databases are not software
Tables: Structured files that can store data of a specific type, each table in a database has a name to identify itself
Schema: About the database and the layout of the tables and feature information
Column: The column stores the information of a certain part of the table, such as our previous cust_id, cust_name, etc. are the columns of the table customers
: The data in the table is stored in rows, each record is stored in its own row, and the row is A record of the table. Also known as database records.
Primary key: Each row has one or several columns that uniquely identify itself. Any column in the table can be used as the primary key, as long as the following conditions are met: No
two rows have the same primary key value.
Each row must have a primary key, and the primary key column does not Allow NULL
primary key column value not allowed to update or modify
primary key value can not be reused
SQL is a language specially used to communicate with the database

Retrieve data SELECT

The most commonly used is SELECT, to retrieve information from one or more tables

Retrieve single column, multiple columns, all columns

SELECT prod_id, prod_name FROM Products;
where prod_id prod_name is a column in the table Products, here we usually set the keywords of SQL to uppercase, the table to capitalize the first letter, and the column to lowercase, but this is just for the convenience of viewing, the SQL itself not case sensitive.
In addition, a semicolon must be added after the SQL statement!
SQL statements can be written as one line or multiple lines. When there are multiple lines, you only need to add a semicolon after the last line.
SELECT * FROM Products; means to retrieve all the columns of the Products table.

retrieve different values

If we want to retrieve the different values ​​of a column, instead of all the rows under the column, use the following statement
SELECT DISTINCT proud_id FROM Products;
note that if it is written as SELECT DISTINCT proud_id, prod_name FROM Products; then DISTINCT acts on two columns, and the output is a row of different values ​​​​compounded by the two columns.

Limit results

The limit will not output all the data, but a part of it, such as the first five or the last five. At this time, the syntax of different databases is different. Take MYSQL as an example:
SELECT prod_name FROM Products LIMIT 5; output the first five
SELECT prod_name FROM Products LIMIT 5 OFFSET 5; Instructs to return 5 rows of data from row 5, excluding row 5, the first number is the number of rows retrieved, and the second number refers to where to start.
Note: row 0 is retrieved first, not row 1.
MYSQL supports simplified search, that is, use LIMIT3,4 instead of LIMIT 4 OFFSET 3;

Notes

SQL uses -- to indicate a comment, note that there is a space between -- and the corresponding comment, such as "-- This is a comment"
Adding # at the beginning of a line will make the entire line a comment, indicated by /..../ Multi-line comments.

Specify the sort direction

Sort data

There is no specific order for the previously selected data. You can use ORDER BY to add one or more column names to sort the output.
SELECT prod_id, prod_name FROM Products ORDER BY prod_id; the data will be sorted by prod_id and output
SELECT prod_id, prod_name ,prod_price FROM Products ORDER BY prod_id,prod_name; will be sorted and output by two columns prod_id, prod_price.

Sort by column position

SELECT prod_id,prod_name,prod_price FROM Products ORDER BY 2,3; indicates that the output is sorted according to the two columns of name and price

Sort direction

The default is ascending
SELECT prod_id, prod_name FROM Products ORDER BY prod_id DESC; it means descending
SELECT prod_id, prod_name, prod_price FROM Products ORDER BY prod_id DESC, prod_name; note that DESC only acts on a column name in front of it.
If you want to sort all columns in descending order, then each column must be followed by DESC.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693595&siteId=291194637