欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何利用insert?into?values插入多條數(shù)據(jù)

 更新時(shí)間:2022年08月30日 14:14:18   作者:小旋風(fēng)-java  
這篇文章主要介紹了如何利用insert?into?values插入多條數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

insert into values插入多條數(shù)據(jù)

insert into 表名(字段名1,字段名2)values(值a1,值b1), (值a2,值b2),

例如:

insert into user_info (user_account,user_name,user_age,user_class) values (‘00001', '張三 ',‘20',‘計(jì)算機(jī)系'), (‘00002', ‘李四',‘19',‘計(jì)算機(jī)系');

SQL insert into插入的單行,多行的情況

1、在已有的表中,插入一行數(shù)據(jù)

a、第一種形式無(wú)需指定要插入數(shù)據(jù)的列名,只需提供被插入的值即可:

INSERT INTO ?table_name ?VALUES (value1,value2,value3,...);
insert into subject values ('語(yǔ)文')

b、第二種形式需要指定列名及被插入的值:

INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
insert into subject(subject_name) values ('數(shù)學(xué)')

2、在已有的表中,一次性插入多行行數(shù)據(jù)

INSERT INTO ?table_name ?VALUES (value1,value2,value3,...),(value1,value2,value3,...);

用逗號(hào)隔開,括號(hào)括起來,加多少行數(shù)據(jù)就寫多少個(gè)。要指定列名插入的,參考1.b 的做法。

insert into subject values ('數(shù)學(xué)'),('英語(yǔ)')

3、將表1 數(shù)據(jù)的某些列插入到表2 中去(其中表2是已經(jīng)創(chuàng)建好,與表1 插入列的屬性是一樣的): 

INSERT INTO ?表2(column1,column2) SELECT ?(column1,column2)FROM ?表1
insert into newtable(StuName) select studentname from student

4、將表1 數(shù)據(jù)的某些列插入到表2 中去???????(其中表2 是不存在的,在插入的同時(shí)創(chuàng)建一個(gè)新表):

SELECT ?column1,column2 ? ?INTO ?表2 ?FROM ?表1
select studentname,StudentClass ?into ?newcreate from Student

創(chuàng)建表的源碼貼在下面了,運(yùn)行下面的代碼創(chuàng)建以后,再運(yùn)行上面的插入代碼,可以更好地理解哦。

CREATE TABLE Student?
(?
? ? StudentNo int PRIMARY KEY IDENTITY(1,1),?
? ? StudentName nvarchar(15) NOT NULL,?
? ? StudentAge int DEFAULT ((7)),?
? ? StudentSex nvarchar(2) CHECK(StudentSex=N'男' or StudentSex=N'女'),?
? ? StudentClass nvarchar(15)?
)?
?
CREATE TABLE Subject
?
(
? ? SubjectNo INT PRIMARY KEY IDENTITY(1,1),?
? ? SubjectName NVARCHAR(15) NOT NULL?
)
?
CREATE TABLE StuResults
?
(?
? ? SR_No INT PRIMARY KEY IDENTITY(1,1),?
? ? SR_StudentNo INT,?
? ? SR_SubjectNo INT,?
? ? SR_Score INT CHECK (SR_Score>=0 AND SR_Score<=120)?
)?
?
alter table StuResults?
add constraint FK_StudentNo?
foreign key (SR_StudentNo) references Student (StudentNo)
???
alter table StuResults?
add constraint FK_Subject?
foreign key (SR_SubjectNo) references Subject (SubjectNo)

go
?
IF EXISTS(SELECT * FROM sysobjects where name = 'newtable')
drop table newtable
else
create table newtable
(?
? StuNo int PRIMARY KEY IDENTITY(1,1),
? StuName nvarchar(15) NOT NULL
)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論