01_ Insert data

insert intoTable name (field name1, field name2) values(value1And the value2);
Copy the code

demonstration

First, enter the Sandy database

use sandy;
Copy the code

Create a Person table

create table if not exists person(
    id int.name varchar(20));Copy the code

Take a look at the table data

select * from person;
Copy the code

Now you can insert data

insert into person (id.name) values (1.'sandy');
Copy the code

Check whether the disk enclosure is successfully inserted

select * from person;
Copy the code

Pay attention to the point

  • The order of field names specified when inserting data does not have to be the same as the order of field names in the table
insert into person (name.id) values ('Tao'.2);
Copy the code

  • The values specified when inserting data must be in the same order as the specified field names
insert into person (name.id) values (3.'ls');
Copy the code

  • If the value order specified when inserting data is the same as the order of the fields in the table, you can omit the field name
insert into person values (3.'zs');
Copy the code

  • We can insert multiple values simultaneously through values
insert into  person values (4.'ls'), (5.'zl');
Copy the code

02_ Update data

First let’s take a look at the operators supported by WHERE

  • = = = =
  • ! = (not equal to)
  • <> (not equal to)
  • < (less than)
  • <= (Less than or equal to) > (greater than or equal to) >= (Greater than or equal to)
  • In (set); Fixed range values
  • between… The and. What is the range of values
  • Is null. (null)
  • Is not null
  • The and. with
  • The or. Or | |
  • The not; non
  • Like: fuzzy query
updateThe name of the tablesetField name = valuewhereConditions;Copy the code

demonstration

Start by looking at the data in the Person table

select * from person;
Copy the code

  • If no condition is specified when the data is updated, the data in the entire table is updated
update person set name='ww';
Copy the code

  • If a condition is specified when updating data, only data that meets the condition is updated
update person set name='sandy' where id=1;
Copy the code

  • When specifying conditions, we can specify multiple conditions by using and
update person set name='sandy' where name='ww' and id=2;
Copy the code

  • When specifying conditions, we can specify multiple conditions with OR
update person set name='sandy' where id=3 or name='ww';
Copy the code

  • It is possible to update multiple fields simultaneously when updating data
update person set name='Tao'.id=1 where id=5;
Copy the code

03_ Querying data

select * fromThe name of the table.Copy the code

This method will query all the data in the table, so the performance is poor

This method will query all the data in the table, cannot query the value of a specific field

If you want to query the complete writing of the data

selectThe field names1, field name2 fromThe name of the tablewhereConditions;Copy the code

demonstration

View the data for the Person table

select * from person;
Copy the code

  • Query data for a specific field
select name from person;
Copy the code

  • Query the data that meets the conditions
select id from person where name='Tao';
Copy the code

select id from person where name='Tao' || name='sandy';
Copy the code

select * from person where name in ('sandy'.'Tao');
Copy the code

select * from person where id between 1 and 3;
Copy the code

select * from person where id is not null;
Copy the code

select * from person where id is  null;
Copy the code

04_ Delete data

delete fromThe name of the tablewhereConditions;Copy the code

demonstration

View the data for the Person table

select * from person;
Copy the code

Delete the data that meets the conditions

delete from person where name='Tao';
Copy the code

Delete all data

delete from person;
Copy the code