primary-key

Composite PRIMARY KEY enforces NOT NULL constraints on involved columns

梦想与她 提交于 2019-11-28 00:42:16
This is one strange, unwanted behavior I encountered in Postgres: When I create a Postgres table with composite primary keys, it enforces NOT NULL constraint on each column of the composite combination. For example, CREATE TABLE distributors (m_id integer, x_id integer, PRIMARY KEY(m_id, x_id)); enforces NOT NULL constraint on columns m_id and x_id , which I don't want! MySQL doesn't do this. I think Oracle doesn't do it as well. I understand that PRIMARY KEY enforces UNIQUE and NOT NULL automatically but that makes sense for single-column primary key. In a multi-column primary key table, the

Get the auto-generated ID after an insert

早过忘川 提交于 2019-11-28 00:30:59
I have an Oracle Express 10g database. In my table I have an auto-generated ID and I would like to know how I can find what the generated ID is after an insert happens. I am currently using PHP. You can get the returning id into a variable. For example, this code: $data = array("larry","bill","steve"); $db = OCILogon("scott","tiger"); $stmt = OCIParse($db,"insert into names values (myid.nextval,:name) returning id into :id"); OCIBindByName($stmt,":ID",$id,32); OCIBindByName($stmt,":NAME",$name,32); while (list(,$name) = each($data)) { OCIExecute($stmt); echo "$name got id:$id\n"; } This gives

Is it OK not to use a Primary Key When I don't Need one

一曲冷凌霜 提交于 2019-11-28 00:13:54
问题 If I don't need a primary key should I not add one to the database? 回答1: A primary key uniquely identifies a row in your table. The fact it's indexed and/or clustered is a physical implementation issue and unrelated to the logical design. You need one for the table to make sense. 回答2: You do need a primary key. You just don't know that yet. 回答3: If you don't need a primary key then don't use one. I usually have the need for primary keys, so I usually use them. If you have related tables you

When not to use surrogate primary keys?

ぐ巨炮叔叔 提交于 2019-11-27 23:07:46
I have several database tables that just contain a single column and very few rows, often just an ID of something defined in another system. These tables are then referenced with foreign keys from other tables. For example one table contains country codes (SE, DK, US etc). All values are always unique natural keys and they are used as primary keys in other (legacy) systems. It seems really unnecessary to introduce a new surrogate key to these tables, or? In general, what are the exceptional cases when surrogate keys shouldn't be used? marc_s I would say the following criteria must be met: your

How to choose my primary key?

前提是你 提交于 2019-11-27 23:04:31
I found this reading material on choosing a primary key . Is there a guide / blog post on how to choose the primary key for a given table? Should I use a auto-incremented/generated key, or should I base the primary key on the data being modeled (assuming it has a truly unique field)? Should the primary key always be long for performance's sake, or can I take an external unique id as primary key, even if it's a string? I believe that in practice using a natural key is rarely better than a surrogate key . The following are the main disadvantages of using a natural key as the primary key: You

Easy mysql question regarding primary keys and an insert

一笑奈何 提交于 2019-11-27 22:26:26
In mysql, how do I get the primary key used for an insert operation, when it is autoincrementing. Basically, i want the new autoincremented value to be returned when the statement completes. Thanks! Your clarification comment says that you're interested in making sure that LAST_INSERT_ID() doesn't give the wrong result if another concurrent INSERT happens. Rest assured that it is safe to use LAST_INSERT_ID() regardless of other concurrent activity. LAST_INSERT_ID() returns only the most recent ID generated during the current session. You can try it yourself: Open two shell windows, run mysql

Entity Framework Code First Migrations: Set Primary Key Value

人走茶凉 提交于 2019-11-27 22:00:09
I have a table that stores some extra data for some rows of a table like: public class QuoteExtra { [Key] public int QuoteId { get; set; } // More fields here } I'd like to be able to add rows to this table where I explicitly set the PK. If I simply leave it as above, setting a value and submitting the row causes the value to be discarded and replaced with the auto-generated value from the database (and the column is defined as an Identity column in the actual schema). This appears to be the right solution: public class QuoteExtra { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)]

Use item specific prefixes and autonumber for primary keys?

眉间皱痕 提交于 2019-11-27 21:28:30
We had a meeting this morning about how would should store our ID for some assets that we have in our database that we are making, the descusion generated a bit of heat so I decided to consult the experts of SO. The table structure that I belive that we should have(short version) is like the following: Example 1) AssetId - int(32) - Primary Key Type - string so some example data is like this: ==AssetId======Type=== 12345 "Manhole" 155415 "Pit" etc. Another member of the team suggested something like this: Example 2) AssetId - string - Primary Key Type - string so some example data is like this

Can a foreign key act as a primary key?

故事扮演 提交于 2019-11-27 20:38:04
I'm currently designing a database structure for our team's project. I have this very question in mind currently: Is it possible to have a foreign key act as a primary key on another table? Here are some of the tables of our system's database design: user_accounts students guidance_counselors What I wanted to happen is that the user_accounts table should contain the IDs (supposedly the login credential to the system) and passwords of both the student users and guidance counselor users. In short, the primary keys of both the students and guidance_counselors table are also the foreign key from

Sqlite3: Disabling primary key index while inserting?

流过昼夜 提交于 2019-11-27 20:14:21
I have an Sqlite3 database with a table and a primary key consisting of two integers, and I'm trying to insert lots of data into it (ie. around 1GB or so) The issue I'm having is that creating primary key also implicitly creates an index, which in my case bogs down inserts to a crawl after a few commits (and that would be because the database file is on NFS.. sigh ). So, I'd like to somehow temporary disable that index. My best plan so far involved dropping the primary key's automatic index, however it seems that SQLite doesn't like it and throws an error if I attempt to do it. My second best