primary-key

Updating MySQL primary key

六月ゝ 毕业季﹏ 提交于 2019-11-29 19:04:22
I have a table user_interactions with 4 columns: user_1 user_2 type timestamp The primary key is (user_1,user_2,type) and I want to change to (user_2,user_1,type) So what I did was : drop primary key ... add primary key (user_2,user_1,type)... and voila... The problem is that database is live on a server. So before I could update the primary key, many duplicates already crept in, and they are continuously creeping in. What to do? What I want to do now is to remove duplicates and keep the ones with the latest timestamp (which is a column in the table). And then somehow update the primary key

SQL primary key, INT or GUID or..? [duplicate]

北城以北 提交于 2019-11-29 19:02:56
问题 This question already has an answer here: INT vs Unique-Identifier for ID field in database 6 answers Is there any reason why I should not use an Integer as primary key for my tables? Database is SQL-CE, two main tables of approx 50,000 entries per year, and a few minor tables. Only two connections will exist constantly open to the database. But updates will be triggered through multiple TCP socket connections, so it will be many cross threads that access and use the same database-connection.

Changing primary key int type to serial

不羁的心 提交于 2019-11-29 17:38:31
问题 Is there a way to change existing primary key type from int to serial without dropping the table? I already have a lot of data in the table and I don't want to delete it. 回答1: Converting an int to a serial more or less only means adding a sequence default to the value, so to make it a serial; Pick a starting value for the serial, greater than any existing value in the table SELECT MAX(id)+1 FROM mytable Create a sequence for the serial (tablename_columnname_seq is a good name) CREATE SEQUENCE

ORM support for compound primary keys

会有一股神秘感。 提交于 2019-11-29 17:14:47
I've read that compound primary keys will confuse the hell out of typical ORM code generators . Which ORMs work best with compound PKs and which to avoid? (I've a particular interest in .NET) I'm using NHibernate successfully with compound keys. <class name="UserProfileField" table="UserProfileFields"> <composite-id> <key-many-to-one name="Parent" column="UserId" lazy="false"/> <key-property name="FieldName"/> </composite-id> ... 来源: https://stackoverflow.com/questions/218100/orm-support-for-compound-primary-keys

Calculate next Primary Key - of specific format

大憨熊 提交于 2019-11-29 16:34:46
I have a table which holds a list of IDs, and various other columns such as IDName. The Primary Key of the table is the ID itself, however it does not auto_increment. So, I want to be able to generate / calculate the next primary key, however there is a twist: The primary key should be in a specific format, i.e. the 8 digit ID is made up of three parts: <the level><a code><a sequence #> , e.g. <2><777><0123> = 27770123 So, when I am creating a new ID for the table, I want the next sequence number for a specific level and code. E.g. following the example above I might want to know the next

Identifying Sybase tables, fields, keys, constraints

◇◆丶佛笑我妖孽 提交于 2019-11-29 16:19:35
I'm trying to set up a Sybase query that will give me the following output: Table KeyType KeyNumber Column table1 PK 1 table1_id table1 FK 2 table2_id table1 FK 3 table3_id table1 FK 4 table4_id table1 Unique 5 table1_abc table1 Unique 5 table1_def In other words, I need the PK for each table, and every foreign key it has, as well as every unique key (not where a key has more than one element, such as the unique key above, this is identified by having the same KeyNumber). I'm guessing I need to use sysobject, syscolumns, syskeys and sysconstraints but I can't seem to figure out how they

Can a database attribute be primary and foreign key?

假如想象 提交于 2019-11-29 14:55:21
问题 I have 2 tables, User and Employee . Each user is given a User_ID and that is a primary key in the User table and a foreign key in the Employee table. Can that attribute in the Employee table also be a primary key? 回答1: If you have a one-to-one relation between two tables, then the primary key of the details table is a foreign key as well. master detail (1 : 1) +----------+ 1:1 +-------------+ | PK id |<---o| PK FK id | +----------+ +-------------+ | col1 | | col1 | | col2 | | col2 | | etc. |

Changing MySQL primary key when foreign key contraints exist

三世轮回 提交于 2019-11-29 13:34:16
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE parent ( old_pk CHAR(8) NOT NULL PRIMARY KEY ) ENGINE=InnoDB; CREATE TABLE child ( parent_key CHAR(8), FOREIGN KEY (parent_key) REFERENCES parent(old_pk) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB; I want to add a new auto-incrementing integer id column to parent and use it as the primary key instead, while still keeping old_pk as a unique key and allowing other tables like child to reference it in foreign key contraints. Unfortunately, simply saying ALTER TABLE parent DROP PRIMARY KEY doesn't work

Referencing a composite primary key

冷暖自知 提交于 2019-11-29 13:08:11
问题 I have two tables, with each table having a composite primary key. One attribute is in both composite primary keys. How am i supposed to reference the common attribute?? Do i just reference it as a FK in both tables as below? The cust_id and flight_id below are each part of the composite key as well and reference primary keys in other tables. (Ignore the third attribute in the erd for the br_flight table as I choose to use a composite key in the end). CREATE TABLE BOOKING_REFERENCE (

ObjectContext.SaveChanges() violates primary key, throws UpdateException?

徘徊边缘 提交于 2019-11-29 12:19:54
Paraphrasing from this MSDN documentation ... An INSERT statement is generated by the Entity Framework and executed on the data source when SaveChanges is called on the ObjectContext. If the INSERT operation succeeds, server-generated values are written back to the ObjectStateEntry. When AcceptChanges is called automatically at the end of the SaveChanges execution, a permanent EntityKey is computed by using the new server-generated values. This does not seem to be occurring in my code. When I call ObjectContext.SaveChanges() , an UpdateException is thrown with InnerException.Message value: