primary-key

Django queries - id vs pk

霸气de小男生 提交于 2019-11-26 21:41:32
When writing django queries one can use both id/pk as query parameters. Object.objects.get(id=1) Object.objects.get(pk=1) I know that pk stands for Primary Key and is just a shortcut, according to django's documentation. However it is not clear when one should be using id or pk. It doesn't matter. pk is more independent from the actual primary key field i.e. you don't have to care whether the primary key field is called id or object_id or whatever. It also gives your more consistency if you have models with different primary key fields. In Django projects where I know that pk always returns id

ALTER TABLE to add a composite primary key

こ雲淡風輕ζ 提交于 2019-11-26 21:23:28
I have a table called provider . I have three columns called person , place , thing . There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination. How would I ALTER TABLE to add a composite primary key for this table in MySQL with the these three columns? ALTER TABLE provider ADD PRIMARY KEY(person,place,thing); If a primary key already exists then you want to do this ALTER TABLE provider DROP PRIMARY KEY, ADD PRIMARY KEY(person, place, thing); @Adrian Cornish's answer is correct. However, there is another caveat to

Composite PRIMARY KEY enforces NOT NULL constraints on involved columns

自作多情 提交于 2019-11-26 21:09:48
问题 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

Indexes and multi column primary keys

孤街醉人 提交于 2019-11-26 19:35:34
问题 Went searching and didn't find the answer to this specific noob question. My apologies if I missed it. In a MySQL database I have a table with the following primary key PRIMARY KEY id (invoice, item) In my application I will also frequently be selecting on "item" by itself and less frequently on only "invoice". I'm assuming I would benefit from indexes on these columns. MySQL does not complain when I define the following: INDEX (invoice), INDEX (item), PRIMARY KEY id (invoice, item) But I don

When should I use primary key or index?

…衆ロ難τιáo~ 提交于 2019-11-26 19:18:17
问题 When should I use a primary key or an index? What are their differences and which is the best? 回答1: Basically, a primary key is (at the implementation level) a special kind of index. Specifically: A table can have only one primary key, and with very few exceptions, every table should have one. A primary key is implicitly UNIQUE - you cannot have more than one row with the same primary key, since its purpose is to uniquely identify rows. A primary key can never be NULL , so the row(s) it

sql primary key and index

做~自己de王妃 提交于 2019-11-26 19:01:15
问题 Say I have an ID row (int) in a database set as the primary key. If I query off the ID often do I also need to index it? Or does it being a primary key mean it's already indexed? Reason I ask is because in MS SQL Server I can create an index on this ID, which as I stated is my primary key. Edit: an additional question - will it do any harm to additionally index the primary key? 回答1: You are right, it's confusing that SQL Server allows you to create duplicate indexes on the same field(s). But

Why does select SCOPE_IDENTITY() return a decimal instead of an integer?

五迷三道 提交于 2019-11-26 18:52:33
So I have a table with an identity column as the primary key, so it is an integer. So, why does SCOPE_IDENTITY() always return a decimal value instead of an int to my C# application? This is really annoying since decimal values will not implicitly convert to integers in C#, which means I now have to rewrite a bunch of stuff and have a lot of helper methods because I use SQL Server and Postgres, which Postgres does return an integer for the equivalent function.. Why does SCOPE_IDENTITY() not just return a plain integer? Are there people out there that commonly use decimal/non-identity values

Can we update primary key values of a table?

此生再无相见时 提交于 2019-11-26 18:49:35
Can we update primary key values of a table? Vincent Malgrat It is commonly agreed that primary keys should be immutable (or as stable as possible since immutability can not be enforced in the DB). While there is nothing that will prevent you from updating a primary key (except integrity constraint), it may not be a good idea: From a performance point of view: You will need to update all foreign keys that reference the updated key. A single update can lead to the update of potentially lots of tables/rows. If the foreign keys are unindexed (!!) you will have to maintain a lock on the children

@OneToOne bidirectional mapping with @JoinColumn

心已入冬 提交于 2019-11-26 18:27:20
问题 Let's say I have Person class Person{ @Id Integer id; @OneToOne @JoinColumn(name = "person_id") Job myJob; } and Job class Job{ @Id Integer id; Integer person_id; @OneToOne @PrimaryKeyJoinColumn(name = "person_id") Person currentWorker; } I'm not able to map the Person and Job to other Entity, when fetching. What mistake am I doing ? 回答1: Your code should be: @Entity public class Person implements Serializable { @Id Integer id; @OneToOne @JoinColumn(name = "id") Job myJob; } @Entity public

How can I define a composite primary key in SQL?

懵懂的女人 提交于 2019-11-26 18:25:28
How can I define a composite primary key consisting of two fields in SQL? I am using PHP to create tables and everything. I want to make a table name voting with fields QuestionID , MemeberID , and vote . And the Composite primary key consists of the fields QuestionID and MemberID . How should I do this? Just for clarification: a table can have at most one primary key. A primary key consists of one or more columns (from that table). If a primary key consists of two or more columns it is called a composite primary key . It is defined as follows: CREATE TABLE voting ( QuestionID NUMERIC,