Questions tagged [composite-primary-key]

Composite primary key is a primary key, which consists of more than one column. The column combination guarantees the uniqueness of the PK.

A Composite Primary Key is a Composite Key, which is assigned as Primary.

Primary key is a single field or the least combination of fields that uniquely identifies each record in table. When the primary key consists of more than one field, when we are talking about composite primary key. Composite or not, is the most appropriate key to be main key of reference for the table.

The primary key must not be null and must contain only unique values.

Primary keys are mandatory for every table. When choosing a primary key from the pool of candidate keys, always choose a single simple key over a composite key.

Links

984 questions
-1
votes
3 answers

where clause with composite primary key

For example I have a composite primary key on columns id, age in the following table: id age name 1 23 John 2 24 Alex 3 22 Rob 4 20 Ella can I somehow query the table using composite primary key in where clause, somehow like…
Oleksandr Baranov
  • 528
  • 2
  • 6
  • 23
-1
votes
1 answer

Mysql - Table design for authorization checking

I have a table called Student and it is used to save the relationship between Class and Student Class Student (PK) Class 1 Peter Class 1 Harry Class 2 Sally Class 2 Tommy And there is another table called…
barry
  • 79
  • 3
-1
votes
1 answer

Composite primary key vs multiple primary keys

Having this entities: User.java: @Entity @NoArgsConstructor @Getter @Setter public class User { @Id private int id; private String username; @OneToMany(mappedBy = "owner") @MapKey(name = "friend_id") private Map
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
-1
votes
2 answers

Query multiple tables to find duplicate one to one relation (group by multiple column from diffeerent tables)

I have 2 tables table1: id columnA 1 one 2 two 3 one table2 : table1_id columnB 1 row1 2 row2 3 row1 Is it possible to have a single query to fetch me the id from table1 where duplicate value in columnA also has…
-1
votes
1 answer

How to create an n-n relation on the same table with composite primary key in MySQL

I want to store if two events are combinables. It's a n to n relation, so I need to have a pivot table with these constraints : PK(event_1, event_2) = PK(event_2, event_1) event_1 is a reference to id in table events event_2 is a reference to id in…
Bhenscamp
  • 1
  • 1
-1
votes
1 answer

I want to create multiple tables with the same composite primary key without data redundancy in mysql. How can I achieve this?

I am using mysql to create a database. I have one base table named GP, with its Primary Key a composite Primary Key(SAT_ID, DATE). I want to create multiple tables with the same Primary Key (SAT_ID,DATE), but would like to avoid data redundancy. Is…
-1
votes
2 answers

MySQL: How do you query on a compound-primary-key? Specifically, a NOT IN query?

I have a comment table and a comment_edit table, as well as olddb_edit. Simplified, the relevant table looks like this: CREATE TABLE `olddb_edit` ( edit_id INT NOT NULL, edit_time INT NOT NULL, edit_text TEXT NOT NULL, PRIMARY KEY…
WoodrowShigeru
  • 1,418
  • 1
  • 18
  • 25
-1
votes
2 answers

How to make separately unique each column in postgresql?

I know we can apply primary key to a column to provide uniqueness for a row and we can apply multiple primary keys and get a composite key. But this didn't work for my case. I have userID and email columns. And I want them to be unique at the same…
-1
votes
1 answer

Can you join two select statements when the composite primary keys are not in both statements

I need some advice. I have an issue with joining two tables. Example: Owner has a five-piece composite key Position has a Six piece composite key Owner Table has As_Of_Date(Key), PORT_CD (Key), Deal_Or_Schedule (Key),…
-1
votes
1 answer

Why is error 1452 appearing when executing these tables?

I want to insert some data into some tables. However, upon reaching statistics_per_year, mysql shoots out error code 1452 and I do not know why this is. error: 17:54:58 INSERT INTO statistics_per_year(disease_continent_id,infected_id,dead_id)…
-1
votes
1 answer

SQL: Is creating multiple foreign keys with one statement equivalent to creating them with one statement?

For example let's have the following table definition: CREATE TABLE table1 ( id INT UNIQUE, name VARCHAR(100) UNIQUE, description VARCHAR(100), PRIMARY KEY (id, name) ); Now I would like to create another table which would have a…
Mr. Nicky
  • 1,519
  • 3
  • 18
  • 34
-1
votes
2 answers

what is the convention for normalizing a table with many primary keys

I have a database table like so : col1 PRI col2 PRI col3 PRI col4 PRI col5 PRI col6 col7 col8 So, it looks like all the columns from 1 to 5 need to be unique and that it makes 'sense' to just make those keys primary. Is this the right way of…
-1
votes
1 answer

How do I have different fields/attributes dependent on multiple time-based fields/attributes in Microsoft Access?

Difficult to explain, but essentially: I am producing a database for a business where customers rent vehicles and perhaps drivers for a short period of time. I am struggling to prevent vehicles and drivers from being booked multiple times. Ideally,…
-1
votes
1 answer

What is the best practice for a composite PRIMARY KEY?

I have a table in which I have made two columns as composite PRIMARY KEY. Now I have a confusion. Let's say I have a table as following: ColA ColB X NULL X NULL Both the rows "X NULL" and "X NULL" are combined…
Samarth
  • 36
  • 4
-1
votes
1 answer

Table in mySQL that has several foreign keys that are primary key, getting an error ERROR 1215: Cannot add foreign key constraint

I had created those tables: CREATE TABLE `course` ( `idcourse` varchar(2) NOT NULL, `courseName` varchar(45) NOT NULL, `subjectID` varchar(2) NOT NULL, PRIMARY KEY (`idcourse`), KEY `subjectID_idx` (`subjectID`), CONSTRAINT `subjectID`…
Asaf
  • 107
  • 1
  • 12