primary-key-design

MySQL - Using foreign key as primary key too

蹲街弑〆低调 提交于 2019-12-20 11:09:08
问题 I have table 1 with a primary key user_id and table 2 where user_id is a foreign key. Only 1 record per user_id can exist in table 2, and no record can exist without it. QUESTION: Can user_id in table 2 be both foreign and primary key at the same time, and if yes, is it a good idea, what are pros/cons? 回答1: Yes, you can do this (and you should, from a database design point of view). However, consider what it means if user_id is the primary key on table 2. You are in effect saying that each

MySQL - using String as Primary Key

萝らか妹 提交于 2019-12-20 11:04:23
问题 I saw a similar post on Stack Overflow already, but wasn't quite satisfied. Let's say I offer a Web service. http://foo.com/SERVICEID SERVICEID is a unique String ID used to reference the service (base 64, lower/uppercase + numbers), similar to how URL shortener services generate ID's for a URL. I understand that there are inherent performance issues with comparing strings versus integers. But I am curious of how to maximally optimize a primary key of type String. I am using MySQL, (currently

Database Design and the use of non-numeric Primary Keys

拜拜、爱过 提交于 2019-12-18 14:56:52
问题 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`

Database Design and the use of non-numeric Primary Keys

荒凉一梦 提交于 2019-12-18 14:56:03
问题 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`

MongoDB and composite primary keys

时间秒杀一切 提交于 2019-12-17 22:12:44
问题 I'm trying to determine the best way to deal with a composite primary key in a mongo db. The main key for interacting with the data in this system is made up of 2 uuids. The combination of uuids is guaranteed to be unique, but neither of the individual uuids is. I see a couple of ways of managing this: Use an object for the primary key that is made up of 2 values (as suggested here) Use a standard auto-generated mongo object id as the primary key, store my key in two separate fields, and then

using Natural key as the ID of DomainObject or GUID + auto-increment Domain Driven Design

不羁岁月 提交于 2019-12-12 11:04:05
问题 I've been reading a lot of articles about DDD and noticed that most are using GUID as their ID when persisting to a database. They say that GUID scales well and auto incrementing ID's are a big no-no when it comes to scalability. Im confused now whether to use GUID or auto-increment . Basically the Domain is about membership system (binary tree). (tracking of register members) The first requirement is that we should have something that uniquely identifies them in the system (we call it

MySQL. Primary key in a relational table. Unique id or multiple unique key?

三世轮回 提交于 2019-12-11 08:44:46
问题 Primary key in relational tables. Composite primary key or unique primary key in those pure relational tables? Which design would you recommend to use in MySQL for high performance? See diagram Technical advantages and disadvantages! Thanks everyone! 回答1: It really depends on the type of query you're doing... If you add an extra surrogate, you'll end up doing two unique checks instead of a single one for every insert, update and delete. That makes the composite key sound right. But if you're

Output Inserted or deleted in SQL Server

柔情痞子 提交于 2019-12-11 07:07:22
问题 I have a SalesTransaction and an Invoice table: Create Table SalesTransaction ( SalesTransactionId int Identity(1,1) Primary Key, CustomerId int Foreign key references Customer(CustomerId) , ProductId int Foreign key references Product(ProductID), Price money, Quantity int, Total money, InvoiceId int ) and Create Table Invoice ( InvoiceId int Primary Key, InvoiceAmount money Not Null , BalanceAmount money, AmountPaid money Not Null ) What I want is the Invoiceid from Invoice table to tag

MySQL - Using foreign key as primary key too

偶尔善良 提交于 2019-12-03 01:11:15
I have table 1 with a primary key user_id and table 2 where user_id is a foreign key. Only 1 record per user_id can exist in table 2, and no record can exist without it. QUESTION: Can user_id in table 2 be both foreign and primary key at the same time, and if yes, is it a good idea, what are pros/cons? Yes, you can do this (and you should, from a database design point of view). However, consider what it means if user_id is the primary key on table 2. You are in effect saying that each row in table 2 corresponds to a user, but you already have a table where each row corresponds to a user: table

MySQL - using String as Primary Key

﹥>﹥吖頭↗ 提交于 2019-12-03 01:07:26
I saw a similar post on Stack Overflow already, but wasn't quite satisfied. Let's say I offer a Web service. http://foo.com/SERVICEID SERVICEID is a unique String ID used to reference the service (base 64, lower/uppercase + numbers), similar to how URL shortener services generate ID's for a URL. I understand that there are inherent performance issues with comparing strings versus integers. But I am curious of how to maximally optimize a primary key of type String. I am using MySQL, (currently using MyISAM engine, though I admittedly don't understand all the engine differences). Thanks. update