primary-key

Do I need a primary key for my table, which has a UNIQUE (composite 4-columns), one of which can be NULL?

梦想与她 提交于 2019-11-27 02:26:45
问题 I have the following table (PostgreSQL 8.3) which stores prices of some products. The prices are synchronised with another database, basically most of the fields below (apart from one) are not updated by our client - but instead dropped and refreshed every once-in-a-while to sync with another stock database: CREATE TABLE product_pricebands ( template_sku varchar(20) NOT NULL, colourid integer REFERENCES colour (colourid) ON DELETE CASCADE, currencyid integer NOT NULL REFERENCES currency

What is the order of records in a table with a composite primary key

喜你入骨 提交于 2019-11-27 02:10:29
问题 In PostgreSQL, when a combination of multiple columns is specified as the PRIMARY KEY , how are the records ordered? This is with the assumption that PostgreSQL orders the records in the order of the primary key. Does it? Also, is the primary key automatically indexed in case of PostgreSQL? 回答1: This question makes the misguided assumption that the primary key imposes a table order at all. It doesn't. PostgreSQL tables have no defined order, with or without a primary key; they're a "heap" of

Why to use “not null primary key” in TSQL?

点点圈 提交于 2019-11-27 02:09:55
问题 I am seeing too frequently "not null primary key" in scripts creating tables in TSQL in blogs, articles, books, forums, here in SO, etc. For example, BING gives 630,000 results ((just in English) if to search against "not null" NEAR "primary key" "sql server": http://www.bing.com/search?q=%22not+null%22+NEAR+%22primary+key%22+%22sql+server%22&go=&form=QBRE&filt=lf I always perceived "NOT NULL" as inseparable part of definition of PRIMARY KEY, at least in SQL Server. I tried to create a tables

Why single primary key is better than composite keys?

≡放荡痞女 提交于 2019-11-27 02:08:34
问题 Why is the rejection of composite keys in favor of all tables using a single primary key named id? Cause generally all ORM follow this. EDIT I just started learning ruby on rails and in the book of agile development by pragmatic there is a line:--- Rails really don't work too well unless each table has a numeric primary key. It is less fussy about the name of the column. Same kind of line I read when I was learning Doctrine. EDIT2 Please check this link too. I am getting more and more

MySQL: #1075 - Incorrect table definition; autoincrement vs another key?

若如初见. 提交于 2019-11-27 01:34:20
Here is a table in MySQL 5.3.X+ db: CREATE TABLE members` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `memberid` VARCHAR( 30 ) NOT NULL , `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `firstname` VARCHAR( 50 ) NULL , `lastname` VARCHAR( 50 ) NULL , UNIQUE (memberid), PRIMARY KEY (id) ) ENGINE = MYISAM; Id column is never used in queries, it is just for visual convenience (so it's easy to see how the table grows). Memberid is an actual key, is unique, and memberid is used in queries to identify any member (WHERE memberid='abcde'). My question is: how to keep auto_increment, but

how to make a composite primary key (java persistence annotation)

你。 提交于 2019-11-27 01:27:56
How to make it so that the table user_roles defines the two columns (userID, roleID) as a composite primary key. should be easy, just can't remember/find. In user entity: @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "user_roles") public List<RoleDAO> getRoles() { return roles; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getUserID() { return userID; } In roles entity: @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "user_roles") public List<UserDAO> getUsers() { return users; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getRoleID()

Do link tables need a meaningless primary key field?

血红的双手。 提交于 2019-11-27 01:16:58
问题 I am working on a couple of link tables and I got to thinking (Danger Will Robinson, Danger) what are the possible structures of a link table and what are their pro's and con's. I came up with a few possible strictures for the link table: Traditional 3 column model id - auto-numbered PRIMARY table1fk - foreign key table2fk - foreign key It's a classic, in most of the books, 'nuff said. Indexed 3 column model id - auto-numbered PRIMARY table1fk - foreign key INDEX ('table1fk') table2fk -

newid() vs newsequentialid() What are the differences/pros and cons?

◇◆丶佛笑我妖孽 提交于 2019-11-27 01:16:01
问题 In a database where all of your primary keys are GUIDs, what are the differences/implications and/or pros and cons using newid() versus newsequentialid() as the "default value or binding". The only difference that I know of is that newid() creates a new random GUID as opposed to newsequentialid() creates a new GUID based on the last one that is in the table in an incremented fashion. 回答1: As I understand it, when you perform an insert in a row the DB, it will be inserted in order relative to

What is the difference between a primary key and a unique constraint?

淺唱寂寞╮ 提交于 2019-11-27 01:13:42
问题 Someone asked me this question on an interview... 回答1: A primary key is a unique field on a table but it is special in the sense that the table considers that row as its key. This means that other tables can use this field to create foreign key relationships to themselves. A unique constraint simply means that a particular field must be unique. 回答2: Primary keys can't be null. Unique keys can. 回答3: Primary key can not be null but unique can have only one null value. Primary key create the

Deciding between an artificial primary key and a natural key for a Products table

安稳与你 提交于 2019-11-27 00:47:02
Basically, I will need to combine product data from multiple vendors into a single database (it's more complex than that, of course) which has several tables that will need to be joined together for most OLTP operations. I was going to stick with the default and use an auto-incrementing integer as the primary key, but while one vendor supplies their own "ProductiD" field, the rest do not and I would have to do a lot of manual mapping to the other tables then to load the data (as I would have to first load it into the Products table, then pull the ID out and add that along with the other