数据定义语言 (DDL) 查询 SQL
Data Definition Language (DDL) Query SQL
我是查询和 SQL 服务器方面的新手,我想了解 DDL。
我的 Table 中有一个属性具有超过 1 个值,例如
大小 = {'S'、'M'、'L'}。
我如何使用查询在我的 Table 中创建属性,以便我可以将多个值插入到我的属性之一?
您不想这样做,因为这会使您的数据反规范化。但是,如果你必须的话。
declare @table table (id int identity(1,1), size varchar(16))
insert into @table
values
('S')
,('M')
select * from @table
update @table
set size = size + ',M'
where id = 1
select * from @table
这是一个使用外键的一对多方法
create table #items (id int identity(1,1), descrip varchar(64))
insert into #items
values
('shirt'),
('pants')
create table #item_sizes (id int identity(1,1), size char(1), item_id int)
alter table #item_sizes
add constraint FK_item foreign key (item_id) references #items(id)
insert into #item_sizes
values
('S',1)
,('M',1)
,('L',1)
,('S',2)
select
ItemID = i.id
,i.descrip
,isiz.size
from #items i
inner join #item_sizes isiz
on isiz.item_id = i.id
drop table #items, #item_sizes
根据您的要求:
产品(...,产品尺寸);
INSERT INTO Product (ProductSize) VALUES ('S')
如何查询 ProductSize 的行以便我可以在 SQL 中插入多个值? –
您可以像下面这样查询
select * from Product where ProductSize like '%S%' or ProductSize like '%L%' or ProductSize like '%M%'
我是查询和 SQL 服务器方面的新手,我想了解 DDL。 我的 Table 中有一个属性具有超过 1 个值,例如 大小 = {'S'、'M'、'L'}。 我如何使用查询在我的 Table 中创建属性,以便我可以将多个值插入到我的属性之一?
您不想这样做,因为这会使您的数据反规范化。但是,如果你必须的话。
declare @table table (id int identity(1,1), size varchar(16))
insert into @table
values
('S')
,('M')
select * from @table
update @table
set size = size + ',M'
where id = 1
select * from @table
这是一个使用外键的一对多方法
create table #items (id int identity(1,1), descrip varchar(64))
insert into #items
values
('shirt'),
('pants')
create table #item_sizes (id int identity(1,1), size char(1), item_id int)
alter table #item_sizes
add constraint FK_item foreign key (item_id) references #items(id)
insert into #item_sizes
values
('S',1)
,('M',1)
,('L',1)
,('S',2)
select
ItemID = i.id
,i.descrip
,isiz.size
from #items i
inner join #item_sizes isiz
on isiz.item_id = i.id
drop table #items, #item_sizes
根据您的要求:
产品(...,产品尺寸);
INSERT INTO Product (ProductSize) VALUES ('S')
如何查询 ProductSize 的行以便我可以在 SQL 中插入多个值? –
您可以像下面这样查询
select * from Product where ProductSize like '%S%' or ProductSize like '%L%' or ProductSize like '%M%'