Mysql Chapter 8 Summary--Insert, Update and Delete Data

content

  • insert data
  • update data
  • delete data

insert data

Create a table first

create table person
(
    id int unsigned not null auto_increment,
    name char(40) not null default '',
    age int not null default 0,
    info char(50) null,
    primary key(id)
);

Specify insertion of all columns

#插入数据
insert into person(id,name,age,info) 
values (1,'Green',21,'Lawyer');

unspecified inline

#允许直接插入,不指定字段,但是每一条都要有
insert into person
values(2,'Suse',22,'dancer'),
(3,'Mary',24,'Musician');

write picture description here


If there is no specified column, if auto-increment is set, it will be auto-incremented, and if there is a default value, the default value will be inserted.

#没有指定第一列
insert into person(name,age,info) #只指定了三列
values 
('Willam',20,'sports man');

write picture description here

#没有指定,使用默认值
insert into person(name,age) 
values ('Laura',25); #info列为null(默认)

Note some caveats when insert inserts multiple records:
write picture description here


Insert query results into a table

The results of one query can be inserted into another table

#测试将查询结果插入到表中
create table person_old
(
    id int unsigned not null auto_increment,
    name char(40) not null default '',
    age int not null default 0,
    info char(50) null,
    primary key(id)
);

insert into person_old
values(11,'Harry',20,'student'),
(12,'Beckham',31,'police');

select * from person_old;
select * from person;

insert into person(id,name,age,info)
(select id,name,age,info from person_old);

write picture description here
write picture description here


update data

Updating the data operation is very simple, the main thing is to remember the conditions .

#更新数据
update person set age = 15,name = 'LiMing' 
where id = 11;

delete data

#删除数据
delete * from person where id = 11;

#删除数据表中所有的数据
delete from person;

#注意删除数据时一定要小心

Guess you like

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