primary-key

sqlite: multi-column primary key with an auto increment column

二次信任 提交于 2019-11-28 10:25:56
I basically want to convert a table from mysql to sqlite with the following scheme: create table items ( id integer auto_increment, version integer default 0, primary key (id, version) ); Essentially, I want ID to auto increment whenever I insert anything into the table, with VERSION starting off at 0, but still allow multiple items with the same ID as long as VERSION is different. I'm trying to replicate this behavior with Sqlite however, I can't seem to get table creation working. It seems like you are only allowed one column as autoincrement and it has to be the primary key. If I make ID

Store and reuse value returned by INSERT … RETURNING

我只是一个虾纸丫 提交于 2019-11-28 10:21:26
In PostgreSQL, it is possible to put RETURNING at the end of an INSERT statement to return, say, the row's primary key value when that value is automatically set by a SERIAL type. Question: How do I store this value in a variable that can be used to insert values into other tables? Note that I want to insert the generated id into multiple tables. A WITH clause is, as far as I understand, only useful for a single insert. I take it that this will probably have to be done in PHP. This is really the result of bad design; without a natural key, it is difficult to grab a unique row unless you have a

Problems setting a custom primary key in a Rails 4 migration

与世无争的帅哥 提交于 2019-11-28 10:18:05
I use postgresql 9.3, Ruby 2.0, Rails 4.0.0. After reading numerous questions on SO regarding setting the Primary key on a table, I generated and added the following migration: class CreateShareholders < ActiveRecord::Migration def change create_table :shareholders, { id: false, primary_key: :uid } do |t| t.integer :uid, limit: 8 t.string :name t.integer :shares t.timestamps end end end I also added self.primary_key = "uid" to my model. The migration runs successfully, but when I connect to the DB using pgAdmin III I see that the uid column is not set as primary key. What am I missing?

Calculate next Primary Key - of specific format

三世轮回 提交于 2019-11-28 10:10:52
问题 I have a table which holds a list of IDs, and various other columns such as IDName. The Primary Key of the table is the ID itself, however it does not auto_increment. So, I want to be able to generate / calculate the next primary key, however there is a twist: The primary key should be in a specific format, i.e. the 8 digit ID is made up of three parts: <the level><a code><a sequence #> , e.g. <2><777><0123> = 27770123 So, when I am creating a new ID for the table, I want the next sequence

Identifying Sybase tables, fields, keys, constraints

我与影子孤独终老i 提交于 2019-11-28 09:24:21
问题 I'm trying to set up a Sybase query that will give me the following output: Table KeyType KeyNumber Column table1 PK 1 table1_id table1 FK 2 table2_id table1 FK 3 table3_id table1 FK 4 table4_id table1 Unique 5 table1_abc table1 Unique 5 table1_def In other words, I need the PK for each table, and every foreign key it has, as well as every unique key (not where a key has more than one element, such as the unique key above, this is identified by having the same KeyNumber). I'm guessing I need

how to set the primary key when writing a pandas dataframe to a sqlite database table using df.to_sql

那年仲夏 提交于 2019-11-28 09:14:27
I have created a sqlite database using pandas df.to_sql however accessing it seems considerably slower than just reading in the 500mb csv file. I need to: set the primary key for each table using the df.to_sql method tell the sqlite database what datatype each of the columns in my 3.dataframe are? - can I pass a list like [integer,integer,text,text] code.... (format code button not working) if ext == ".csv": df = pd.read_csv("/Users/data/" +filename) columns = df.columns columns = [i.replace(' ', '_') for i in columns] df.columns = columns df.to_sql(name,con,flavor='sqlite',schema=None,if

Do I need a primary key for my table, which has a UNIQUE (composite 4-columns), one of which can be NULL?

你离开我真会死。 提交于 2019-11-28 08:38:10
I have the following table (PostgreSQL 8.3) which stores prices of some products. The prices are synchronised with another database, basically most of the fields below (apart from one) are not updated by our client - but instead dropped and refreshed every once-in-a-while to sync with another stock database: CREATE TABLE product_pricebands ( template_sku varchar(20) NOT NULL, colourid integer REFERENCES colour (colourid) ON DELETE CASCADE, currencyid integer NOT NULL REFERENCES currency (currencyid) ON DELETE CASCADE, siteid integer NOT NULL REFERENCES site (siteid) ON DELETE CASCADE, master

Why is the foreign key part of the primary key in an identifying relationship?

老子叫甜甜 提交于 2019-11-28 08:22:45
I'm trying to understand a concept rather than fixing a piece of code that won't work. I'll take a general example of a form (parent table) and a form field (child table). Logically , this would be an identifying relationship, since a form field cannot exist without a form. This would make me think that in order to translate the logical relationship into the technical relationship, a simple NOT NULL for the form_id field in the form_field table would suffice. (See the left part of above screenshot.) However, when I add an identifying relationship using MySQL Workbench, form_id is not only NOT

Creating composite primary key in SQL Server

二次信任 提交于 2019-11-28 08:14:39
How to add composite primary keys in SQL Server 2008? I have a table as follows. testRequest (wardNo nchar(5) , BHTNo nchar(5) , testID nchar(5) , reqDateTime datetime); I need wardNo, BHTNo and testID to be a composite primary key. How can I do this in SQL Server Management Studio? MaDu_LK If you use management studio, simply select the wardNo, BHTNo, testID columns and click on the key mark in the toolbar. Command for this is, ALTER TABLE dbo.testRequest ADD CONSTRAINT PK_TestRequest PRIMARY KEY (wardNo, BHTNo, TestID) How about this: ALTER TABLE dbo.testRequest ADD CONSTRAINT PK_TestRequest

Changing MySQL primary key when foreign key contraints exist

空扰寡人 提交于 2019-11-28 07:27:08
问题 I have two already-existing tables which look (in part) roughly like this: CREATE TABLE parent ( old_pk CHAR(8) NOT NULL PRIMARY KEY ) ENGINE=InnoDB; CREATE TABLE child ( parent_key CHAR(8), FOREIGN KEY (parent_key) REFERENCES parent(old_pk) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB; I want to add a new auto-incrementing integer id column to parent and use it as the primary key instead, while still keeping old_pk as a unique key and allowing other tables like child to reference it