比如table中三个字段 a b c
c的值怎么设置默认为(a-b)?求大神指教!
10 个解决方案
#1
就这20分了,希望大神解决
#2
create table [dbo].[table_name]
(
[a] [int] null,
[b] [int] null,
[col_add] as ([a]+[b]),
[col_ji] as ([a]*[b])
)
on [primary]
--插入数据
insert into table_name(a,b)
select 2,3
--查询出结果
select * from table_name
--删掉表
drop table table_name
/*
a b col_add col_ji
----------- ----------- ----------- -----------
2 3 5 6
*/
#3
#4
为什么不用计算列呢?
#5
--如果关系是固定可以用类似#1的形式。
create table [dbo].[table_test]
(
[a] [int] null,
[b] [int] null,
[c] as ([a]-[b])
)
insert into [table_test](a,b)
select 10,6
select * from [table_test]
/*
a b c
----------- ----------- -----------
10 6 4
*/
--正常情况不用存,取得的时候计算一下就可以了。
select a,b,a-b as c from tablename
#6
怎么用?
#7
CREATE TABLE test
(
a INT,
b INT,
c AS a-b
)
INSERT INTO test(a,b)
VALUES (2,1)
SELECT * FROM test
/*
a b c
----------- ----------- -----------
2 1 1
*/
#8
视图设计怎么设计
#9
为啥要用视图?如果非要用视图,假设你的表只有a、b列
create view test
as
select a,b,a-b as c
from test
create view test
as
select a,b,a-b as c
from test
#10
已经设置好了谢谢啊
#1
就这20分了,希望大神解决
#2
create table [dbo].[table_name]
(
[a] [int] null,
[b] [int] null,
[col_add] as ([a]+[b]),
[col_ji] as ([a]*[b])
)
on [primary]
--插入数据
insert into table_name(a,b)
select 2,3
--查询出结果
select * from table_name
--删掉表
drop table table_name
/*
a b col_add col_ji
----------- ----------- ----------- -----------
2 3 5 6
*/
#3
#4
为什么不用计算列呢?
#5
--如果关系是固定可以用类似#1的形式。
create table [dbo].[table_test]
(
[a] [int] null,
[b] [int] null,
[c] as ([a]-[b])
)
insert into [table_test](a,b)
select 10,6
select * from [table_test]
/*
a b c
----------- ----------- -----------
10 6 4
*/
--正常情况不用存,取得的时候计算一下就可以了。
select a,b,a-b as c from tablename
#6
怎么用?
#7
CREATE TABLE test
(
a INT,
b INT,
c AS a-b
)
INSERT INTO test(a,b)
VALUES (2,1)
SELECT * FROM test
/*
a b c
----------- ----------- -----------
2 1 1
*/
#8
视图设计怎么设计
#9
为啥要用视图?如果非要用视图,假设你的表只有a、b列
create view test
as
select a,b,a-b as c
from test
create view test
as
select a,b,a-b as c
from test
#10
已经设置好了谢谢啊