Mysql: Multiple column primary key with null values

Tobia

I have these tables:

CREATE TABLE items(
   id int,
   ...
)

CREATE TABLE types(
   id char(1),
   ...
)


CREATE TABLE prices(
   item int,
   type char(1) NULL, --can not be null because it is in PK!
   price decimal,
   PRIMARY KEY (item, version),
   FOREIGN KEY (item) REFERENCES items(id),
   FOREIGN KEY (type) REFERENCES types(id)
)

Not all items have different price for type:

INSERT INTO prices (item, type, price)
VALUES (1,'A',10.0),
VALUES (1,'B',20.0),
VALUES (1,'C',20.0),
VALUES (2,NULL,50.0),
VALUES (3,'A',10.0),
VALUES (3,'B',20.0),
VALUES (4,NULL,70.0);

As you can se some items (2,4) have just one price. This way I can set the foreign key between prices and types but I can not add type in the price's primary key because it is nullable... how to solve this problem? I need both foreign key and primary key to type field, but this can be optional.

M.Scherzer

you could create a "surrogate" primary key on the prices table based on a identidy so you dont have to worry about mainting the pk column. You can then create a unique index across your item and type columns.

the id column which is your pk then just exists so mqsql can keep the rows organised on the file system.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set a column containing NULL values as secondary primary key of a DataTable

From Dev

How to select multiple rows by multi-column primary key in MySQL?

From Dev

Primary key for multiple column in PostgreSQL?

From Dev

Why primary key cannot contain null values?

From Dev

Query to update primary key column values

From Dev

Update all values in Primary Key column

From Dev

MySQL is not failing when deliberately inserting `NULL` in Primary Key AUTO_INCREMENT column

From Dev

mysql "Default" column value NULL is getting erased, on executing alter table for PRIMARY KEY

From Dev

MySQL: Does the referenced column need to be primary key?

From Dev

Configuring MySQL to allow generation of Primary Key values

From Dev

Configuring MySQL to allow generation of Primary Key values

From Dev

PHP MySQL Insert Ignore with multiple Primary Key

From Dev

MySQL column with multiple values

From Dev

Multiple values in column MYSQL

From Dev

How to avoid inserting null values into Primary key or Not Null columns?

From Dev

MySQL unique key not working as expected with multiple columns containing a primary key

From Dev

Create a combined primary key with a one column accepting NULL

From Dev

Why would an access table primary key column contain a null?

From Dev

Altering an existing column to 'Primary Key' and 'NOT NULL' without dropping the table

From Dev

Finding distinct values of non Primary Key column in CQL Cassandra

From Dev

Get Primary Key column values after bulk insertion using SqlBulkCopy

From Dev

Retrieve primary key value of table when search for values in other column

From Dev

Get distinct column values based on composite primary key

From Dev

Setting new column values based on primary key lookup in another table

From Dev

How to create a primary key column and fill it with integer values on Hana sql

From Dev

Searching for multiple values in MySQL column

From Dev

How to select a range of rows from a multiple column primary key?

From Dev

how to build a URL for a json object consisting of a multiple column primary key?

From Dev

SQL get interection of values across multiple rows grouped by primary key

Related Related

  1. 1

    How to set a column containing NULL values as secondary primary key of a DataTable

  2. 2

    How to select multiple rows by multi-column primary key in MySQL?

  3. 3

    Primary key for multiple column in PostgreSQL?

  4. 4

    Why primary key cannot contain null values?

  5. 5

    Query to update primary key column values

  6. 6

    Update all values in Primary Key column

  7. 7

    MySQL is not failing when deliberately inserting `NULL` in Primary Key AUTO_INCREMENT column

  8. 8

    mysql "Default" column value NULL is getting erased, on executing alter table for PRIMARY KEY

  9. 9

    MySQL: Does the referenced column need to be primary key?

  10. 10

    Configuring MySQL to allow generation of Primary Key values

  11. 11

    Configuring MySQL to allow generation of Primary Key values

  12. 12

    PHP MySQL Insert Ignore with multiple Primary Key

  13. 13

    MySQL column with multiple values

  14. 14

    Multiple values in column MYSQL

  15. 15

    How to avoid inserting null values into Primary key or Not Null columns?

  16. 16

    MySQL unique key not working as expected with multiple columns containing a primary key

  17. 17

    Create a combined primary key with a one column accepting NULL

  18. 18

    Why would an access table primary key column contain a null?

  19. 19

    Altering an existing column to 'Primary Key' and 'NOT NULL' without dropping the table

  20. 20

    Finding distinct values of non Primary Key column in CQL Cassandra

  21. 21

    Get Primary Key column values after bulk insertion using SqlBulkCopy

  22. 22

    Retrieve primary key value of table when search for values in other column

  23. 23

    Get distinct column values based on composite primary key

  24. 24

    Setting new column values based on primary key lookup in another table

  25. 25

    How to create a primary key column and fill it with integer values on Hana sql

  26. 26

    Searching for multiple values in MySQL column

  27. 27

    How to select a range of rows from a multiple column primary key?

  28. 28

    how to build a URL for a json object consisting of a multiple column primary key?

  29. 29

    SQL get interection of values across multiple rows grouped by primary key

HotTag

Archive