primary-key

Add primary key to existing table

泪湿孤枕 提交于 2019-11-27 00:05:24
问题 I have an existing table called Persion . In this table I have 5 columns: persionId Pname PMid Pdescription Pamt When I created this table, I set PersionId and Pname as the primary key . I now want to include one more column in the primary key - PMID. How can I write an ALTER statement to do this? (I already have 1000 records in the table) 回答1: drop constraint and recreate it alter table Persion drop CONSTRAINT <constraint_name> alter table Persion add primary key (persionId,Pname,PMID) edit:

Why use multiple columns as primary keys (composite primary key)

孤人 提交于 2019-11-26 23:49:26
This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) ) My understanding is that both columns together ( P_Id and LastName ) represent a primary key for the table Persons . Is this correct? Why would someone want to use multiple columns as primary keys instead of a single column? How many columns can be used together as a primary key in a given table? MJB Your understanding is correct. You would do this in many cases. One

Is id column position in Postgresql important?

折月煮酒 提交于 2019-11-26 23:42:09
问题 I was testing a migration that deletes a primary key column id (I wanted to use a foreign key as primary key). When I ran and reverted the migration, I saw that the state of my table is the same, except that id column is now last one. Will it change the behaviour of my database in any way and should I bother to restore the column order in the migration revert code? 回答1: In theory everything should be fine, but there are always scenarios when your code could fail. For example: a) blind insert:

Automatically generate Date + 4-digit sequence number for ID in Access 2010+

此生再无相见时 提交于 2019-11-26 23:31:56
问题 I need to automatically generate a 12 character value for my Business Key. Without any user interaction. 8 character -> Today Date (yyyymmdd or ddmmyyyy). + 4 character -> Sequential Number (0001,0002,0003). The Sequential Number must reset on each new day. Is it possible to do this in Microsoft Access 2010+ without any coding involved? 回答1: Since you are using Access 2010+ the best way to accomplish your goal would be to use a Before Change data macro like this To create the Before Change

Get the auto-generated ID after an insert

馋奶兔 提交于 2019-11-26 23:25:57
问题 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. 回答1: 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"

Sqlite3: Disabling primary key index while inserting?

萝らか妹 提交于 2019-11-26 22:55:24
问题 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,

Entity Framework 4: How to find the primary key?

柔情痞子 提交于 2019-11-26 22:41:55
I am trying to create a generic method using EF4 to find the primary key of an object. example public string GetPrimaryKey<T>() { ... } To give more info I am working off of the Tekpub StarterKit and below is the class I am trying to get up and running using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Objects; using System.Data.Objects.ELinq; using System.Data.Linq; using Web.Infrastructure.Storage.EF4; namespace Web.Infrastructure.Storage { public class EFSession:ISession { PuzzleEntities _db;//This is an ObjectContext public EFSession() {

Hibernate/persistence without @Id

徘徊边缘 提交于 2019-11-26 22:21:48
I have a database view that yields a result set that has no true primary key. I want to use Hibernate/Persistence to map this result set onto Java objects. Of course, because there is no PK, I cannot decorate any field with @Id . When deploying, Hibernate complains about the missing @Id . How can I work around this? If there's a combination of columns that makes a row unique, model a primary key class around the combination of columns. If there isn't, you're basically out of luck -- but you should reexamine the design of the view since it probably doesn't make sense. There are a couple

How to get primary key of table?

为君一笑 提交于 2019-11-26 22:21:32
问题 Is there a way to get the name of primary key field from mysql-database? For example: I have a table like this: +----+------+ | id | name | +----+------+ | 1 | Foo1 | | 2 | Foo2 | | 3 | Foo3 | +----+------+ Where the field id is primary key (it has auto increment but I can't use that). How can I retrieve fields name "id" in php? 回答1: A better way is to use SHOW KEYS since you don't always have access to information_schema. The following works: SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'

sql swap primary key values

走远了吗. 提交于 2019-11-26 21:48:56
问题 is it possible to swap primary key values between two datasets? If so, how would one do that? 回答1: Let's for the sake of simplicity assume you have two records id name --------- 1 john id name --------- 2 jim both from table t (but they can come from different tables) You could do UPDATE t, t as t2 SET t.id = t2.id, t2.id = t.id WHERE t.id = 1 AND t2.id = 2 Note: Updating primary keys has other side effects and maybe the preferred approach would be to leave the primary keys as they are and