Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 15264

Beginner create table help

$
0
0

I am trying to create a table with the following columns:

id, descr, prop_1, prop_2, prop_3

I create a trigger on id to upper case it and set some rules for prop_1 through prop_4 as type int and where only one of the four can be 1 and all others in row must be null or all must be null. The problem is that after creating a row with all prop_* fields null, I cant update any to 1, but I can create the row and define a prop_ field to 1?

I also need to make sure that only one row can have a value of 1 in the table.

Any pointers would be greatly appreciated.
Thanks!

BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.my_table
	(
	id varchar(50) NOT NULL,
	descr varchar(50),
	prop_1 int,
	prop_2 int,
	prop_3 int,
	CONSTRAINT Type_CK CHECK(
		(prop_1 IS NOT NULL AND prop_2 IS NULL AND prop_3 IS NULL) OR
		(prop_1 IS NULL AND prop_2 IS NOT NULL AND prop_3 IS NULL) OR
		(prop_1 IS NULL AND prop_2 IS NULL AND prop_3 IS NOT NULL) OR
		(prop_2 IS NULL AND prop_2 IS NULL AND prop_3 IS NULL))
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.my_table SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
GO

-- Force id to UPPER
CREATE TRIGGER my_table_id_upper ON dbo.my_table FOR INSERT, UPDATE AS
UPDATE T SET T.id=Upper(T.id)
FROM dbo.my_table T
GO


Viewing all articles
Browse latest Browse all 15264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>