Mysql basic CRUD

CRUD:

column_name: Column name table_name: table name operator value: value operator

1.sql select query:

SELECT * FROM table_name;
SELECT column_name,column_name FROM table_name;

2.sql distinct statement:

  • (The only different value selected from "column_name" column "table_name" table, which is to remove the "column_name" column duplicate values):
SELECT DISTINCT column_name,column_name FROM table_name;

3.sql where the words:

  • For extracting those records that meet specified criteria.
SELECT * FROM table_name WHERE column_name='operator value';
  (where+条件)

If a numeric field, without using quotation marks; If a string, case sensitive

4.sql and & or operator:

AND & OR operator based on one or more conditions of the recording filtered.

If the first and second conditions are true, the AND operator displays a record.

If the first condition and the second condition as long as one is satisfied, the OR operator displays a record.

  • and operator:

Select the country from the "Websites" list as "CN" and alexa rank greater than "50" of all sites:

SELECT * FROM Websites WHERE country='CN'AND alexa > 50;
  • or operator:

Choose from the "Websites" list countries "USA" or "CN" to all customers:

SELECT * FROM Websites WHERE country='USA' OR country='CN';
  • and & or operator in combination:

Choose from the "Websites" alexa ranking table is greater than "15" and the country "CN" or "USA" all sites:

SELECT * FROM Websites WHERE alexa > 15 AND (country='CN' OR country='USA');

5.sql order by keyword: default ascending order

ORDER BY keyword is used to sort the result set according to a column or a plurality of columns.

When ORDER BY arrangement does not specify the ASC DESC time, the default is ASC.

  • Multiple columns in ascending order:
SELECT * FROM table_name ORDER BY column_name,column_name DESC; 
  • Single Descending:
SELECT column_name FROM table_name ORDER BY column_name ASC; 
  • where statements when ordering:
SELECT * FROM table_name WHERE column_name AND column_name ORDER BY  column_name ASC;   

6.sql insert into statement:

TNSERT INTO to insert a new record to the table.

  • No need to specify the column name you want to insert data only provide value to be inserted:
INSERT INTO table_name VALUES (value1,value2,value3,...);
  • You need to specify the value of the column name and is inserted:
INSERT INTO table_name (column1, column2, column3,...) VALUES ('value1','value2','value3',...);          

7.update statement:

UPDATE statement is used to update existing records in the table.

Execution did not WHERE clause UPDATE be careful.

UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值;
UPDATE table_name SET column_name='value', column_name='value' WHERE  column_name='value';
  • Example: Suppose we want, "rookie tutorial," the alexa ranking is updated to 5000, country changed USA:
UPDATE Websites SET alexa='5000', country='USA' WHERE name='菜鸟教程';

8.sql delete statement:

DETELE statement to remove rows in a table.

​ DELETE FROM table_name WHERE some_column='value';

  • Example: Suppose we "Websites" Delete website called "Baidu" and the country is CN's website from the table:
DELETE FROM Websites WHERE name='百度' AND country='CN';
  • Delete all the data:

Without deleting the table, delete all the rows in the table, which means that the table structure, properties, indexers will remain unchanged.

Be careful when deleting because it can not be repeated .

DELETE FROM table_name; 或者 DELETE * FROM table_name; 
  • About the difference between three statements deleted: (difference DROP, TRUNCATE, DELETE's)

DROP: drop table test, and free up space, delete the test completely.

DROP test

TRUNCATE: Delete the contents of the test table, and free space, without deleting the table definition, the target structure is still there.

TRUNCATE test

DELETE: Deletes the specified data; delete the entire table: delete all contents in the table only test, reserved the table definition, not free up space.

DELETE test

Guess you like

Origin www.cnblogs.com/wsq-1/p/12510620.html