SQL Server constraint to check other columns - sql

Product: SQL Server
Is it possible to write a constraint, that checks the values of other columns? In my specific case I will give you an example:
Let's imagine I have a table with 5 Columns:
Name | Hobby1 | Hobby2 | Hobby3 | Hobby4
Lets say there are the following values in it:
John Doe| fishing | reading| swimming| jogging
What I try to reach is the following:
If someone trys to Insert : John Doe, fishing,reading
It should be blocked, cause I don't want the same combination in the first 3 Columns.
Can I realise that with a constraint or do I need a Primary key combination for the first 3 columns?
Thanks for your replies.

Add unique constraint to your table for first three columns.
ALTER TABLE YourTableName
ADD CONSTRAINT UK_Name_Hobby1_Hobby2 UNIQUE (Name, Hobby1,Hobby2);

Well, you could do as #jarlh says (in comments) and ensure the hobby columns are ordered and this will satisfy your requirements:
CREATE TABLE Hobbies
(
Name VARCHAR(35) NOT NULL,
Hobby1 VARCHAR(35) NOT NULL,
Hobby2 VARCHAR(35) NOT NULL,
Hobby3 VARCHAR(35),
Hobby4 VARCHAR(35),
CHECK ( Hobby1 < Hobby2 ),
CHECK ( Hobby2 < Hobby3 ),
CHECK ( Hobby3 < Hobby4 ),
UNIQUE ( Name, Hobby1, Hobby2 ),
);
...However, this would allow the following which feels like it should be invalid data:
INSERT INTO Hobbies VALUES ( 'John Doe', 'fishing', 'jogging', 'reading', 'swimming' );
INSERT INTO Hobbies VALUES ( 'John Doe', 'jogging', 'reading', 'swimming', NULL );

Related

How to write case expression while pivoting the data

CREATE TABLE details_1 (
e_id NUMBER(10),
e_name VARCHAR2(30),
CONSTRAINT pk_details_1_e_id PRIMARY KEY ( e_id )
);
insert into details_1 values(11,'A');
CREATE TABLE ques_ans (
ques_ans_id NUMBER(10),
ref_ques_id NUMBER(10),
ref_ans_id NUMBER(10),
ref_ans_value VARCHAR2(100),
e_id NUMBER(10),
CONSTRAINT pk_ques_ans PRIMARY KEY ( ques_ans_id ),
CONSTRAINT fk_ques_ans FOREIGN KEY ( e_id )
REFERENCES details_1 ( e_id ),
constraint fk_ques_and_ques_id foreign key(ref_ques_id)
references ques_ref (ques_id)
);
insert into ques_ans values(1,3,1,11,null);
insert into ques_ans values(2,2,2,11,null);
insert into ques_ans values(3,4,1,11,null);
insert into ques_ans values(4,23,1,11,11);
CREATE TABLE ques_ref (
ques_id NUMBER(10),
code VARCHAR2(50),
code_label VARCHAR2(100),
constraint pk_ques_ref primary key(ques_id)
);
insert into ques_ref values(3,'changes_exist','Any known changes');
insert into ques_ref values(2,'E_Clubbed','E_id clubbed with other');
insert into ques_ref values(4,'E_impacted','E impacted by other');
insert into ques_ref values(23,'E_Clubbed_with_other','E clubbed with other E');
CREATE TABLE ans_ref (
ref_ans_id NUMBER(10),
code VARCHAR2(10),
code_value VARCHAR2(30)
);
insert into ans_ref values(1,'R_Yes','Yes');
insert into ans_ref values(2,'R_No','No');
commit;
My Attempt :
select d.e_id,
max(case qa.ref_ques_id when 3 then ar.code_value end) changes_exist,
max(case qa.ref_ques_id when 2 then ar.code_value end) E_Clubbed,
max(case qa.ref_ques_id when 4 then ar.code_value end) E_impacted,
--need to write case expression here
from details_1 d
join
ques_ans qa
on d.e_id = qa.e_id
join ans_ref ar
on ar.ref_ans_id = qa.ref_ans_id
group by d.e_id
I got stuck in the below requirement:
I need to check if ref_ques_id of ques_ans table is 23 then it should display ref_ans_value from the same table i.e ques_ans
For example:
In the table ques_ans for ques_ans_id 4 ref_ques_id is 23 then in this case it will display ref_ans_value i.e 11 in the column ref_ans_value
How can I write case expressions while pivoting the data? I am wondering if we can do it using case expression or is there any other way to achieve this?
Expected Output:
+------+---------------+-----------+------------+------------------+
| E_ID | CHANGES_EXIST | E_CLUBBED | E_IMPACTED | E_CLUBBED_WITH_E |
+------+---------------+-----------+------------+------------------+
| 11 | Yes | No | Yes | 11 |
+------+---------------+-----------+------------+------------------+
You can achieve that with further aggregations.
CASE
WHEN MAX(ques_ans.ref_ques_id) = '23' THEN MAX(COALESCE(ques_ans.ref_ans_value, 0))
ELSE -4
END
There are two problems you will need to sort out:
1
You artificially aggregated things in the SELECT clause to prevent technical problems, but you might need to change this, depending on how your actual data looks alike (not the one shown in the question, but in reality). If you have answers with different references, then you might want to change your database structure to better reflect reality.
2
You did not tell us what should happen when the value of ques_id is NOT 23, so I added an ad-hoc value in the ELSE. You will need to change it to the one you actually need.

How to automatically generate unique id in SQL like UID12345678?

I want to automatically generate unique id with per-defined code attach to it.
ex:
UID12345678
CUSID5000
I tried uniqueidentifier data type but it generate a id which is not suitable for a user id.
Any one have suggestions?
The only viable solution in my opinion is to use
an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value
a computed, persisted column to convert that numeric value to the value you need
So try this:
CREATE TABLE dbo.tblUsers
(ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
UserID AS 'UID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED,
.... your other columns here....
)
Now, every time you insert a row into tblUsers without specifying values for ID or UserID:
INSERT INTO dbo.tblUsersCol1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)
then SQL Server will automatically and safely increase your ID value, and UserID will contain values like UID00000001, UID00000002,...... and so on - automatically, safely, reliably, no duplicates.
Update: the column UserID is computed - but it still OF COURSE has a data type, as a quick peek into the Object Explorer reveals:
CREATE TABLE dbo.tblUsers
(
ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
UserID AS 'UID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED,
[Name] VARCHAR(50) NOT NULL,
)
marc_s's Answer Snap
Reference:https://learn.microsoft.com/en-us/sql/t-sql/functions/newid-transact-sql?view=sql-server-2017
-- Creating a table using NEWID for uniqueidentifier data type.
CREATE TABLE cust
(
CustomerID uniqueidentifier NOT NULL
DEFAULT newid(),
Company varchar(30) NOT NULL,
ContactName varchar(60) NOT NULL,
Address varchar(30) NOT NULL,
City varchar(30) NOT NULL,
StateProvince varchar(10) NULL,
PostalCode varchar(10) NOT NULL,
CountryRegion varchar(20) NOT NULL,
Telephone varchar(15) NOT NULL,
Fax varchar(15) NULL
);
GO
-- Inserting 5 rows into cust table.
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
PostalCode, CountryRegion, Telephone, Fax)
VALUES
(NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
'90110', 'Finland', '981-443655', '981-443655')
,(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
'08737-363', 'Brasil', '(14) 555-8122', '')
,(NEWID(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,
'1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')
,(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
'8010', 'Austria', '7675-3425', '7675-3426')
,(NEWID(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,
'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68');
GO
If you want to add the id manually you can use,
PadLeft() or String.Format() method.
string id;
char x='0';
id=id.PadLeft(6, x);
//Six character string id with left 0s e.g 000012
int id;
id=String.Format("{0:000000}",id);
//Integer length of 6 with the id. e.g 000012
Then you can append this with UID.
The 'newid()' method unique id generate for per record.
AddColumn("dbo.Foo", "Key", c => c.String(nullable: false, maxLength: 250, defaultValueSql: "newid()"));
Table Creating
create table emp(eno int identity(100001,1),ename varchar(50))
Values inserting
insert into emp(ename)values('narendra'),('ajay'),('anil'),('raju')
Select Table
select * from emp
Output
eno ename
100001 narendra
100002 rama
100003 ajay
100004 anil
100005 raju

EAV Select query from spreaded value tables

I have the following SQL Server database structure I have to use to query data. The model could be wrong; I appreciate arguments if that's the case so I can ask for changes. If not, I need a query to get tabbed data in the format I will detail below.
The structure goes like this:
CLIENTS:
ClientID ClientName
-----------------------
1 James
2 Leonard
3 Montgomery
ATTRIBUTES:
AttributeID AttributeName
-----------------------------
1 Rank
2 Date
3 Salary
4 FileRecordsAmount
ATTRIBUTES_STRING:
ClientID AttributeID AttributeStringValue
1 1 Captain
2 1 Chief Surgeon
3 1 Chief Engineer
ATTRIBUTES_NUMERIC:
ClientID AttributeID AttributeNumericValue
1 4 187
2 4 2
3 4 10
The result I need would be the following:
RESULTS:
----------------------------------------------------------
ClientID ClientName Rank FileRecordsAmount
1 James Captain 187
2 Leonard Chief Surgeon 2
3 Montgomery Chief Engineer 10
How can I achieve this?
Thank you very much!
EDIT: The challenging issue here (for me) is that the attributes are dynamic... I have 5 tables of attributes (ATTRIBUTES_STRING, ATTRIBUTES_NUMERIC, ATTRIBUTES_DATE, ATTRIBUTES_BIT, ATTRIBUTES_INT) and the user should be able to set up it's own attributes.
You need an SQL join. It will look something like this:
select
CLIENTS.ClientID,
CLIENTS.ClientName,
ATTRIBUTES_STRING1.AttributeStringValue as Rank,
ATTRIBUTES_NUMERIC2.AttributeNumericValue as FileRecordsAmount
from
CLIENTS,
ATTRIBUTES ATTRIBUTES1,
ATTRIBUTES ATTRIBUTES2,
ATTRIBUTES_STRING ATTRIBUTES_STRING1,
ATTRIBUTES_NUMERIC ATTRIBUTES_NUMERIC2
where CLIENTS.ClientID = ATTRIBUTES_STRING1.ClientID
and CLIENTS.ClientID = ATTRIBUTES_NUMERIC2.ClientID
and ATTRIBUTES_STRING1.AttributeID = ATTRIBUTES1.AttributeID
and ATTRIBUTES_NUMERIC2.AttributeID = ATTRIBUTES2.AttributeID
and ATTRIBUTES1.AttributeName = 'Rank'
and ATTRIBUTES2.AttributeName = 'FileRecordsAmount'
;
Here is the SQL Fiddle for reference. This is my first EAV schema so I wouldn't put too much trust in it :)
Edit: Schema provided below for reference:
create table CLIENTS (
ClientID integer primary key,
ClientName varchar(50) not null
);
insert into CLIENTS values (1,'James');
insert into CLIENTS values (2,'Leonard');
insert into CLIENTS values (3,'Montgomery');
create table ATTRIBUTES (
AttributeID integer primary key,
AttributeName varchar(50) not null
);
create index ATTRIBUTE_NAME_IDX on ATTRIBUTES (AttributeName);
insert into ATTRIBUTES values (1,'Rank');
insert into ATTRIBUTES values (2,'Date');
insert into ATTRIBUTES values (3,'Salary');
insert into ATTRIBUTES values (4,'FileRecordsAmount');
create table ATTRIBUTES_STRING (
ClientID integer,
AttributeID integer not null,
AttributeStringValue varchar(255) not null,
primary key (ClientID, AttributeID)
);
insert into ATTRIBUTES_STRING values (1,1,'Captain');
insert into ATTRIBUTES_STRING values (2,1,'Chief Surgeon');
insert into ATTRIBUTES_STRING values (3,1,'Chief Engineer');
create table ATTRIBUTES_NUMERIC (
ClientID integer,
AttributeID integer not null,
AttributeNumericValue numeric(10, 5) not null,
primary key (ClientID, AttributeID)
);
insert into ATTRIBUTES_NUMERIC values (1,4,187);
insert into ATTRIBUTES_NUMERIC values (2,4,2);
insert into ATTRIBUTES_NUMERIC values (3,4,10);
Edit: Modified the select to make it easier to extend with extra attributes

How to insert a record into a table with a column declared with the SERIAL function

My database is using PostgreSQL. One table is using the serial auto-increment macro. If I want to insert a record into the table, do I still need to specify that value, or it is be automatically assigned for me?
CREATE TABLE dataset
(
id serial NOT NULL,
age integer NOT NULL,
name character varying(32) NOT NULL,
description text NOT NULL DEFAULT ''::text
CONSTRAINT dataset_pkey PRIMARY KEY (id)
);
Using the DEFAULT keyword or by omitting the column from the INSERT list:
INSERT INTO dataset (id, age, name, description)
VALUES (DEFAULT, 42, 'fred', 'desc');
INSERT INTO dataset (age, name, description)
VALUES (42, 'fred', 'desc');
If you create a table with a serial column then if you omit the serial column when you insert data into the table PostgreSQL will use the sequence automatically and will keep the order.
Example:
skytf=> create table test_2 (id serial,name varchar(32));
NOTICE: CREATE TABLE will create implicit sequence "test_2_id_seq" for serial column "test_2.id"
CREATE TABLE
skytf=> insert into test_2 (name) values ('a');
INSERT 0 1
skytf=> insert into test_2 (name) values ('b');
INSERT 0 1
skytf=> insert into test_2 (name) values ('c');
INSERT 0 1
skytf=> select * From test_2;
id | name
----+------
1 | a
2 | b
3 | c
(3 rows)
These query work for me:
insert into <table_name> (all columns without id serial)
select (all columns without id serial)
FROM <source> Where <anything>;
Inserting multiple rows wasn't working for me in this scenario:
create table test (
id bigint primary key default gen_id(),
msg text not null
)
insert into test (msg)
select gs
from generate_series(1,10) gs;
because I had mistakenly marked my gen_id function IMMUTABLE.
The insert query was being optimized to only call that function once rather than 10 times. Oops...
For example, you create "person" table with "id" of serial and "name" as shown below:
CREATE TABLE person (
id serial PRIMARY KEY,
name VARCHAR(50)
)
Then, you can use DEFAULT for "id" of serial and insert rows without column(field) names as shown below:
INSERT INTO person VALUES (DEFAULT, 'John'), (DEFAULT, 'Tom');
postgres=# SELECT * FROM person;
id | name
----+------
1 | John
2 | Tom

rows that exclude each other in create table statement

I have to create a table, with either first name and last name of a person, or a name of an organization. There has to be exactly one of them. For example one row of the table is -
first_name last_name organization
---------- --------- ------------
John Smith null
or another row can be -
first_name last_name organization
---------- --------- --------------------
null null HappyStrawberry inc.
Is there a way to define this in SQL language? Or should I just define all three columns being able to get null values?
Your situation is a classical example of what some ER dialects call "entity subtyping".
You have an entity called "Person" (or "Party" or something of that ilk), and you have two ditinct sub-entities called "NaturalPerson" and "LegalPerson", respectively.
The canonical way to model ER entity subtypes in a relational database is using three tables : one for the "Person" entity with all columns that are "common" for both NaturalPerson and LegalPerson (i.e. that exist for Persons, regardless of their type), and one per identified sub-entity holding all the columns that pertain to that sub-entity in particular.
You can read more on this in Fabian Pascal, "Practical Issues in Database Management".
You could use a check constraint, like:
create table YourTable (
col1 varchar(50)
, col2 varchar(50)
, col3 varchar(50)
, constraint TheConstraint check ( 1 =
case when col1 is null then 1 else 0 end +
case when col2 is null then 1 else 0 end +
case when col3 is null then 1 else 0 end )
)
Another way is to add a type column (EAV method):
create table YourTable (
type varchar(10) check (type in ('FirstName', 'LastName', 'Organisztion')
, value varchar(50))
insert YourTable ('LastName', 'Obama')
insert YourTable ('FirstName', 'Barrack')
insert YourTable ('Orginazation', 'White House')
You can do this using a constraint:
CREATE TABLE [dbo].[Contact](
[first_name] [varchar](50) NULL,
[last_name] [varchar](50) NULL,
[organization] [varchar](50) NULL,
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Contact] WITH CHECK ADD CONSTRAINT [CK_Contact] CHECK (([first_name] IS NOT NULL OR [last_name] IS NOT NULL OR [organization] IS NOT NULL))
GO
ALTER TABLE [dbo].[Contact] CHECK CONSTRAINT [CK_Contact]
GO
The CK_Contact constraint ensures that at least one value was entered.