Relationship between tables in ms sql server [closed] - sql-server

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
A patient can select many tests, and a test can be selected by many patient.
Then what would be the structure of these tables and how the relationship can be established between them?

your table structure should be below
PatientTable
PatientId int Primary Key,
PatientName varchar(50),
EmailId varchar(50)
Password varchar(50)
TestTable
TestId int Primary key,
TestName varchar(50)
PatientTestTable
PatientId int FK(PatientTable)
TestId int FK (TestTable)
This way you can give relationship to two tables. you need to understand funamental of RDBMS.

You will probably need 3 tables, Patient table, Test Table and PatientTest Table
with PatientID as foreign key fro Patient table and TestId as foreign key from Test table and you can add any other column ( like TestDatetime, TestResult ...)

Related

SQL Server modifying XML columns with unique value for all the rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a table with a xml column, I need to modify a few fields in the XML for every row. The new values should be unique to that entry, I want to do that by appending the primary key (ID int) to the value in the fields.
For instance: I inserted the rows into a temp table. For simplicity, assume the table only has two columns: payload (XML), id (INT, primary key)
UPDATE #Temp
SET payloadXml.modify('replace value of (/abc/customer/contact/name[#part=("first")]/text())[1] with "hello"')
FROM #Temp
This will replace the first name field with hello. But I want to append the id of that row to this field. I tried sql:variable, but to no avail. Is this even possible?
You can use sql:column to reference a relational column in your XML DML, eg
drop table if exists #temp
go
create table #temp(id int identity primary key, payloadXML xml)
go
insert into #temp(payloadXml) values ('<abc><customer><contact><name part="first">foo</name></contact></customer></abc>');
insert into #temp(payloadXml) values ('<abc><customer><contact><name part="first">foo</name></contact></customer></abc>');
insert into #temp(payloadXml) values ('<abc><customer><contact><name part="first">foo</name></contact></customer></abc>');
WITH q as
(
select *, concat('hello ', id) new_name
from #Temp
)
UPDATE q
SET payloadXml.modify('replace value of (/abc/customer/contact/name[#part=("first")]/text())[1]
with sql:column("new_name")')
select payloadXML from #temp

Dimension attribute with one-to-many relationship [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a request for an attribute in a dimension that has a one-to-many relationship with the lower level of the dimension.
Here is the case:
Dimension: Employee
Attribute: Immatriculation
I have 3 source tables : Employee, Language and LanguageLevel.
One employee can have multiple immatriculation codes.
Each immatriculation has a number, start_date, expiration_date.
How can I model this case ?
You need to create Immatriculation-Dimension and use a foreign key on the many side (Immatriculation) of the relationship that linkes back to the one side(Employee). This gives Primary Key- Foreign Key relationship.
Here's a similar case. One book can have multiple authors (in order for this to be a one-to-many relationship one author can only be related to one book. Otherwise it would be a many-to-many relationship..):
CREATE TABLE dbo.Book
(
Pk_Book_Id INT PRIMARY KEY,
Name VARCHAR(255),
ISBN VARCHAR(255)
);
CREATE TABLE dbo.Author
(
Pk_Author_Id INT PRIMARY KEY,
FullName VARCHAR(255),
MobileNo CHAR(10),
Fk_Book_Id INT FOREIGN KEY REFERENCES Book(Pk_Book_Id)
);
INSERT INTO Book VALUES (1, 'Let is Snow', 'ISBN3030303');
INSERT INTO Book VALUES (2, 'Three Cups of Tea','ISBN638242');
GO
INSERT INTO dbo.Author VALUES(100,'John Green','30303',1);
INSERT INTO dbo.Author VALUES(101,'Maureen Johnson','4343',1);
INSERT INTO dbo.Author VALUES(102,'Lauren Myracle','76665',1);
INSERT INTO dbo.Author VALUES(103,'Greg Mortenson','6434',2);
INSERT INTO dbo.Author VALUES(104,'David Oliver Relin','72322',2);
GO
SELECT * FROM dbo.Book;
SELECT * FROM dbo.Author;

PK and FK finding algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can an algorithm is written to find Primary Keys and Foreign Keys of a relation ?
Given parameters are name of relation, degree of relation and an array of the attributes(a primary key may include more than one attribute)
I think if an attribute is referenced than it is primary key. and the attirbute that references is FK
I would make a list of columns that contain no redundancies and no null cells as possible primary keys. I suppose one fast way to detect them would be to attempt to declare the column as a PK and see if there are errors. Another approach would be a group by:
select column_name, count(*) c from table_name
where column_name is not null
group by c
having c <> 1
If column_name of table_name is plausibly a primary key, the query above should produce no rows.
As for foreign keys, try this:
select column_name from table_name
except
select other_column from other_table
This should return an empty set if other_column of other_table has column_name of table_name as a foreign key.
As for automating the above tests over all the tables and each of their columns, I can't help there as my SQL vocabulary doesn't include Microsoft.
Note that passing the tests above is a necessary but not sufficient condition for a column to be a key. Determining which columns should be keys is as much a matter of intuition as algorithms, and if your starting point is taming a messy collection of raw data there may be columns that fail the tests but should nevertheless be keys.

Two ways of creating a table with primary key ... which is best practice (SQLSERVER 2014) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is there any difference at all with the following three scripts, and which is best practice?
CREATE TABLE Test1 (
TestID INT IDENTITY(1,1) NOT NULL,
Description NVARCHAR(50) NOT NULL,
CONSTRAINT [PK_AnyNameIFancy] PRIMARY KEY CLUSTERED
(
TestID ASC
)
) ON PRIMARY;
CREATE TABLE Test1 (
TestID INT NOT NULL PRIMARY KEY IDENTITY,
Description NVARCHAR(50) NOT NULL) ON PRIMARY
The only difference is that you are naming the constraint in the first one - that is a very good practice to follow.
They both end up with a clustered unique index.
For single column constraints I tend to write it on the one line...
TestID INT NOT NULL IDENTITY CONSTRAINT PK_AnyNameIFancy PRIMARY KEY CLUSTERED

Order of columns creation in SQL Server database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it important for any of the reasons if I create column A before column B in SQL Server database? No other actions are presumed in between.
See this:
Your Table Structure:
create table test (id int, name varchar(100));
Table of your customer:
create table test (name varchar(100), id int);
If you use:
insert into test values (10, 'Roger')
without specifying the order of fields in the table will cause your customer conversion error.

Resources