Common operations on SQL (add, delete, modify, check)

  The common operations on SQL are mainly adding, deleting, modifying, and checking.

  1. Add, as the name suggests, is to add data (insert into). This statement is used to insert new records into the table. insert into has two uses.

    (1) There is no need to specify the column name of the data to be inserted, only the inserted value is provided,

      语法:INSERT INTO table_name VALUES (value1,value2,value3,...);

      But this method should pay attention to one thing, that is, you need to list each column of data in the inserted row. If there is no data in a certain column, you can use quotation marks instead. If the data of each column in the inserted row is not listed, an error will be reported.

 

    (2), specify the column name and the inserted value,

      语法:INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);

      This method can also add data to the specified column

 

    (3) Insert multiple pieces of data into the table at one time.

      The operation is, insert into table_name values ​​(value1, value2,...), (value3, value4,...), (value5, value6). You can specify a column name or not. If a column name is specified, data is added to the specified column. If no column names are specified, then each column of data in the inserted row needs to be listed.

 

  2. Delete, that is, delete data. There are actually two types here, delete and drop. However, the effects of the two are indeed different, although both can delete data.

    (1), delete. This statement is used to delete records in the table. Syntax: DELETE FROM table_name WHERE some_column=some_value;

      For example, to delete the data whose title is 'virus scan' from the myTable table. The operation is delete from [dbo].[myTable] where title='Anti-drug'

      If you want to delete all the data in the myTable table, the operation is delete from [dbo].[myTable], then the data in the table will be deleted, but the structure, attributes, indexes, etc. of the table will remain unchanged, and will not Free up memory space.

      Note: When using delete to delete data, if the where clause is omitted, all data will be deleted.

 

    (2), drop. This statement is used to drop the table, and the memory space will be freed.

      For example, to delete the table myTable, then the operation is, drop table [dbo].[myTable]

 

  3. Change, that is, change the data in the table (update), which is used to update the existing data in the table.

    语法:UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;

    For example, to change the data with the title of 'Sweeping Drugs' in the myTable table to 'Gu Tianle, Zhang Jiahui, Liu Qingyun', then the operation is, update [dbo].[myTable] set play='Gu Tianle, Zhang Jiahui, Liu Qingyun' where title='Anti-drug'

    Note: When using update to update the data in the table, if the where clause is omitted, then all the data in the table will be updated.

 

  4. Check, that is, query the data in the table (select).

    语法:SELECT column_name,column_name FROM table_name; 或 SELECT * FROM table_name;

    For example, to query all the data in the table myTable, you can use select * from [dbo].[myTable] or change "*" to the data name of each column of the data row in the table.

    (1) Select the data that contains a certain character in the table.

      For example, to select all data whose play contains 'Gu' in the table myTable, the operation is, select * from [dbo].[myTable] where play like '%gu%'

 

    (2), select the data starting with a certain character in the table.

      For example, to select all data whose play starts with "Ancient" in the table myTable, the operation is, select * from [dbo].[myTable] where play like 'Ancient%'

 

    (3) Select the data that ends with a certain character in the table.

      For example, to select all data whose play ends with "gu" in the table myTable, the operation is, select * from [dbo].[myTable] where play like '%gu'

 

    (4), select the data whose specified length contains specified characters in the table.

      For example, to select the data whose play contains three digits and "Ancient" in the table myTable, the operation is select * from [dbo].[myTable] where play like '_Ancient_'

 

      Select the data whose play contains three digits and starts with "Ancient" in the table myTable, then the operation is select * from [dbo].[myTable] where play like 'Ancient__'

 

      Select the data whose play contains three digits and ends with "Ancient" in the table myTable, then the operation is select * from [dbo].[myTable] where play like '__Ancient'

 

    ps: % => represents 0 or more characters

      _ => represents 1 character

 

    (5) Use the in operator, which allows multiple values ​​to be specified in the where clause.

      For example, to select the data whose title is "Drug Sweeping" or "Creeding Wolf" in the table myTable, the operation is select * from [dbo].[myTable] where title in ('Drug Sweeping', 'Creeding Wolf')

 

    (6) The between operator selects a value in the data range between two values. These values ​​can be numbers, text, or dates.

      For example, to select data in the table myTable with a score between 10 and 200. Then the operation is select * from [dbo].[myTable] where score between 10 and 200

      Note: In some databases, BETWEEN selects fields between but not including the two test values.

           In some databases, BETWEEN selects fields between and including two test values.

                In some databases, BETWEEN selects fields between two values ​​including the first test value but not the last test value.

 

    (7), select to return the specified number of data (select top).

      语法:SELECT TOP number|percent column_name(s) FROM table_name;

      For example, to select the first 100 pieces of data in the table myTable, the operation is select top 100 * from [dbo].[myTable]

 

      To select the top 50% of the data in the table myTable, the operation is select top 50 percent * from [dbo].[myTable]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325071933&siteId=291194637