SQL Check constraint on column referencing other columns - sql

I want to limit a column that it can only have a value when another column has a value.
example: (this doesn't work)
create table testConstraint (
col1 int not null identity(1, 1) primary key,
col2 int,
col3 int check (col2 is not null),
col4 int)
This is not possible because he cannot reference another column.
Error:
Column CHECK constraint for column 'col3' references another column,
table 'testConstraint'.
Another try was: (also doesn't work)
create table testConstraint (
col1 int not null identity(1, 1) primary key,
col2 int,
col3 int,
col4 int)
GO
alter table testConstraint add constraint ck_columnNotNull check (case when col2 is null then col3 is null end)
GO
Anyone have an idea how this would be possible with a constraint?

You can write a trigger.
Also, you can try this
(1)
ALTER TABLE TestConstraint ADD CONSTRAINT
CK_TestConstraint CHECK (NOT ( (col3 is not null) and (col2 is null) ))
GO
or this
(2)
ALTER TABLE TestConstraint ADD CONSTRAINT
CK_TestConstraint CHECK
(
((col3 is not null) and (col2 is not null)) or
((col3 is null) and (col2 is null))
)
GO
depending on what exactly you need.
I just tested it and it works OK, I think.
insert into
TestConstraint
(col2, col3, col4)
values
(null, 1, 2)
-- ERROR
insert into
TestConstraint
(col2, col3, col4)
values
(1, 1, 2)
-- OK

ALTER TABLE testConstraint
ADD CONSTRAINT ck_columnNotNull
CHECK ( 1 = CASE
WHEN col2 IS NULL AND col3 IS NULL THEN 1
WHEN col2 IS NOT NULL AND col3 IS NOT NULL THEN 1
ELSE 0
END)

Only simple logic is required, plus it needs (as per your second attempt) to be a table check constraint, so you can't declare it inline with the declaration of col3:
create table testConstraint (
col1 int not null identity(1, 1) primary key,
col2 int,
col3 int,
col4 int)
GO
alter table testConstraint add constraint ck_columnNotNull check (
col3 is null
or col2 is not null
)
GO
If col3 is null, then we don't care what the value of col2 is. Conversely, if it's not NULL, then we do want to enforce the col2 isn't null. That's what the two sides of the or effectively give us.

Related

How to select more columns than insert column

I have the following query:
INSERT INTO TableA (Col1, Col2, Col3)
OUTPUT #SomeData, INSERTED.ID, ID INTO TableB(SomeColumn, TableAID, ID)
SELECT Col1, Col2, Col3, ID
FROM TableC;
When I run it, I get this error:
The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
That error makes sense, but I don't know how to fix it. I want to select 4 columns from TableC, but I want to only insert three of them (Col1, Col2, Col3) into TableA. I am selecting the column ID because I want to insert it into the ID column of TableB. Is there any way to do that?
CREATE TABLE TableA
(
ID bigint identity
constraint PK_TableA_ID
primary key
Col1 int,
Col2 int,
Col3 int
)
CREATE TABLE TableB
(
ID [int] NOT NULL,
SomeColumn [int] NOT NULL,
TableAID [bigint] NOT NULL
);
CREATE TABLE TableC
(
ID [int] IDENTITY (1,1) NOT NULL,
Col1 [int],
Col2 [int],
Col3 [int],
);
You can try merge and write something like this:
MERGE TableA AS target
USING TableC AS source
ON 1 = 0
WHEN NOT MATCHED BY target
THEN INSERT (Col1, Col2, Col3) VALUES (source.Col1, source.Col2, source.Col3)
OUTPUT #SomeData, INSERTED.ID, source.ID INTO TableB (SomeColumn,TableAID, ID);

how we can Create Primary Keys in a table

How can I create a Primary Key in a table. I used the following command to create one, but the key doesn't appear to be enforced:
ALTER TABLE tablename ADD PRIMARY KEY (column1,column3);
Snowflake supports defining and maintaining constraints, but does not enforce them, except for NOT NULL constraints, which are always enforced.
https://docs.snowflake.com/en/sql-reference/constraints-overview.html#supported-constraint-types
Although the primary key is not enforced in Snowflake, you can use the MERGE statement to insert the data to enforce uniqueness of that key. This approach requires making sure that the source data set does not include duplicates, by using for example QUALIFY function.
Example:
CREATE TABLE IF NOT EXISTS test (
col1 VARCHAR(50) NOT NULL,
col2 VARCHAR(50),
col3 VARCHAR(50) NOT NULL,
constraint PK_test primary key (col1, col3) not enforced
);
MERGE INTO TEST AS tgt
USING (
SELECT COL1, COL2, COL3 FROM (
SELECT 'a' COL1, 'b' COL2, 'a' COL3
UNION
SELECT 'a' COL1, 'c' COL2, 'a' COL3
UNION
SELECT 'a' COL1, 'd' COL2, 'a' COL3)
QUALIFY row_number() over (partition by COL1, COL3 order by COL2) = 1
)AS src
ON (tgt.COL1=src.COL1 AND tgt.COL3=src.COL3)
WHEN NOT MATCHED
THEN INSERT (COL1, COL2, COL3)
VALUES (src.COL1, src.COL2, src.COL3);
Only the row a,b,a was inserted.

Can CHECK constraints act like if else?

I have a table with 4 columns:
(ID (PK, int, NOT NULL), col1 (NULL), col2 (NULL), col3 (NULL))
I'd like to add a CHECK constraint (table-level I think?) so that:
if col1 OR col2 are NOT NULL then col3 must be NULL
and
if col3 is NOT NULL then col1 AND col2 must be NULL
i.e. col3 should be null if col1 and col2 are not null or vice-versa
I am very new to SQL and SQL server though and am not sure how to actually implement this or even if it can/should be implemented?
I think maybe:
CHECK ( (col1 NOT NULL OR col2 NOT NULL AND col3 NULL) OR
(col3 NOT NULL AND col1 NULL AND col2 NULL) )
But I am not sure if the brackets can be used to group the logic like this? If not, how can this best be implemented?
Absolutely, you can do this. See this sqlfiddle.
However, you need to make sure you bracket your logic properly. You should never mix ANDs and ORs in the same bracketing scope. So:
(col1 NOT NULL OR col2 NOT NULL AND col3 NULL)
Needs to become:
((col1 NOT NULL OR col2 NOT NULL) AND col3 NULL)
Or:
(col1 NOT NULL OR (col2 NOT NULL AND col3 NULL))
Depending on your intent.
Just be careful not to make mistake with brackets.
CREATE TABLE Test1 (col1 INT, col2 INT, col3 INT);
ALTER TABLE Test1
ADD CONSTRAINT CHK1
CHECK (((col1 IS NOT NULL OR col2 IS NOT NULL) AND col3 IS NULL) OR
((col1 IS NULL AND col2 IS NULL) AND col3 IS NOT NULL))
INSERT INTO Test1 VALUES (1,1,1); --fail
INSERT INTO Test1 VALUES (1,1,NULL); --good
INSERT INTO Test1 VALUES (1,NULL,NULL); --good
INSERT INTO Test1 VALUES (1,NULL,1); --fail
INSERT INTO Test1 VALUES (NULL,NULL,1); --good
I would say create a UDF like below
create FUNCTION dbo.fn_check_val
(#col1 int , #col2 int , #col3 int)
RETURNS bit
AS
BEGIN
declare #toRet bit
IF(#col1 is Not null OR #col2 is NOT NULL)
Begin
if(#col3 is null)
Begin
Set #toRet = 1
End
Else
Begin
Set #toRet = 0
End
End
Else
if(#col3 is not null)
Begin
Set #toRet = 1
End
Else
Begin
Set #toRet = 0
End
return #toRet
END
and then add following check statement in your table
([dbo].[fn_check_val]([col1],[col2],[col3])=(1))

SQL Column 2 change from NULL to NOT NULL if the value of Column 1 is 1

I have an sql table with let say col1 and col2, i want to create a constraint or a trigger (whatever works) such that col2 should change from NULL to Not Null if and only if the value entered in col1 is 1.
The point is, i want to make a col2 field mandatory if col1 is set to 1 otherwise remain optional.
You mention SQL Server in a comment
CREATE TABLE YourTable
(
Col1 INT,
Col2 VARCHAR(25) NULL,
CONSTRAINT ck_foo CHECK (NOT (Col1 = 1 AND Col2 IS NULL))
);
would disallow NULL as stated in the question. To also disallow empty strings the constraint definition could be
CONSTRAINT ck_foo CHECK (NOT (Col1 = 1 AND ISNULL(Col2,'') = ''))
ALTER TABLE YourTable WITH CHECK ADD CONSTRAINT [CK_YourTable] CHECK (([col1]=(1) AND [col2] IS NOT NULL OR [col1]<>(1)))
GO
If you want to exclude empty string too then:
ALTER TABLE YourTable WITH CHECK ADD CONSTRAINT [CK_YourTable] CHECK (([col1]=(1) AND ( [col2] IS NOT NULL AND [col2] <> '') OR [col1]<>(1)))
GO

SQL-Multiple Insert into identity table

I need to to do a insert from a table with the following structure:
Table A
Col1 Col2 Col3 Col4
intID1 intID2 intID3 intID4
I need to select the rows from the above table that are null
for col1,col2,col3 and insert those rows into a table that will generate an identity
row that I need to use to insert into another table.I am not sure of the
sql statement or the general method used to select those rows and insert them multiple times and retrieve the identity id one by one to insert into the next table.
Any help is greatly appreciated!
Sample process:
Table A
Col1 Col2 Col3 Col4
1 3 7 null
null null null 45
null null null 67
1)Retrieve rows 2 and 3
2)Insert 2 and 3 into another table to retrieve identity id for both rows
3)Insert identities from step 2 into another table
Venk covered step 1 and 2 I think. For 3 can use the OUPUT clause to retrieve the identity value from set operation.
Get Identity of multiple insertion in sql server 2008
INSERT INTO TABLEB(Col1,Col2,Col3,Col4)
SELECT * FROM TABLEA WHERE Col1 is NULL AND Col2 is NULL AND Col3 is NULL;
Sounds like you need the output operator:
declare #TableA table(Col1 int, Col2 int, Col3 int, Col4 int);
declare #TableB table(id int identity(1,1), Col1 int, Col2 int, Col3 int, Col4 int);
declare #Audit table(id int);
insert into #TableA
select 1,3,7,null union all
select null, null, null, 45 union all
select null, null, null, 67;
-- copy null columns from #TableA to #TableB
-- and output id's to #Audit
insert into #TableB
output inserted.id
into #Audit
select *
from #TableA
where Col1 is null
and Col2 is null
and Col3 is null;
-- Copied #TableB values and #Audit values
select * from #TableB;
select * from #Audit;