primary-key

How to create foreign key that is also a primary key in MySQL?

我们两清 提交于 2019-11-30 03:01:30
This should be a fairly straightforward question, but I'm unable to find an easy answer. How do you create a foreign key that is also a primary key in MySQL? Here's my current attempt: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY KEY(sale_id), discount DOUBLE, type VARCHAR(255), price DOUBLE, ); CREATE TABLE Normal_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id); ); CREATE TABLE Special_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id); ); What am I missing here? Thanks in advance. Add FOREIGN KEY (sale_id) REFERENCES Sale(sale_id) to each foreign table: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY

Auto-increment on Azure Table Storage

ⅰ亾dé卋堺 提交于 2019-11-30 02:25:31
I am currently developing an application for Azure Table Storage. In that application I have table which will have relatively few inserts (a couple of thousand/day) and the primary key of these entities will be used in another table, which will have billions of rows. Therefore I am looking for a way to use an auto-incremented integer, instead of GUID, as primary key in the small table (since it will save lots of storage and scalability of the inserts is not really an issue). There've been some discussions on the topic, e.g. on http://social.msdn.microsoft.com/Forums/en/windowsazure/thread

What are the pros and cons of using multi column primary keys?

岁酱吖の 提交于 2019-11-30 02:04:43
I would like to see an example of: When this is appropriate When this is not appropriate Is there a time when the choice of database would make a difference to the above examples? This really seems to be a question about surrogate keys, which are always either an auto-incrementing number or GUID and hence a single column, vs. natural keys, which often require multiple pieces of information in order to be truly unique. If you are able to have a natural key that is only one column, then the point is obviously moot anyway. Some people will insist on only using one or the other. Spend sufficient

EF4.1 Code First Complex Type as primary key

半腔热情 提交于 2019-11-30 01:56:56
问题 I'm currently trying to implement the repositories for my domain objects with the RC of Entity Framework 4.1 and its code first approach. Now I have a domain entity "Voyage" which has a unique identifier encapsulated in the type "VoyageNumber" public class VoyageNumber { private readonly string number; public VoyageNumber(string number) { Validate.NotNull(number, "VoyageNumber is required"); this.number = number; } public string Id { get { return number; } } Now I get an exception when i do

Is it bad to use user name as primary key in database design?

被刻印的时光 ゝ 提交于 2019-11-30 01:50:25
I was told by a friend: What unique key do you use? I hope you are not saving the entire user name --- this will use up too much table space! Assign an unique userID to each (unique) userNAME and save this userID (should be INTEGER UNSIGNED auto_increment or BIGINT UNSIGNED auto_increment). Don't forget to create a reference FOREIGN KEY ( userID ) REFERENCES usertable ( userID ) in all tables using the userID. Is the above statement correct? Why or why not? I think he is right ( for the wrong reason) because primary key cannot change, but username can change. So you should use userid because

MySQL only insert new row if combination of columns (which allow duplicates) is unique

假装没事ソ 提交于 2019-11-29 23:48:54
问题 Since IF EXISTS isn't supported by MySQL I am struggling to think of the syntax for doing something like the following pseudo in MySQL: IF ((select count(*) from table where col1='var1' AND col2='var2' AND col3='var3' AND col4='var4' AND col5='var5')>0) then combination of vars exist in record - do a thing; ELSE combination of vars does not exist in record - insert record; END IF; I should have thought CASE would suit this but for life of me I'm unable to think of the correct syntax. I'd use

What is the difference between a primary key and a index key

好久不见. 提交于 2019-11-29 22:58:16
Can anyone tell me what is the difference between a primary key and index key. And when to use which? A primary key is a special kind of index in that: there can be only one; it cannot be nullable; and it must be unique. You tend to use the primary key as the most natural unique identifier for a row (such as social security number, employee ID and so forth, although there is a school of thought that you should always use an artificial surrogate key for this). Indexes, on the other hand, can be used for fast retrieval based on other columns. For example, an employee database may have your

The differences between INT and UUID in MySQL

被刻印的时光 ゝ 提交于 2019-11-29 21:49:10
If I set the primary key to be INT type ( AUTO_INCREMENT ) or set it in UUID , what is the difference between these two in the database performance ( SELECT , INSERT etc) and why? Nikos M. UUID returns a universal unique identifier (hopefuly also unique if imported to another DB as well). To quote from MySQL doc (emphasis mine): A UUID is designed as a number that is globally unique in space and time . Two calls to UUID() are expected to generate two different values, even if these calls are performed on two separate computers that are not connected to each other. On the other hand a simply

Best way to reset an Oracle sequence to the next value in an existing column?

两盒软妹~` 提交于 2019-11-29 20:09:05
For some reason, people in the past have inserted data without using sequence.NEXTVAL. So when I go to use sequence.NEXTVAL in order to populate a table, I get a PK violation, since that number is already in use in the table. How can I update the next value so that it is usable? Right now, I'm just inserting over and over until it's successful ( INSERT INTO tbl (pk) VALUES (sequence.NEXTVAL) ), and that syncs up the nextval. Basanth Roy You can temporarily increase the cache size and do one dummy select and then reset the cache size back to 1. So for example ALTER SEQUENCE mysequence INCREMENT

Is it a bad idea to use GUIDs as primary keys in MS SQL?

妖精的绣舞 提交于 2019-11-29 19:54:27
We have a system that uses UniqueIdentifier as the primary key of each of the tables. It has been brought to our attention that this is a bad idea. I have seen similar post on the subject but I am interested in any MS SQL performance and other potential problems I may encounter due to this decision. Chris Craft There are pros and cons: This article covers everything. GUID Pros Unique across every table, every database, every server Allows easy merging of records from different databases Allows easy distribution of databases across multiple servers You can generate IDs anywhere, instead of