Please correct me if I design it wrong. I need to design 3 tables as follow:
students : id, name
sections : id, student_id, s_name
schedules: id, section_id, c_name
And here are requirements:
There are students, and each of those student has sections, and each of those sections has schedule.
The relationship from students to sections, sections to schedules is 1 to many
student can have many sections
section can have only 1 student / section can have many schedules
schedule can have only 1 section
========= Here are my tables: =====
students
id --> primary and auto increment
name
sections
id --> primary and auto increment
student_id --> foreign key reference to id of students table
s_name
schedules
id --> primary and auto increment
section_id --> foreign key reference to id of sections table
c_name
I would appreciate any helps and many thanks.
Yes, your schema seems to be BCNF normalized, but it does not fulfil all the given requirement. I would change a few things to make it more intuitive.
2. Sections Table:
section_id -> primary, auto increment
student_id -> Foreign Key to students.id
Explanation: Section ID is one to one with student, but student is one to many with section.
3. SectionNames Table:
section_id -> primary and foreign key, from section table.
section_name -> String, name of section.
Explanation : You would add this if you need to store s_name, like you're doing right now.
4. Schedule Table:
class_id -> Primary, auto increment key
section_id -> Foreign key, sections table.
Explanation: Schedule's (Class's) one to one relation to section, and sections one to many relation to classes.
5. ScheduleName Table:
class_id -> Primary, and foreign key to schedule table.
class_name -> String
Explanation: Stores c_name as per your table schema.
Related
I have three tables in a PostgreSQL database (one for storing articles, one for all tags, and one for the relation between the two):
table: article
columns: article_id, title, content
table: tag
columns: tag_id, name
table: article2tag
columns: article_id, tag_id
For now I only have two tags in the tag table:
tag (table)
-----------
tag_id name
1 apple
2 orange
I have an article (the one with ID 1) tagged with the two tags: apple and orange:
article2tag (table)
--------------------
article_id tag_id
1 1
1 2
But let's say one of the tags in the tag table will be removed, for example the tag named apple, now tag_id in article2tag will point to an inexistent tag.
What is the proper way (if there is one) to handle this situation?
That can be handled with foreign key constraints.
ALTER TABLE article2tag
ADD FOREIGN KEY (article_id)
REFERENCES article
(article_id);
ALTER TABLE article2tag
ADD FOREIGN KEY (tag_id)
REFERENCES tag
(tag_id);
That way, if you try to delete a tag that is still used in an article you'll get an error and cannot delete the tag.
You can also specify that, if a tag is deleted it will be removed from all articles with ON DELETE CASCADE.
ALTER TABLE article2tag
ADD FOREIGN KEY (tag_id)
REFERENCES tag
(tag_id)
ON DELETE CASCADE;
You can also use that for the foreign key to article.
You probably don't know about primary keys too, so you might encounter an error when trying to create the foreign key constraints about the referenced column not being unique. In that case add primary key constraints to article and tag.
ALTER TABLE article
ADD PRIMARY KEY (article_id);
ALTER TABLE tag
ADD PRIMARY KEY (tag_id);
You should also define a primary key for articel2tag analogously.
ALTER TABLE article2tag
ADD PRIMARY KEY (article_id,
tag_id);
I am thinking of three tables;
Employee with emp_id an, emp_name and other related data.
and
Person table with person_id, person_name and other related data.
and
GroupList with grp_list_id, member_id (either emp_id or person_id) and is_employee (true for employee and false for person) and other related data.
This is the first time I am trying to do a table whose foreign key can come from two different tables. Can someone please suggest the best way to achieve it?
We are going to store in PostgreSQL some info about organizations (in fact that is some entity, but I say "organization" just for clarity of structure). Each organization has 6 sublevels. First level - departments, second (inside departments) - sections etc. Each department (just like section and other) can have a unique name. We are thinking about 7 tables like
Table organizations
id (primary key) -- organization name
Table level_1
id (primary key) -- organization_id (foreign key) -- name
Table level_2
id (primary key) -- level_1_id (foreign key) -- name
etc
We suppose that on low levels (2-6) we can have thousands ob objects for each level.
The question is how we should construct the primary key - as simple key (like described above) or composite key. F.i.
Table level_1
id (primary key) -- organization_id (primary key) -- name
Table level_2
id (primary key) -- level_1_id (primary key) -- name
etc
What key type is better (first of all, faster for SELECT queries) and why?
Thank you.
IMHO, it's a bad design.
What will you do if you need to add a level ?
You'll need to add a table
You'll need to modify all your code, views...
Not very scalable...
You should have only one table.
Organizations
Id Parent_Id Name
Data Sample:
1 NULL Corporation
2 1 Division_1
3 1 Division_2
4 2 Dept_1_of_Division_1
5 2 Dept_2_of_Division_1
6 3 Dept_1_of_Division_2
To query this table, you would need to use WITH RECURSIVE
I've here a problem that I couldn't find a proper solution on my researches, maybe it's because I couldn't find out the exact terms to search for it, so if this is a duplicate I will delete it.
My problem is I want to know if it is possible to avoid a combination of data between two fields. I will show the structure and the kind of data I want to avoid. It will be easier to understand.
Table_A Table_B
------------------------ -------------------------------
id integer (PK) id integer (PK)
description varchar(50) title varchar(50)
id1_fromA (FK A->id)
id2_fromA (FK A->id)
I'm trying to validate the following data on table Table_B (combination is between id1_fromA and id2_fromA)
id title id1_fromA id2_fromA
1 Some Title 1 2 --It will be permmited
2 Some other 1 2 --It is a duplicate NOT ALLOWED
3 One more 1 1 --It is equals NOT ALLOWED
4 Another 2 1 --It is same as registry id 1 so NOT ALLOWED
5 Sample data 3 2 --It is ok
With above data I can easily solve the problem for registry ID=2 with
ALTER TABLE table_B ADD CONSTRAINT UK_TO_A_FKS UNIQUE (id1_fromA, id2_fromA);
And the problem for registry ID=3 with
ALTER TABLE table_B ADD CONSTRAINT CHK_TO_A_FKS CHECK (id1_fromA != id2_fromA);
My Problem is with the registry ID=4 I want to avoid such duplicate of combination as 1,2=2,1. Is it possible to do it with a CONSTRAINT or an INDEX or an UNIQUE or I will need to create a trigger or a procedure to do so?
Thanks in advance.
You can't do this with a unique constraint, but you can do this with a unique index.
create unique index UK_TO_A_FKS
on table_b (least(id1_froma, id2_froma), greatest(id1_froma, id2_froma));
i have three tables say city,state and road
1) city -> city_id(PK),name
2) state-> Stt_id(PK),name
3) Road-> Edge_id(PK), Admin_id(FK)
where Admin_id refers to city_id and Stt_id both.
This is done because the tables are too huge.
say city_id contains 1,2,3
and Stt_id contains 4,5,6
now if i am inserting 1,2,3,4,5,6 in admin_id it is throuing an error .. what is the solution of my problem,
regards
sanjay
Create an admin table that holds an entry for every city and state, using its admin_ids as city_id and stt_id in their respective tables. Then declare foreign keys on city_id, stt_id and road.admin_id, referencing admin.admin_id (retaining all the existing PKs, of course).