Business-Logic Relationships in SQL - sql

Can anyone please tell me how can one estabilish 1 to 0..1 and 1 to 1..* relationships between tables in SQL (Server)?
Thank you very much.

1 to 1..*
Create a Foreign key from a parent table to the primary key of the child (lookup table).
CREATE TABLE A
(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Somecolumn int,
SomeOtherColumn Varchar(50),
B_id int CONSTRAINT FOREIGN KEY REFERENCES B(id),
-- ...other columns
)
CREATE TABLE B
(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name Varchar(50)
)
1 to 0..1
Create a table with the primary key also defined as a Foreign key to the parent table
CREATE TABLE [Master]
(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Somecolumn int,
SomeOtherColumn Varchar(50),
-- ...other columns
)
CREATE TABLE [Child]
(
id int NOT NULL PRIMARY KEY,
OtherColumn Varchar(50),
)
ALTER TABLE Child
ADD CONSTRAINT FK_Master FOREIGN KEY (id) REFERENCES Master(id)

One to many
Define two tables (example A and B), with their own primary key
Define a column in Table A as having a Foreign key relationship based on the primary key of Table B
This means that Table A can have one or more records relating to a single record in Table B.
If you already have the tables in place, use the ALTER TABLE statement to create the foreign key constraint:
ALTER TABLE A ADD CONSTRAINT FOREIGN KEY fk_b ( b_id ) references b(id)
* fk_b: Name of the foreign key constraint, must be unique to the database
* b_id: Name of column in Table A you are creating the foreign key relationship on
* b: Name of table, in this case b
* id: Name of column in Table B

Related

There are no primary or candidate keys in the referenced table 'tblgender' that match the referencing column list in the foreign key 'giFK'

create database my_data
create table tblperson(
pid int primary key,
pname varchar(15),
pgender varchar(15)
)
create table tblgender(
gid varchar(15) primary key,
gender varchar(15)
)
Alter table tblperson add constraint giFK
Foreign key (pgender) references tblgender(gid)

Register Answers given by a User in a Test

I have 4 SQL tables that define a test:
create table Tests (
TestId int identity not null primary key
)
create table TestsQuestions (
QuestionId int foreign key references Questions(QuestionId),
TestId int foreign key references Tests(TestId),
constraint PK primary key clustered (TestId, QuestionId)
)
create table Questions (
QuestionId int identity not null primary key
)
create table Answers (
AnswerId int identity not null primary key,
QuestionId int not null foreign key references Questions(QuestionId),
IsCorrectAnswer bit not null
)
And a Users table:
create table Users (
UserId int identity not null primary key
)
To register which Tests a User has done I am using:
create table UsersTests (
UserId int foreign key references Users(UserId),
TestId int foreign key references Tests(TestId),
constraint PK primary key clustered (UserId, TestId)
)
But I also need to register which Answers a User gave in each question.
How can I do this?
It seems to be a simple "put all keys into one table" situation, where you'll have to inner join in a view to get more complex data out of it
Create Table UsersTestsQuestionsAnswers(
UserId int NOT NULL foreign key references UsersTests(UserId),
TestID int NOT NULL foreign key references UsersTests(TestId),
AnswerID int NOT NULL foreign key references Answers(AnswerId)
constraint PK primary key clustered (UserId, TestId, AnswerID)
)
You should be able to inner join yourself out of that situation

Oracle SQL Constraints same value in other tables

I need a short answers how to make constraints when
I have table A, B, C, E
Table E have E_ID (E id is primary key)
Table B have B_ID, E_ID (E id are foreign keys)
Table C have C_ID, E_ID (E id are foreign key)
Table A have A_ID,B_ID,C_ID (B and C id are foreign keys)
where id are primary keys.
I want constrain to make sure I table A i have records where C_ID and B_ID has same E_ID.
And it should still have 3rd normal form.
You need the following four constraints:
alter table a add constraint fk1 foreign key (b_id) references b (b_id);
alter table a add constraint fk2 foreign key (c_id) references c (c_id);
alter table c add constraint fk3 foreign key (e_id) references e (e_id);
alter table b add constraint fk4 foreign key (e_id) references e (e_id);
One method is to repeat e_id in table a and then use this for foreign key constraints.
Note the foreign key relationships to the unique keys in this data model:
create table e (eid int primary key);
create table b (bid int primary key,
eid int references e(eid),
unique (bid, eid)
);
create table c (cid int primary key,
eid int references e(eid),
unique (cid, eid)
);
create table a (aid int primary key,
bid int references b(bid),
cid int references c(cid),
eid int references e(eid),
foreign key (bid, eid) references b(bid, eid),
foreign key (cid, eid) references c(cid, eid)
);
Here is a SQL Fiddle.

How to set 2 different FK from different tables on PK with 2 columns

I have 3 tables:
create table book (book_id int not null primary key,
book_name char (100) unique)
create table author (author_id int not null primary key,
authorname char (100) unique)
create table bookauthor (book_id int,
author_id int,
CONSTRAINT pk_book_id PRIMARY KEY (book_id,author_id))
I want to set
column book.book_id as fk to bookauthor.book_id
column author.author_id as fk to bookauthor.author_id
Please remember that pk in bookauthor is on book_id,author_id.
Change your bookauther table to
create table bookauther (
book_id int ,
auther_id int,
CONSTRAINT pk_book_id PRIMARY KEY (book_id,auther_id),
FOREIGN KEY (book_id) REFERENCES book(book_id),
FOREIGN KEY (auther_id) REFERENCES auther(auther_id)
)
Have a look at FOREIGN KEY Constraints and SQL FOREIGN KEY Constraint

Reference from one table to another entire table and specified row

I've got three tables: A, B and C. I want to create table A, and it should have 3 columns, first one should be a PRIMARY KEY.
1) How to create second column, with is a refference to table B,
2) and third with is a refference to 'C.id' row from table C. A.id = C.id
CREATE TABLE A
(
id SERIAL PRIMARY KEY,
// ? - reference to table B
// ? - reference to C.id row. A.id = C.id
)
Database: postgresql
I assume you use mysql database.
CREATE TABLE A
(
id INT NOT NULL PRIMARY KEY,
b_id INT NOT NULL,
c_id INT NOT NULL,
FOREIGN KEY (b_id) REFERENCES B (id),
FOREIGN KEY (c_id) REFERENCES C (id)
) TYPE = INNODB;
Update for using postgresql:
CREATE TABLE "A"
(
id integer NOT NULL,
b_id integer NOT NULL,
c_id integer NOT NULL,
CONSTRAINT id PRIMARY KEY (id),
CONSTRAINT b_id FOREIGN KEY (b_id) REFERENCES "B" (id)
ON UPDATE NO ACTION ON DELETE NO ACTION, --with no action restriction
CONSTRAINT c_id FOREIGN KEY (c_id) REFERENCES "C" (id)
ON UPDATE CASCADE ON DELETE CASCADE --with cascade restriction
)
WITH (
OIDS = FALSE
)
;
ALTER TABLE "C" OWNER TO postgres;