primary-key

How to to create unique random integer ID for primary key for table?

安稳与你 提交于 2019-11-30 09:13:44
I was wondering if anybody knew a good way to create a unique random integer id for a primary key for a table. I'm using MySQL. The value has to be integer. byte_slave If your're open to suggestions and you can implement it, use UUIDs. MySQL's UUID() function will return a 36 chars value which can be used for ID . If you want to use integer, still, I think you need to create a function getRandID() that you will use in the INSERT statement. This function needs to use random + check of existing ids to return one that is not used before. Check RAND() function for MySQL. In response to: "Because I

Insert Where Not Exists-Without Primary Key

十年热恋 提交于 2019-11-30 09:05:33
I have 3 tables: dentists, groups, and groupdentlink. Many dentists link to many groups through the groupdentlink table. So I'm trying to make a query where it will insert rows into groupdentlink (linking all dentists in the state with all the groups in the state) but only if those rows don't already exist. In a nutshell I want to add new rows without overwriting existing ones or duplicating them. So the intent of the query is something like: INSERT INTO groupdentlink (f_dent_id, f_group_id, f_schedule_id) VALUES ('$_POST[id]', '$groupid', '$scheduleid') WHERE NOT EXISTS ('$_POST[id]', '

Why can I create a table with PRIMARY KEY on a nullable column?

柔情痞子 提交于 2019-11-30 08:53:31
问题 The following code creates a table without raising any errors: CREATE TABLE test( ID INTEGER NULL, CONSTRAINT PK_test PRIMARY KEY(ID) ) Note that I cannot insert a NULL, as expected: INSERT INTO test VALUES(1),(NULL) ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null). ********** Error ********** ERROR: null value in column "id" violates not-null constraint SQL state: 23502 Detail: Failing row contains (null). Why can I create a table with a self

Mysql - how to set auto-increment to start from zero

有些话、适合烂在心里 提交于 2019-11-30 08:37:43
问题 I just want my mysql table id(primary key) to start from 0.. As I have seen, I used ALTER TABLE yourtable AUTO_INCREMENT =0 but it starts from 1..What is that I need to do? Edit1 I also emptied my table with truncate option 回答1: SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO' NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior

UUID versus auto increment number for primary key

坚强是说给别人听的谎言 提交于 2019-11-30 08:34:21
Why should I choose UUID over an auto increment number for my entity's primary key? What are the pros and cons? Andrey and Mjg both had good points, but I would add a related performance issue that is significant. With the decoupling of database and key generation also allows applications that have complex relationships between objects to create them all with the keys in place, so that bulk inserts are possible. In the case of auto-increment, all of the objects that own relationships (ie the tables with foreign keys) have to wait for the other side of the relationship (ie the table the foreign

Is ID column required in SQL?

江枫思渺然 提交于 2019-11-30 08:25:53
Traditionally I have always used an ID column in SQL (mostly mysql and postgresql). However I am wondering if it is really necessary if the rest of the columns in each row make in unique. In my latest project I have the "ID" column set as my primary key, however I never call it or use it in any way, as the data in the row makes it unique and is much more useful for me. So, if every row in a SQL table is unique, does it need a primary key ID table, and are there ant performance changes with or without one? Thanks! EDIT/Additional info: The specific example that made me ask this question is a

Two foreign keys instead of primary

爱⌒轻易说出口 提交于 2019-11-30 08:23:59
问题 I was wondering, is there any possibility to create a table without a primary key, but with two foreign keys, where the foreign keys pairs are always different? For example, a STOCK table with item_id and warehouse_id as foreign keys from ITEMS and WAREHOUSES tables. So same item can be in different warehouses. The view of the table: item_id warehouse_id quantity 10 200 1000 10 201 3000 10 202 10000 11 200 7000 11 202 2000 12 203 5000 Or do i have to create unused primary key field with auto

PRIMARY KEY issue with creating tables in Rails using rake db:migrate command with mysql

有些话、适合烂在心里 提交于 2019-11-30 08:05:17
My version of rails is 4.0.0, my version of mysql is Ver 14.14 Distrib 5.7.9, for Win64 (x86_64). I am operating of an older version of rails as I was getting some clashes with the mysql as per my previous question Here . (check Kalelc 's approved answer for my recourse) upon running rake db:migrate I get the following error == CreateUsers: migrating ==================================================== -- create_table(:users) rake aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key,

Should I index primary key column(s) in Oracle

北战南征 提交于 2019-11-30 07:54:03
问题 I've recently stopped to think that Primary Keys are not indexes, they're a combination of Unique and Null constraints. And till now, I've never created index for PK columns. My question is if I should create index for PK columns if this column is going to be used in the WHERE part of many queries. 回答1: Oracle will create an index for you, or can use an existing one. Whether a unique or non-unique index is used is up to you. http://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm

mysql Multiple Foreign Keys in a Table to the Same Primary Key

亡梦爱人 提交于 2019-11-30 07:05:26
I have a table user with userID as the primary key. I have another table called Friends . In the Friends table, I have two Users as friends represented by the columns UserID and FrndID where both UserID and FrndID should be a userID in table user . I want to enforce data integrity. Could I use something like this? ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`, `friendId`) REFERENCES `users` (`userId`, `userId`) ON DELETE CASCADE ON UPDATE CASCADE; I want to know is REFERENCES users ( userId , userId ) referencing a column multiple times correctly? The reason I am not creating 2 separate