primary-key

Primary Key Type: int vs long

两盒软妹~` 提交于 2019-11-28 21:10:00
I know some software shops have been burned by using the int type for the primary key of a persistent class. That being said, not all tables grow past 2 billions. As a matter of fact, most don't. So, do you guys use the long type only for those classes that are mapped to potentially large tables OR for every persistent class just to be consistent? What's the industry concensus? I'll leave this question open for a while so that you can share with us your success/horror stories. Long can be advantageous even if the table does not grow super large, yet has a high turnover ie if rows are deleted

Change Primary Key

南笙酒味 提交于 2019-11-28 20:05:19
I have a table in Oracle which has following Schema: City_ID Name State Country BuildTime Time When i declared the table my primary key was both City_ID and the BuildTime but now I want to change the primary key to three columns: City_ID BuildTime Time How can I change the primary key? Assuming that your table name is city and your existing Primary Key is pk_city , you should be able to do the following: ALTER TABLE city DROP CONSTRAINT pk_city; ALTER TABLE city ADD CONSTRAINT pk_city PRIMARY KEY (city_id, buildtime, time); Make sure that there are no records where time is NULL , otherwise you

Can a foreign key refer to a primary key in the same table?

戏子无情 提交于 2019-11-28 19:04:55
I just think that the answer is false because the foreign key doesn't have uniqueness property. But some people said that it can be in case of self joining the table. I am new to SQL . If its true please explain how and why? Employee table | e_id | e_name | e_sala | d_id | |---- |------- |----- |--------| | 1 | Tom | 50K | A | | 2 | Billy | 15K | A | | 3 | Bucky | 15K | B | department table | d_id | d_name | |---- |------- | | A | XXX | | B | YYY | Now, d_id is foreign key so how it can be a primary key. And explain something about join . What is its use? mvsagar I think the question is a bit

Primary and Foreign Key at the same time

对着背影说爱祢 提交于 2019-11-28 18:35:40
Would it be possible in SQL Server 2008 to have a table created with 2 columns that are at the same time primary and foreign keys? If yes, how would such a code look like? I've searched and came up with nothing. Sure, no problem: CREATE TABLE dbo.[User] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [Group] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [UserToGroup] ( UserId int NOT NULL, GroupId int NOT NULL, PRIMARY KEY CLUSTERED ( UserId, GroupId ), FOREIGN KEY ( UserId ) REFERENCES [User] ( Id ) ON UPDATE NO

Determine a table's primary key using TSQL

孤者浪人 提交于 2019-11-28 18:33:11
I'd like to determine the primary key of a table using TSQL (stored procedure or system table is fine). Is there such a mechanism in SQL Server (2005 or 2008)? Stuart Ainsworth This should get you started: SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name WHERE tc.TABLE_NAME = 'TableName' and tc.CONSTRAINT_TYPE = 'Primary Key' How about sp_pkeys 'TableName' Here's one based on system tables from SQL 2005 (99% sure it'd work in 2008). This will list all PKs for all user-defined tables, with all

ADO.NET Entity Framework: Update Wizard will not add tables

五迷三道 提交于 2019-11-28 18:04:31
I added a new ADO.Net Entity Data Model into my project and used the Update Wizard to add tables into the model. Five of the selected tables were added to the design surface. Two other tables will not add. I select them in the wizard and click Finish, yet they never show up on the design surface. Is this a bug, or are there some situations where a table cannot be added to the model (by design)? UPDATE: The XML (*.edmx) reveals the problem. <!--Errors Found During Generation: warning 6013: The table/view 'FooBar.dbo.Roles' does not have a primary key defined and no valid primary key could be

SQL Server: how to add new identity column and populate column with ids?

和自甴很熟 提交于 2019-11-28 17:10:05
I have a table with huge amount of data. I'd like to add extra column id and use it as a primary key. What is the better way to fill this column with values from one 1 to row count Currently I'm using cursor and updating rows one by one. It takes hours. Is there a way to do that quicker? Thank you Just do it like this: ALTER TABLE dbo.YourTable ADD ID INT IDENTITY(1,1) and the column will be created and automatically populated with the integer values (as Aaron Bertrand points out in his comment - you don't have any control over which row gets what value - SQL Server handles that on its own and

How can set two primary key fields for my models in Django

↘锁芯ラ 提交于 2019-11-28 16:58:50
I have a model like this: class Hop(models.Model): migration = models.ForeignKey('Migration') host = models.ForeignKey(User, related_name='host_set') I want to migration and host both together be the primary key. I would implement this slightly differently. I would use a default primary key (auto field), and use the meta class property, unique_together class Hop(models.Model): migration = models.ForeignKey('Migration') host = models.ForeignKey(User, related_name='host_set') class Meta: unique_together = (("migration", "host"),) It would act as a "surrogate" primary key column. If you really

Creating tables and problems with primary key in Rails

有些话、适合烂在心里 提交于 2019-11-28 16:58:33
When I try to run the following code in Rails using Mysql2 as database manager: rake db:migrate I obtain the following error: rake aborted! "Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL:" Why do I get this error, if the primary key in a table by default is NOT "null"? Migration code, however : class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string "first_name" t.timestamps end end end Bater Chen I had a same problem before, and I solved according to here https://github.com/rails/rails/pull/13247#issuecomment-32425844 With Rails 2.3.5, MySQL

Django BigInteger auto-increment field as primary key?

十年热恋 提交于 2019-11-28 16:33:38
I'm currently building a project which involves a lot of collective intelligence. Every user visiting the web site gets created a unique profile and their data is later used to calculate best matches for themselves and other users. By default, Django creates an INT(11) id field to handle models primary keys. I'm concerned with this being overflown very quickly (i.e. ~2.4b devices visiting the page without prior cookie set up). How can I change it to be represented as BIGINT in MySQL and long() inside Django itself? I've found I could do the following ( http://docs.djangoproject.com/en/dev/ref