MySQL插入更新刪除資料

2021-07-25 17:31:42 字數 4572 閱讀 9693

更新資料

select * from person where id=10; 

update person set age = 15, name='liming'  where id = 10;

select * from person where id=10; 

update person set info='student' where age between 19 and 22;

select * from person where age between 19  and 22;

刪除資料

select * from person where id=10;

delete from person where id=10;

delete from person where age between 19 and 22;

select * from person where age between 19  and 22;

delete from person;

select * from person;

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)

);【例.1】在person表中,插入一條新記錄,id值為3,name值為green,age值為21,sql語句如下:

insert into person (id ,name, age , info) values (1,'green', 21, 'lawyer');

【例.2】在person表中,插入一條新記錄,id值為4,name值為suse,age值為22,info值為dancer,sql語句如下:

insert into person (age ,name, id , info)

values (22, 'suse', 2, 'dancer');

【例.3】在person表中,插入一條新記錄,name值為mary,age值為24,sql語句如下:

insert into person 

values (3,'mary', 24, 'musician');

【例.4】在person表中,插入一條新記錄,name值為willam,age值為20,info值為sports man,sql語句如下:

insert into person (name, age,info)

values('willam', 20, 'sports man');

【例.5】在person表中,插入一條新記錄,name值為laura,age值為25,sql語句如下:

insert into person (name, age ) values ('laura', 25);

【例.6】在person表中,在name、age和info欄位指定插入值,同時插入3條新記錄,sql語句如下:

insert into person(name, age, info)

values ('evans',27, 'secretary'),

('dale',22, 'cook'),

('edison',28, 'singer');

【例.7】在person表中,不指定插入列表,同時插入2條新記錄,sql語句如下:

insert into person 

values (9,'harry',21, 'magician'), 

(null,'harriet',19, 'pianist');

【例.8】從person_old表中查詢所有的記錄,並將其插入到person表中,過程如下:

首先,建立乙個名為person_old的資料表,

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)

);向person_old表中新增兩條記錄:

insert into person_old

values (10,'harry',20, 'student'), (11,'beckham',31, 'police');

將查詢結果插入到表中

insert into person(id, name, age, info)

select id, name, age, info from person_old;

【例.9】在person表中,更新id值為10的記錄,將age字段值改為15,將name字段值改為liming,sql語句如下:

update person set age = 15, name=』liming』 where id = 10;

【例.10】在person表中,更新age值為19到22的記錄,將info字段值都改為student,sql語句如下:

update person set info=』student』 where id between 19 and 22;

【例.11】在person表中,刪除id等於10的記錄,sql語句如下:

delete from person where id = 10;

【例.12】在person表中,使用delete語句同時刪除多條記錄,在前面update語句中將age字段值在19到22之間的記錄的info字段值修改為student,在這裡刪除這些記錄,sql語句如下:

delete from person where age between 19 and 22;

【例.13】刪除person表中所有記錄,sql語句如下:

delete from person;

MySQL 插入 更新 刪除資料

我們吧檢索單獨拉出去,是因為在jdbc中對於檢索的處理,和對於插入,更新,刪除操作是不同的。現在我們將分別介紹mysql的insert插入語句,update更新語句,delete刪除語句。part 1 插入資料 sql語句中,insert是用來插入的 或新增 插入或新增乙個行到資料庫中。有以下幾種方...

MySQL 插入 更新 刪除資料

插入 更新 刪除資料 一 插入資料 插入資料使用 insert 語句向表插入資料記錄,插入資料有四種方式,即為所有字段插入資料 為指定字段輸入資料 同時插入多條資料以及插入查詢結果。先建立乙個表,方便下面舉例 create table student stu id int 10 primary ke...

MySQL插入更新刪除資料

insert into customers values null pep e.lapew 100 main street los angeles ca 90046 usa null null insert into customers cust name,cust address,cust cit...