primary-key

Python Pandas to_sql, how to create a table with a primary key?

删除回忆录丶 提交于 2019-11-27 20:07:18
I would like to create a MySQL table with Pandas' to_sql function which has a primary key (it is usually kind of good to have a primary key in a mysql table) as so: group_export.to_sql(con = db, name = config.table_group_export, if_exists = 'replace', flavor = 'mysql', index = False) but this creates a table without any primary key, (or even without any index). The documentation mentions the parameter 'index_label' which combined with the 'index' parameter could be used to create an index but doesn't mention any option for primary keys. Documentation Disclaimer: this answer is more

Indexes and multi column primary keys

三世轮回 提交于 2019-11-27 19:42:37
Went searching and didn't find the answer to this specific noob question. My apologies if I missed it. In a MySQL database I have a table with the following primary key PRIMARY KEY id (invoice, item) In my application I will also frequently be selecting on "item" by itself and less frequently on only "invoice". I'm assuming I would benefit from indexes on these columns. MySQL does not complain when I define the following: INDEX (invoice), INDEX (item), PRIMARY KEY id (invoice, item) But I don't see any evidence (using DESCRIBE -- the only way I know how to look) that separate indexes have been

MySQL non primary foreign key

梦想与她 提交于 2019-11-27 18:56:44
问题 I'm a bit of a newbie and I can't get my head around primary keys as foreign keys. To me, foreign keys are meant to connect two rows of a table together. Therefore, it would make logical sense to use the, for example, username of the user table as a foreign key in the picture table. This means that the picture in that row belongs to the specified user. However, it appears that general practice favors using meaningless numbers as primary IDs. Furthermore the foreign key must/should refer to

Slow INSERT into InnoDB table with random PRIMARY KEY column's value

♀尐吖头ヾ 提交于 2019-11-27 18:40:53
问题 For my website I use the PHP API for Flickr ( http://www.flickr.com/services/api/ ). This API provides several useful methods to get photos around particular GPS positions. The call to API methods looks like URL with specific parameters like latitude, longitude, API key, radius, sorting, etc. Say, it'll look like http://api.flickr.com/method?lat=0.0&lon=0.0&radius=10 My website makes over 200,000 calls to API to generate several pages with pictures from Flickr. It is a pritty hard push on API

Resetting the primary key to 1 after deleting all the data

我的未来我决定 提交于 2019-11-27 18:25:26
问题 So i have MySql and i have a table user with a user_id column and it is the primary key and auto incremented. Now when i delete all my data from the table and add the new one, the user_id does not start from 1 but from the number it had before deletion. What if i want to reset it without dropping the whole table and creating it again. 回答1: ALTER TABLE some_table AUTO_INCREMENT=1 So some_table would be the table you want to alter. You could also use: TRUNCATE TABLE some_table This will reset

MySQL - how to use VARCHAR as AUTO INCREMENT Primary Key

牧云@^-^@ 提交于 2019-11-27 18:20:59
问题 I am using a VARCHAR as my primary key. I want to auto increment it (base 62, lower/upper case, numbers), However, the below code fails (for obvious reasons): CREATE TABLE IF NOT EXISTS `campaign` ( `account_id` BIGINT(20) NOT NULL, `type` SMALLINT(5) NOT NULL, `id` VARCHAR(16) NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; however, this works: CREATE TABLE IF NOT EXISTS `campaign` ( `account_id` BIGINT(20) NOT NULL, `type` SMALLINT(5) NOT

sql primary key and index

[亡魂溺海] 提交于 2019-11-27 17:33:42
Say I have an ID row (int) in a database set as the primary key. If I query off the ID often do I also need to index it? Or does it being a primary key mean it's already indexed? Reason I ask is because in MS SQL Server I can create an index on this ID, which as I stated is my primary key. Edit: an additional question - will it do any harm to additionally index the primary key? You are right, it's confusing that SQL Server allows you to create duplicate indexes on the same field(s). But the fact that you can create another doesn't indicate that the PK index doesn't also already exist. The

What is the difference between Primary Key and unique key constraint?

谁说我不能喝 提交于 2019-11-27 17:22:30
问题 What is the difference between Primary key And unique Key constraint ? What's the use of it?? 回答1: Both are used to denote candidate keys for a table. You can only have one primary key for a table so would just need to pick one if you have multiple candidates. Either can be used in Foreign Key constraints. In SQL Server the Primary Key columns cannot be nullable. Columns used in Unique Key constraints can be. By default in SQL Server the Primary Key will become the clustered index if it is

Composite primary key

假装没事ソ 提交于 2019-11-27 17:21:32
问题 I am working on the design of a database that will be used to store data that originates from a number of different sources. The instances I am storing are assigned unique IDs by the original sources. Each instance I store should contain information about the source it came from, along with the ID it was associated by this source. As an example, consider the following table that illustrates the problem: ---------------------------------------------------------------- | source_id | id_on

Should a Composite Primary Key be clustered in SQL Server?

*爱你&永不变心* 提交于 2019-11-27 16:23:48
问题 Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int not null, child_product_id int not null, quantity int not null ) I'm considering a composite primary key containing the two product_id columns (I'll definitely want a unique constraint) as opposed to a separate unique ID column. Question is, from a performance point of view, should that primary key be clustered? Should I also create an index on each ID column so that lookups