先建一张基础表进行测试
create table user
(
id INT NOT null auto_increment,
name CHAR(10) not NULL DEFAULT'',
age INT not null DEFAULT 0,
PRIMARY KEY(id)
);
1.id使用默认自增的方式添加记录
insert into user(name,age) values('xixi',12);
#一次性插入多条记录
insert into user(name,age) VALUES('xixi',20),('haha',30);
2.为所有字段插入数据
insert into user(id,name,age) values(4,'zhangsan',20);
#一次性插入多条记录
insert into user(id,name,age) values(5,'lisi',30),(6,'wangwu',40);
3.还可以使用INSERT INTO…FROM 语句将查询结果插入到表中
当然这条命令在数据量大的时候别随便使用!!
因用了Insert into select语句,同事被开除了!
create table user2(
id int not null auto_increment,
name char(10) not null DEFAULT '',
age INT,
PRIMARY key(id)
)
INSERT INTO user2(id,name,age) SELECT id,name,age from user;