primary-key

Example of when you should use a foreign key that points to a candidate key, not a primary key?

别等时光非礼了梦想. 提交于 2019-12-04 19:47:28
From my reading, I understand what makes a good primary key, what a foreign key is and what a candidate key is. I've read in several different books and sources that: A foreign key must point to a candidate key (or primary) A foreign key almost always points to a primary key The authors of the sources always say something along the lines of, "while foreign keys can point at a candidate key (not primary) they seem to". Are there any examples of why you might choose a candidate key and not the primary key? Thank you Primary keys (PKs) have no role in relational theory. (Eg integrity or

what's the performance difference between int and varchar for primary keys

六眼飞鱼酱① 提交于 2019-12-04 18:34:19
I need to create a primary key scheme for a system that will need peer to peer replication. So I'm planning to combine a unique system ID and a sequential number in some way to come up with unique ID's. I want to make sure I'll never run out of ID's, so I'm thinking about using a varchar field, since I could always add another character if I start running out. But I've read that integers are better optimized for this. So I have some questions... 1) Are integers really better optimized? And if they are, how much of a performance difference is there between varchars and integers? I'm going to

How to automatically create the primary key that has the following series A001000001 … A001xxxxxx in SQL?

非 Y 不嫁゛ 提交于 2019-12-04 17:48:50
I want to create a primary key(auto-increment) which is start with A001000001 . Here A001 would be constant and 000001 would be iterate. I want rows like this: How can I do it using SQL query? For SQL Server (you didn't clearly specify which RDBMS you're using), my suggestion would be: add a INT IDENTITY column to your table - and quite frankly, I would make that column the primary key add a computed column to your table that does this "prefixing" Something like: CREATE TABLE dbo.YourTable ( ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, EmpID AS 'A001' + RIGHT('0000000' + CAST(ID AS VARCHAR(10))

Is primary key always clustered?

烂漫一生 提交于 2019-12-04 17:27:21
问题 Please clear my doubt about this, In SQL Server (2000 and above) is primary key automatically cluster indexed or do we have choice to have non-clustered index on primary key? 回答1: Nope, it can be nonclustered. However, if you don't explicitly define it as nonclustered and there is no clustered index on the table, it'll be created as clustered. 回答2: One might also add that frequently it's BAD to allow the primary key to be clustered. In particular, when the primary key is assigned by an

Why the auto_increment id does not increase one by one, how to set it?

谁说我不能喝 提交于 2019-12-04 17:03:58
问题 I have a MariaDB Galera Cluster(3 nodes), I set uid to increase automatically and be the primary key of the table as `uid | int(11) | NO | PRI | NULL | auto_increment`. MariaDB [hello_cluster]> select uid from table order by uid limit 10; +-----+ | uid | +-----+ | 3 | | 6 | | 9 | | 12 | | 15 | | 18 | | 21 | | 24 | | 27 | | 30 | +-----+ I tried the following command, and it does not work alter table uid AUTO_INCREMENT=1 回答1: This is by design and is reported in MariaDB Galera Cluster - Known

How do I rename a primary key column in MySQL?

我的未来我决定 提交于 2019-12-04 15:58:55
问题 How do I rename a primary key column in MySQL? 回答1: it's no different than altering any other column -- ALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT this changes the column keyfield in table pkey to be called keyfield2 -- you have to supply the definition afterwards, as usual. 回答2: Maybe you have a foreign key constraint in place. You can disable those by SET foreign_key_constraints=0 but you have to remember to update the database afterwards. 回答3: Leave

MySql - Is primary key unique by default?

て烟熏妆下的殇ゞ 提交于 2019-12-04 15:08:03
问题 If I define a column as a primary key in MySql, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)? I saw this question What is the difference b/w Primary Key and Unique Key that explain the difference between the two but doesn't exactly answer my question. Does PK is UK by default or I need to explicitly define it. 回答1: Primary key is always unique in every SQL. You dont have to explicitly define it as UNIQUE. On a side note: You

Is there any benefit to creating and index on a primary key?

蹲街弑〆低调 提交于 2019-12-04 14:50:27
I checked this SO post: What's the difference between primary key, unique key, and index in MySQL? and found the statement: Also note that columns defined as primary keys or unique keys are automatically indexed in MySQL. Based on this, I have two questions: Am I safe in assuming that there is no performance benefit to creating an index on a primary key itself because the primary key, by design, is an index? Perhaps the more important question: If you are doing the classic example people cite, doing SELECT based on lastName and firstName, and that table has a primary key that you SELECT by

Does the foreign keys automatically get updated as primary table is updated?

☆樱花仙子☆ 提交于 2019-12-04 13:30:04
问题 Above is my simple database design, just wanted to gain information about how things happen as I'm really new at database. Following are my questions: as I update wall_id in walls table, does the wall_id in wall_categories table also get updated? as the wall_id in wall_categories table references to wall_id in walls table. same with desktop_id in walls table since it is a foreign key referencing to desktop_id in desktop_wall table, so when I update desktop_id in walls table does the desktop

Are there any good reasons to have a database table without an integer primary key?

泄露秘密 提交于 2019-12-04 13:21:34
问题 Although I'm guilty of this crime, it seems to me there can't be any good reason for a table to not have an identity field primary key. Pros: - whether you want to or not, you can now uniquely identify every row in your table which previously you could not do - you can't do sql replication without a primary key on your table Cons: - an extra 32 bits for each row of your table Consider for example the case where you need to store user settings in a table in your database. You have a column for