Add a Foreign Key to a column - sql-server

I have many tables for example EmployeeType, OrderType, AddressType... etc. these tables has only Id and Name, they look the same so I decided to use only 1 table to simplify the schema here you have the tables:
Type
------------
Id int Identity
EntityId id that represent the table check the table below
TypeId id of each table, EmployeeType, OrderType, etc.
Name Name of the type
Entity
------------
EntityId
Name
Here some example:
Entity table:
EntityId Name
1 EmployeeType
2 OrderType
Type table:
Id EntityId TypeId Name
1 1 1 Employee
2 1 2 Manager
Now I have the Employee table, this table has a TypeId, I need to be able to put a FK to Type table when my EntityId is equal to 1 (EmployeeType) is this possible?

Related

How do i update a column with specific values to reference the primary key ID of another table?

My goal is to normalize my table. My table before normalization is called Product Specifications, to normalize it I have two other tables called buyers_table and product_table
Table "Product Specifications"
Columns: Product_id , model_number, buyer
Buyers_table
Column: Buyer_id, buyer
Product_table
product_id, model_number, buyer_id
How do I insert the buyers from my product_specification table into my product_table but have the values show up as the buyer_id value instead of buyer?
Sorry I am new to this. This is a simplified version of the database that I have set up for my company.
use this design for database
Product_buyer: Columns:Id, Product_id , Buyer_id
Buyers_table: Column: Id, Buyername
Product_table: Id, Model_number
Product_Specifications: Id,Product_id,Productname,Price
then For insert data to database
First enter the information in the Product_table table
Retrieve the latest Product_table table record and enter Id as Product_id and the other of the information in the Product_Specifications table.
Enter the buyer information in the Buyers_table table when purchasing
Product_buyer
Create tables
create table Product_table
(
Id int identity,
Model_number int,
primary key(Id));
create table Product_Specifications
(Id int identity,
Product_id int,
Productname nvarchar(150),
Price int,
primary key(ID),
foreign key(Product_id) references Product_table);
//**********************
//and define other table
//......................
Retrieve product information
select Product_table.Id,Product_table.Model_number,Product_Specifications.Productname,Productname.Price
from Product_table join Product_Specifications
on Product_table.Id = Product_Specifications.Product_id
Retrieve Product_buyer information
select Product_table.Id,Product_table.Model_number,Product_Specifications.Productname,Productname.Price,Buyers_table.Id,Buyers_table.Buyername
from Buyers_table join Product_buyer on Buyers_table.Id = Product_buyer.Buyer_id
join Product_table on Product_buyer.Product_id = Product_table.Id
join Product_Specifications on Product_table.Id = Product_Specifications.Product_id

Combine two columns of a table in third column

I want to create a table wherein the 'Id' column is combined with 'Category' column and gets stored in 'UserId' column. Here is an example.
Id Category UserId
1 STD STD1
2 NMM NMM2
3 COV COV3
I have tried something like this :
Create table tblUsers
(
Id int identity primary key,
RegId as RIGHT('0000'+CAST([Id] as varchar(5)),5),
Category nvarchar(3),
UserId CONCAT(CategoryInitials,Id)
)
but this doesnt work. So, how does that work WHILE CREATING THIS TABLE?
May be like this:
Create table tblUsers
(
Id int identity primary key,
Category nvarchar(3),
UserId as (Category + cast(Id as varchar(10)))
)

There is at least one case were a null primary key is needed. How to treat this particular case?

I've a table SCHOOL_PARENT and a table SCHOOL_CHILD. The SCHOOL_CHILD table contains an id for the child and an id for the parent, that togheter forms the primary key. This is because a child can have two parents (mom and father, therefore 2 rows).
Exists then the case of a child without any parents. But I can't insert this row because the table SCHOOL_CHILD doesn't accept a NULL parent, being part of the primary key. How to solve this problem?
Not sure if I've understood the question properly, but I think your data model is something like:
Table: Parent
------------
ParentID (PK)
Name
....
Table: Child
------------
ChildID (PK)
Name
....
Table: Parent_Child
----------------
ParentID (PK)
ChildID (PK)
There is a many-to-many relationship between children and parents; you typically model this in a "bridging" table.
Table: SCHOOL_PARENT
------------
ParentID (PK)
Name
....
Table: SCHOOL_CHILD
------------
ChildID (PK)
MotherID (FK)
FatherID (FK)
Name
....
FatherID ant MotherID can be null.

Database design: best approach

I'm not a DBA and I don't know what is the best solution. I have two tables,
Custumers Table
CustomerId (primary key, identity)
...
and
Suppliers Table
SupplierId (primary key, identity)
...
and I want to store multiple telephone number and multiple emails. I thought to create two other tables, Emails and Telephones and use those in join with my Custumers and Suppliers, something like
Telephones Table
Id
UserId (reference to SuppliersId or CustomerId)
Value
...
But if I use as key for custumers and suppliers an Identity I'll have for sure problems. I'm thinking to do something like
Telephones Table
Id
SuppliersId
CustumersId
Value
...
But I don't know if is a good design. Any advice?
Thank you
One idea:
Entity (ID (PK), {common fields})
Customer (ID (PK), EntityID (FK), {other fields})
Supplier (ID (PK), EntityID (FK), {other fields})
Telephone (ID (PK), EntityID (FK), Value)
This also has the added advantage of reducing duplication between Customer and Supplier.
A good design would be
Customers: Table of customers - CustomerId, Other columns
Suppliers: Table of suppliers - SupplierId, Other columns
Telephones: Table of telephones - TelephoneId, other columns
CustomerTelephones: CustomerId, TelephoneId
SupplierTelephones: SupplierId, TelephoneId
my advise is that you assign an ID for the table then add a reference field with the same datatype with the SupplierID and CustomerID
The other answers are decent beginner solutions, but they all have the same fundamental flaw in that Customer or Supplier are Relationships and not their own unique entities. A Customer is not a person, it's a relationship between you and a person.
In fact a person could be an Employee, Customer, Supplier all at the same time, or over time.
A Customer or Supplier could also be a Business, with many involved people.
Here is the correct answer:
PARTY
id
type {individual, organization, automated_agent}
org_name null
first_name null
last_name null
PARTY_RELATIONSHIP
from_party_id FK PARTY
type {supplier_of, customer_of, ...}
to_party_id FK PARTY
from_date
to_date null
Usage:
Insert into party (id, type, org_name) values (1, 'organization', 'Raw Steel Co');
Insert into party (id, type, f_name, last_name) values (2, 'individual', 'Davide', 'X');
-- Raw Steel Co is both a customer of and supplier to you:
Insert into party_relationship values (1, 'supplier_of', 2, getdate(), null);
Insert into party_relationship values (1, 'customer_of', 2, getdate(), null);
Now, some people don't like Single Table Inheritance because of the nulls, but goddam it is easier to work with. Use Class Table Inheritance if you're finicky.
The 'type' columns should be foreign keys to type tables.
you can do like
Customers: Table of customers - CustomerId, Other columns
Suppliers: Table of suppliers - SupplierId, Other columns
Telephones: Table of telephones - TelephoneId,TypeId,TypeName, other columns
where TypeName will be Customers or Suppliers, ant TypeId will be id of the resp.

Table with multiple languages

I want to store multiple translations of an text in a MSSQL database.
for example one table product with the columns
ProductId
ProductPrice
ProductNameId
primairy key = ProductId
and a table with the productnames
Id
Language
Name
primairy key = Id and Language
How can i create an foreign key to link the Product.ProductNameId to ProductName.Id
I think that the most appropriate pk-fk relationship is between ProductId on the Product table, and a ProductId (without Language) in the ProductName table. The field on the Product table is the pk, and the field in the ProductName table is the fk. That will ensure that there are no records in the PeoductName table that don't match with a record in the ProductName table.
If you want to treat Language similarly, then you can create a Language table with a LanguageId field. then, make a LanguageId field in the ProductNames table, and make that a fk.
when you retrieve product information, you JOIN Product and ProductName on their ProductId fields, and then specify the LanguageId in the WHERE clause.
I'd change the ProductNames table to:
Id
ProductId
Language
Name
With these constraints:
Primary key on Id
Foreign key on ProductId
Unique constraint on (ProductId,Language)

Resources