primary-key

Do link tables need a meaningless primary key field?

喜夏-厌秋 提交于 2019-11-28 07:22:54
I am working on a couple of link tables and I got to thinking (Danger Will Robinson, Danger) what are the possible structures of a link table and what are their pro's and con's. I came up with a few possible strictures for the link table: Traditional 3 column model id - auto-numbered PRIMARY table1fk - foreign key table2fk - foreign key It's a classic, in most of the books, 'nuff said. Indexed 3 column model id - auto-numbered PRIMARY table1fk - foreign key INDEX ('table1fk') table2fk - foreign key INDEX ('table2fk') In my own experience, the fields that you are querying against are not

newid() vs newsequentialid() What are the differences/pros and cons?

做~自己de王妃 提交于 2019-11-28 06:18:10
In a database where all of your primary keys are GUIDs, what are the differences/implications and/or pros and cons using newid() versus newsequentialid() as the "default value or binding". The only difference that I know of is that newid() creates a new random GUID as opposed to newsequentialid() creates a new GUID based on the last one that is in the table in an incremented fashion. As I understand it, when you perform an insert in a row the DB, it will be inserted in order relative to the other PK's in the table. With a normal guid, this could be anywhere in the table. A newsequentialid()

What is the difference between a primary key and a unique constraint?

爷,独闯天下 提交于 2019-11-28 06:12:56
Someone asked me this question on an interview... Andrew Hare A primary key is a unique field on a table but it is special in the sense that the table considers that row as its key. This means that other tables can use this field to create foreign key relationships to themselves. A unique constraint simply means that a particular field must be unique. Primary keys can't be null. Unique keys can. Primary key can not be null but unique can have only one null value. Primary key create the cluster index automatically but unique key not. A table can have only one primary key but unique key more

When should I use primary key or index?

牧云@^-^@ 提交于 2019-11-28 04:20:16
When should I use a primary key or an index? What are their differences and which is the best? 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 consists of must be NOT NULL A table can have multiple indexes, and indexes are not necessarily UNIQUE . Indexes

Native primary key or auto generated one?

随声附和 提交于 2019-11-28 04:19:56
问题 As a rule is it better to use native primary keys (ie existing columns or combination of columns) or set your primary key to an auto generating row of integers? EDIT: It has been pointed out to me that this very similar to this question. The consensus here is to use surrogate keys, which was my natural inclination, but my boss told me I should also use natural keys where possible. His advice may be best for this particular application, as Name in row uniquely identifies it and we have a need

How to reset the primary key of a table?

北城余情 提交于 2019-11-28 03:54:34
In my table tbphotos I had a 100 records. I then deleted all the records and now that I want to restart data entry I see that my primary key doesn't start from 1, but it starts from 101, Is there any way to reset the primary key? I am using MySQL administrator account. alter table foo AUTO_INCREMENT = 1 You can reset the auto-increment like this: ALTER TABLE tablename AUTO_INCREMENT = 1 But if you are relying on the autoincrement values, your program is very fragile. If you need to assign consecutive numbers to your records for your program to work you should create a separate column for that,

Add primary key to existing table

删除回忆录丶 提交于 2019-11-28 03:36:59
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) drop constraint and recreate it alter table Persion drop CONSTRAINT <constraint_name> alter table Persion add primary key (persionId,Pname,PMID) edit: you can find the constraint name by using the query below: select OBJECT_NAME(OBJECT_ID) AS

Must database primary keys be integers?

我的未来我决定 提交于 2019-11-28 03:10:37
问题 I always see MySQL database primary keys as integers. Is that because primary keys must be integers, or because of ease of use when setting auto_increment on the column? I am wondering just in case I want my primary key to be a varchar in the future. 回答1: You can use varchar as well as long as you make sure that each one is unique . This however isn't ideal (see article link below for more info). What you are looking for is called natural key but a primary key with auto-increment and handled

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

耗尽温柔 提交于 2019-11-28 01:32:27
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? 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 macro, click the "Before Change" button on the "Table" tab of the ribbon when the table is open in Datasheet

sql swap primary key values

余生颓废 提交于 2019-11-28 00:46:06
is it possible to swap primary key values between two datasets? If so, how would one do that? Unreason 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 swap the values of all the other columns. Caveat: The reason why the t.id = t2.id, t2.id = t.id works