primary-key

create foreign key without a primary key

拈花ヽ惹草 提交于 2019-11-30 12:23:25
Why is it necessary to have a primary key on a column of one table to which a column of the other table having foreign key references. create table D(Did int) create table E(Eid int foreign key references D(Did)) The above query gives error: There are no primary or candidate keys in the referenced table 'D' that match the referencing column list in the foreign key 'FK__E__Eid__79C80F94'. Easy. If you have 2 values the same in the parent table, how do you know which one to associate child rows to? One side of foreign key must be unambiguous The requirement is also "unique key", not just a

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

流过昼夜 提交于 2019-11-30 12:15:55
问题 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? 回答1: I think he is right ( for the

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

岁酱吖の 提交于 2019-11-30 12:07:12
问题 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? 回答1: 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

How to choose the clustered index in SQL Server?

限于喜欢 提交于 2019-11-30 12:03:58
Usually the clustered index is created in SQL Server Management Studio by setting the primary key, however my recent question about PK <-> clustered index ( Meaning of Primary Key to Microsoft SQL Server 2008 ) has shown that it is not necessary to set PK and clustered index to be equal. So how should we choose clustered indexes then? Let's have the following example: create table Customers (ID int, ...) create table Orders (ID int, CustomerID int) We would usually create the PK/CI on both ID columns but i thought about creating it for Orders in CustomerID. Is that the best choice? According

Auto-increment on Azure Table Storage

我只是一个虾纸丫 提交于 2019-11-30 12:01:33
问题 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

Database Design and the use of non-numeric Primary Keys

强颜欢笑 提交于 2019-11-30 11:34:34
I'm currently in the process of designing the database tables for a customer & website management application. My question is in regards to the use of primary keys as functional parts of a table (and not assigning "ID" numbers to every table just because). For example, here are four related tables from the database so far, one of which uses the traditional primary key number, the others which use unique names as the primary key: -- -- website -- CREATE TABLE IF NOT EXISTS `website` ( `name` varchar(126) NOT NULL, `client_id` int(11) NOT NULL, `date_created` timestamp NOT NULL default CURRENT

How large can an id get in postgresql

妖精的绣舞 提交于 2019-11-30 11:30:01
I am using postgresql, and was wondering how large id INTEGER PRIMARY KEY can get compared to id SERIAL PRIMARY KEY In java an int is 4 bytes (32 bits) so it can get up to 2,147,483,647. Is this the case in postgresql? If so does that mean I cannot go past 2,147,483,647 rows? Hart CO Here is a handy chart for PostgreSQL: Name Storage Size Description Range smallint 2 bytes small-range integer -32768 to +32767 integer 4 bytes usual choice for integer -2147483648 to +2147483647 bigint 8 bytes large-range integer -9223372036854775808 to 9223372036854775807 serial 4 bytes autoincrementing integer

Android: Use UUID as primary key in SQLite

人盡茶涼 提交于 2019-11-30 10:56:44
问题 My app needs to get synced with other app users (on there own devices). I also want to support offline editing, that are synchronized to the other collaborative users when the user gets connected to the internet. So the User A changes (while he is offline) some data (in ohter words he would update database entries) or add new records to the database. When User A gets connected to the internet, all changes and new records are delivered to the other collaborative Users. So User B will get the

The differences between INT and UUID in MySQL

萝らか妹 提交于 2019-11-30 10:44:24
问题 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? 回答1: 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

Can a database attribute be primary and foreign key?

[亡魂溺海] 提交于 2019-11-30 09:48:48
I have 2 tables, User and Employee . Each user is given a User_ID and that is a primary key in the User table and a foreign key in the Employee table. Can that attribute in the Employee table also be a primary key? If you have a one-to-one relation between two tables, then the primary key of the details table is a foreign key as well. master detail (1 : 1) +----------+ 1:1 +-------------+ | PK id |<---o| PK FK id | +----------+ +-------------+ | col1 | | col1 | | col2 | | col2 | | etc. | | etc. | +----------+ +-------------+ If you have a m-to-n relation, the junction table has columns