Showing posts with label define. Show all posts
Showing posts with label define. Show all posts

Tuesday, February 14, 2012

Constraints in SQL server

I wanna know how to define constraints in sql server.
everytime i try something it says "error validating constraint'

does someone have an example how a constraint have to look like?

PS: i want to the constraint that check's this:
i have an order.. an order haves orderlines
and we have a 'contract' .. and contract have 'contractlines'

.. an order can became a 'contract' .. and some of the orderlines will be 'contractlines'

so a contract have a FK:orderID.. and a contractline have FK:contractID&orderlineID

i want to check if the orderlineID in contractline exists in the correspondending order..

>>>something like:
orderlineID exists in ( select orderlineID from orderline left join
order on order.orderID = orderLine.orderID left join contract on
contract.orderID = order.orderID left join contract on contractLine.contractID = contract.contractIDIt sounds like you want foreign key constraints:


create table Orders
(
OrderID int not null,
primary key (OrderID),
)
create table OrderLines
(
OrderID int not null,
OrderLineID int not null,
primary key (OrderID, OrderLineID),
)
create table Contracts
(
ContractID int not null,
OrderID int null,
primary key (ContractID),
foreign key (OrderID) references Orders(OrderID),
)
create table ContractLines
(
ContractID int not null,
ContractLineID int not null,
OrderID int null,
OrderLineID int null,
primary key (ContractID, ContractLineID),
foreign key (OrderID, OrderLineID) references OrderLines(OrderID, OrderLineID),
)

I don't think this is a very good design for several reasons, including the following:
1) I used nulls
2) I have no idea if the various IDs are natural or surrogate keys
3) I assumed a lot of things about how the four concepts orders, contracts, order lines and contract lines are related

Friday, February 10, 2012

constant

How do you define a constant in trigger definition? Thanks.
There are no constants in SQL Server. Use DECLARE to declare variables and
SET to assign them.
Don't try to assign column values to variables in a trigger. Doing so forces
you to write cursor-like code to cope with multiple row updates. Try to
write set-based code in your triggers instead.
David Portas
SQL Server MVP
|||Allen
I am not sure why do you need to do that?
There is no CONSTANS in SQL Server , probably you are talking about
DEFAULT.
If you insist , try somethin like that
DECLARE @.const INT
SET @.const=<VALUE>
""Allen Iverson"" <no_spam@.bk.com> wrote in message
news:eNV3onO4EHA.1452@.TK2MSFTNGP11.phx.gbl...
> How do you define a constant in trigger definition? Thanks.
>
|||Uri,
Actually what I want to use it for is like this. I wrote a
temporarily table name for testing and later I need to change it to the real
one. I am thinking if I can define a constant table name so that I can just
change it in one place instead of replacing all over the trigger definition.
Can you please help? Thanks.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:OYxr1GP4EHA.1524@.TK2MSFTNGP09.phx.gbl...
> Allen
> I am not sure why do you need to do that?
> There is no CONSTANS in SQL Server , probably you are talking about
> DEFAULT.
> If you insist , try somethin like that
> DECLARE @.const INT
> SET @.const=<VALUE>
>
> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
> news:eNV3onO4EHA.1452@.TK2MSFTNGP11.phx.gbl...
>
|||Why not do your testing in a separate database so that you can keep the same
object names? Surely you don't test this in a production database?
Other than that, search-and-replace is the best I can suggest.
David Portas
SQL Server MVP

constant

How do you define a constant in a trigger?
Hi,
Like a normal constant in stored procedure.
create trigger ins_tri on x for insert
as
begin
declare @.val int
set @.val = 1
insert into y(i) values(1)
end
Thanks
Hari
SQL Server MVP
" -00Eric Clapton" <a@.b.com> wrote in message
news:OfWRriZuFHA.464@.TK2MSFTNGP15.phx.gbl...
> How do you define a constant in a trigger?
>

constant

How do you define a constant in a trigger?Hi,
Like a normal constant in stored procedure.
create trigger ins_tri on x for insert
as
begin
declare @.val int
set @.val = 1
insert into y(i) values(1)
end
Thanks
Hari
SQL Server MVP
" -00Eric Clapton" <a@.b.com> wrote in message
news:OfWRriZuFHA.464@.TK2MSFTNGP15.phx.gbl...
> How do you define a constant in a trigger?
>

constant

How do you define a constant in a trigger?Hi,
Like a normal constant in stored procedure.
create trigger ins_tri on x for insert
as
begin
declare @.val int
set @.val = 1
insert into y(i) values(1)
end
Thanks
Hari
SQL Server MVP
" -00Eric Clapton" <a@.b.com> wrote in message
news:OfWRriZuFHA.464@.TK2MSFTNGP15.phx.gbl...
> How do you define a constant in a trigger?
>

constant

How do you define a constant in trigger definition? Thanks.There are no constants in SQL Server. Use DECLARE to declare variables and
SET to assign them.
Don't try to assign column values to variables in a trigger. Doing so forces
you to write cursor-like code to cope with multiple row updates. Try to
write set-based code in your triggers instead.
--
David Portas
SQL Server MVP
--|||Allen
I am not sure why do you need to do that?
There is no CONSTANS in SQL Server , probably you are talking about
DEFAULT.
If you insist , try somethin like that
DECLARE @.const INT
SET @.const=<VALUE>
""Allen Iverson"" <no_spam@.bk.com> wrote in message
news:eNV3onO4EHA.1452@.TK2MSFTNGP11.phx.gbl...
> How do you define a constant in trigger definition? Thanks.
>|||Uri,
Actually what I want to use it for is like this. I wrote a
temporarily table name for testing and later I need to change it to the real
one. I am thinking if I can define a constant table name so that I can just
change it in one place instead of replacing all over the trigger definition.
Can you please help? Thanks.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:OYxr1GP4EHA.1524@.TK2MSFTNGP09.phx.gbl...
> Allen
> I am not sure why do you need to do that?
> There is no CONSTANS in SQL Server , probably you are talking about
> DEFAULT.
> If you insist , try somethin like that
> DECLARE @.const INT
> SET @.const=<VALUE>
>
> ""Allen Iverson"" <no_spam@.bk.com> wrote in message
> news:eNV3onO4EHA.1452@.TK2MSFTNGP11.phx.gbl...
>> How do you define a constant in trigger definition? Thanks.
>>
>|||Why not do your testing in a separate database so that you can keep the same
object names? Surely you don't test this in a production database?
Other than that, search-and-replace is the best I can suggest.
--
David Portas
SQL Server MVP
--