primary-key

How can I have a primary key in a VIEW (a key that doesn't depend on source tables)

99封情书 提交于 2019-12-06 03:43:16
I'm creating a VIEW out of 3 TABLES. Each TABLE has it's primary key. However, in the VIEW in order to have a primary key, I'll have to use a composite primary key (combination of primary keys of 3 TABLES). I would however like to add a column in the VIEW as a primary key that is created just for the purpose of the VIEW. As part of the VIEW definition, it should be UNIQUE(autoincrement since it would be mostly an INT). How can I achieve this? I am using MySQL 5.1 you could use various methods to insert a unique ID to the view data, such as: SELECT @rownum:=@rownum+1 as id, mytable.* FROM

How to change value of primary key and update foreign key in the same time

痴心易碎 提交于 2019-12-06 01:29:45
问题 I have a record in table with wrong primary key. I want change it to correct value, but this value is used in many other tables. Is there any simple way to update primary key and foreign key at the same tim? 回答1: If the foreign keys are set to cascade changes then the value should change automatically. 回答2: Make sure that your foreign key relationships have ON UPDATE CASCADE specified, and the foreign key will automatically update to match the primary key. From Books Online: http://msdn

Resetting Primary key without deleting truncating table

霸气de小男生 提交于 2019-12-05 21:48:29
I have a table with a primary key, now without any reason I don't know when I am inserting data it is being loaded like this Pk_Col Some_Other_Col 1 A 2 B 3 C 1002 D 1003 E 1901 F Is there any way I can reset my table like below, without deleting/ truncating the table? Pk_Col Some_Other_Col 1 A 2 B 3 C 4 D 5 E 6 F You can't update the IDENTITY column so DELETE/INSERT is the only way. You can reseed the IDENTITY column and recreate the data, like this: DBCC CHECKIDENT ('dbo.tbl',RESEED,0); INSERT INTO dbo.tbl (Some_Other_Col) SELECT Some_Other_Col FROM (DELETE FROM tbl OUTPUT deleted.*) d; That

Change primary key value in Oracle

大憨熊 提交于 2019-12-05 20:52:10
Is there a way to change the value of a primary key which is referenced by another table as foreign key? An easier alternative is to insert a new row and delete the old one. (Update any referencing rows in other tables before you do the delete) There isn't an in-built UPDATE CASCADE if that's what you're after. You'd need to do something like disable any FK constraints; run UPDATE statements; re-enable the constraints. Note that updating Primary Keys is (usually always) a bad idea. You will need to disable the foreign key constraints before changing the primary key values, and then re-enable

Reset PK auto increment column

醉酒当歌 提交于 2019-12-05 19:56:06
I've been importing thousands of records multiple times in an effort to get the import running perfectly. As a result, now when I do the live import before release, the ID columns for the auto increment column are on around 300,000. Is there any easy way to 'reset' this once I have deleted all the data from these tables? I only want to for SEO reasons, the URL: Forum/1/Post Forum/35/Post Forum/5600/Post Looks a lot nicer and more concise (therefore more clickable in results) than Forum/300124/Post Forum/370321/Post Forum/450111/Post I'd rather not delete the column and reinsert the column as

Will SQL Server 2005 penalize me for using an nvarchar(50) as a primary key, instead of an integer?

本秂侑毒 提交于 2019-12-05 14:56:11
问题 I'm considering altering some tables to use nvarchar(50) as primary key instead of an int primary key. Using an int ID for a key really is irrelevant data, it's the string I'm interested in. What sort of performance hit will occur, or where do you research this? Other than cut and try that is. 回答1: You have hit upon one of the major "holy wars" of database design. The debate you're referring to is the "surrogate vs. natural key" argument that's been raging for as long as there have been

Performance: UUID vs auto-increment in cakephp-mysql

淺唱寂寞╮ 提交于 2019-12-05 14:41:32
I was searching if UUID generated by cakePHP (32 char long) is faster in performance compared to auto-increment. Comparison in both inserts and Select operation. Which one should I use UUID generated by cakePHP or by using simple auto-increment of MySQL Here's is a case-study I found but its not specific to cakePHP http://krow.livejournal.com/497839.html I doubt you're going to notice much of a performance issue in choice of primary key. Your bottlenecks will be somewhere else, almost guaranteed. By default, I recommend just using an auto-increment primary key. This makes sense -- you will

SQL Unique Key Syntax

♀尐吖头ヾ 提交于 2019-12-05 14:36:29
Very basic question; I'm very new to SQL and trying to decipher an example data base. In the below create table code, why does the define primary key syntax reference only the 'id' column once in parentheses but the unique key definition references the 'category' column twice? both before and within the parentheses. Seems like there is a simple answer to this but cant track one down: CREATE TABLE `categories` ( `id` SMALLINT NOT NULL AUTO_INCREMENT, `category` VARCHAR(30) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `category` (`category`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; It is the key name,

Violation of PRIMARY KEY constraint 'PK_Address'. etc…what am I doing wrong?

柔情痞子 提交于 2019-12-05 13:47:16
Evening all. I am fairly new to SQL but have been doing quite a bit of fooling around. I am following a guide I found online to learn SQL in 21 days and I am having a bit of trouble figuring out what the error I am receiving is causing. I am trying to INSERT data into an existing table. The Primary Key for this table is AddressID . The data I am trying to enter is in the code below: INSERT INTO [dbo].[Address] (AddressID,Street,City,State,ZipCode) VALUES (1,'2400 Broadway','New York','NY',11201), (2,'320 21st Street','Atlanta','GA',303), (3,'439 Skyline Blvd','Seattle','WA',98101), (4,'56 Park

Why use primary keys?

我的未来我决定 提交于 2019-12-05 13:24:24
What are primary keys used aside from identifying a unique column in a table? Couldn't this be done by simply using an autoincrement constraint on a column? I understand that PK and FK are used to relate different tables, but can't this be done by just using join? Basically what is the database doing to improve performance when joining using primary keys? Mostly for referential integrity with foreign keys,, When you have a PK it will also create an index behind the scenes and this way you don't need table scans when looking up values RDBMS providers are usually optimized to work with tables