Database _8_SQL basic operations - data operations

SQL basic operations - data manipulation

1. New data (two options)

  • plan 1:

  Inserting data into the fields of the whole table does not need to specify a field list. The order in which the values ​​of the data appear must be the same as the order in which the fields designed in the table appear. Any non-numeric data needs to be wrapped in quotation marks (single quotation marks are recommended)

  insert into table name values(value list)[,(value list)]; -- can insert multiple records at one time

desc my_student;
-- insert data
insert into my_student values(1,'itcast0001','Jim','male'),
(2,'itcast0002','Hanmeimei','female');

  

  • Scenario 2:

  To insert data into some fields, you need to specify a field list. The order in which the field list appears has nothing to do with the order of the fields, but the order of the value list must be consistent with the order of the selected fields.

  insert into table name(field list) values(value list)[,(value list)]; -- can insert multiple records at once

desc my_student;
-- Insert data: specify a list of fields
insert into my_student (number,sex,name,id) values
('itcast0003','male','Tom',3),
('itcast0004','female','Lily',4);

  

  Note that single quotes are not required to insert numbers, and single quotes must be added to strings.

2. View the data

select */ field list from table name [where condition]; -- / means or

  • View all data:

-- view all data
select * from my_student;

  

  • View the data of the specified field and the specified condition.

    -- View the data of the specified field and the specified condition
    select id,number,sex,name from my_student where id = 1; --View the information of students whose id is 1
    

  

 

 

 Generalized projection:

 

 

3. Update data

updata table name set field = value [where condition]; -- it is recommended to have where, otherwise it will update all

select * from my_student;
-- update data
update my_student set sex = 'female' where name = 'jim';
select * from my_student;

The update may not be successful: if there is no real data to be updated, the affected only means success, and no impact means unsuccessful, as shown below:

#Data has not changed
select * from my_student;
-- update data
update my_student set sex = 'female' where name = 'jim';
select * from my_student;

 

4. Delete data

Deletion is irreversible, delete with caution

delete from tablename[where condition];

select * from my_student;
-- delete data
delete from my_student where sex = 'male';#If you don't add where, the table my_student will be deleted
select * from my_student;

 

 

 

 

 

 

 

 

  

 

Guess you like

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