Mysql之insert into

INSERT INTO 插入语句的使用

第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:
INSERT INTO table_name VALUES (value1,value2,value3,…);
eg:

INSERT INTO Student VALUES ('03','孙风','1990-05-20','男');

第二种形式需要指定列名及被插入的值:
INSERT INTO table_name (column1,column2,column3,…)
VALUES (value1,value2,value3,…);
eg:

INSERT INTO student(SId,Sname,Sage,Ssex) VALUES('04','李云','1990-08-06','女'),('05','周梅','1991-12-01','女')

第三种方法:
INSERT INTO table_name SELECT value1,value2 union all
SELECT value1,value2;
如果插入的是数字的话,需要在数字后面加逗号n
eg:

INSERT INTO Student SELECT '07','郑竹','1989-07-01','女' UNION ALL SELECT'10','赵红','2001-01-01','女';

可以写成:

INSERT INTO Student SELECT 01,n'郑竹','1989-07-01','女' UNION ALL SELECT 10,n'赵红','2001-01-01','女';

第四种方法:(如果两个表的结构一样,复制旧表的信息到新表去
INSERT INTO new_table SELECT 语句

eg:

INSERT INTO new_student SELECT * from student where Ssex='男';

第五种方法:如果两个表的结构不一样,复制旧表信息到新表中
INSERT INTO new_table(column) SELECT 语句
eg:

insert into new_student(sname) select student.Sname from student where sname = '赵红';

猜你喜欢

转载自blog.csdn.net/SYSZ520/article/details/115145319