I have a SQLite database with the following tables:
There is a Persons table (ID, name,...) and a Pets table (ID, name, owner,...), where owner is a foreign key that refers to a row of Persons.
A person can have as many pets as they want, but each pet must have an individual name.
The problem is that the name must be unique only for the same owner, a name can occur more than once in the Pets table, but only once per ID of the person stored in the owners column.
Therefore, the name column cannot be unique, but owner and name together should be.
How can this be achieved in SQLite?
You simply want a unique constraint or index:
create unique index unq_pets_owner_name on pets(owner, name);
Related
Let me try to make it simple with an example.
I am creating a database with 2 tables, School and Students
School table with 2 columns SchoolID(PK) and Location
Student table with 3 columns StudentID(PK), SchoolID(FK) and Grade
The Student table will have students from all the schools which can be identified by the foreign key SchoolID. The StudentID column will be unique to each student across all schools. Well and good so far.
I need another column in Student table which will be unique only with respect to the foreign key value. Let's call this ID. I need this column to be unique only to each SchoolID. So, If I filter out the students belonging to a particular SchoolID, I shouldn't get 2 students with same ID.
I'm not sure if that's a valid scenario.
Edit:
This is good
This is not
I think there is something wrong with the datamodel. Since you have StudentID as primary key in Student-table, this column will always be unique in this table. But it seems like you are trying to create a Student-School table where you can have the same student connected to multiple schools (but no the same school multiple times for one single student). I think you need at least 3 tables:
Student (Pk StudentID)
School (Pk SchoolId)
StudentSchool
The StudentSchool table will have two FK-columns: StudentID and SchoolID. To protect from the same student beeing mapped to the same school multiple times you could either have the PK include StudentId and SchoolId or create a Unique constraint.
i have a database for university so the students take many courses but each student have id as primary key also each course have an id as primary key its not allowed to have duplicate id at same table what should i use to make a table that same id but different courses id
In a normalized relational database, when you have a situation where items from one table can be associated with many items in a second table, and likewise those related items can be associated with many items in the first table, you should use a third table to store those relationships. In your case, that table might be called Enrollments.
You can use JOIN operations on that third table to relate, group, and aggregate across that relationship. You can also store any metadata associated with that relationship in the JOIN- table, for example you might store the enrollment date there.
Like vicatcu said, create a third table
Course_id | Student_id
The ID's of the courses and the students are uniq in their own tables. The relation between course < - > student is stored in that third table.
I have the following two schemas for two tables
create table My_contacts
(
Contact_ID, last name, first name, email, phone, profession_ID
)
Create table Profession
(
Profession_ID, Profession Name
)
The relationship between tables profession and My_contact and is one-to-many.
I need a fast way to decide which one is parent table and which one is child table, so my assumption is that because in the table My-Contacts has the foreign key profession_ID, it is the child table. (Based on primary key belongs to parent table, foreign key is in child table.)
However, I read the book Headfirst SQL, page P311. it says
we moved the profession column into a new child table, and change the profession column in the parent table into foreign key profession_ID"
according to the above, the book means the profession table is the child table, and the my_contact table is the parent table.
I have tried to figure out this conflict, but could not.
I know that a primary key must be unique, but is it okay for a primary key to be equal to a different column in the same table by coincidence?
For instance, I have 2 tables. One table is called person that holds information about a person (ID, email, telephone, address, name). The other table is staff (ID, pID(person ID), salary, position).
In staff the ID column is the primary key and is used to uniquely identify a staff member. The number is from 1 - 100. However, the pID (person ID) may be equal to the ID. For instance the staff ID may be 1 and the pID that it references to may be equal to 1.
Is that okay?
The job of the primary key is to uniquely and reliably identify each row - therefore, it must be unique and NOT NULL - anything else is irrelevant.
If you just happen to have a second column with the exact same values - I'd be wondering why that is the case - but that doesn't in any way affect the primary key negatively.
Primary key of a table must be unique and not null. There are no restrictions on uniquity between tables. It's 100% up to you.
Yes. There's no checking of relationships between different columns in a table.
The restriction you're worried about doesn't even make sense. Suppose you had a table for persons with columns ID, name, and year_of_birth. It wouldn't allow someone who was born in 1975 to have ID = 1975.
Simple question.
If i had a table users which contains columns like username, password, and an auto incrementing id which is the primary key.
And another table called names which contains first_name, middle_name, last_name and a foreign key called user_id which references the id in the users table.
Do I have to insert a value to that foreign key? Or SQL will automatically copy the value of the id in the users table?
Another follow up question, What is the data type of the user_id field in the names table?
Yes, you will have to insert the foreign key. When you insert a record into the names table, SQL will not automatically be able to know which user the names record is referring to.
As user_id is referring to an id field that is auto-incrementing it is most likely going to be an integer field.