primary-key

sqlalchemy foreign keys works only with primary key?

穿精又带淫゛_ 提交于 2019-12-11 14:47:33
问题 Tables and foreign keys are creating ok, but working not ok: SQLite version 3.7.9 2011-11-01 00:52:41 sqlite> pragma foreign_keys=on; sqlite> sqlite> CREATE TABLE t1(id int primary key, uuid varchar(36)); sqlite> CREATE TABLE t2(id int primary key, t1_uuid varchar(36), FOREIGN KEY (t1_uuid) REFERENCES t1(uuid)); sqlite> sqlite> INSERT INTO t1(uuid) values ("uuid-1"); sqlite> INSERT INTO t2(t1_uuid) values ("uuid-1"); Error: foreign key mismatch But if i make t1(uuid) primary key, all works as

Oracle: Make a composite Key containing three Foregin keys

余生长醉 提交于 2019-12-11 13:54:44
问题 This is my code: create table orderline ( Order_No number(4) constraint orderno_fk references order_detail(Order_No), Product_Code varchar2(6) constraint productcode2_fk references product(Product_Code), Product_Size char(1) constraint productsize_fk references product_stock(Product_Size), Product_Quantity number(4) not null constraint orderline_comp primary key (Order_No,Product_Code, Product_Size) ); I get the error (with the star underneath the left parenthesis before 'Order'): ERROR at

A query with primary key field used twice

非 Y 不嫁゛ 提交于 2019-12-11 12:23:34
问题 In my database I have two tables: relationship table: organization_id_first, organization_id_second, relationship_type organization table: primary key = org_id ; org_id, org_name, ... How would I be able to join the organization table so that I could get the org_name for both organizations that have an entry in the relationship table? I don't think I can join on the same primary key. Would I have to do a subquery of some sort? Thanks! 回答1: This is how i would do it in T-SQL ... just join it

Slow update (primary key)

馋奶兔 提交于 2019-12-11 11:55:14
问题 update auditdata set TATCallType='12',TATCallUnit='1' from auditdata auditdata inner join Auditdata_sms_12 a_sns on auditdata.ID = a_sns.id when I above query it takes more than 10 minutes to execute. what wrong in this Auditdata.ID is primary key.. if i run Update command is that also update the indexes??? is this reason of update getting is slow 回答1: Looking at your comment, main table contains less rows than temp table. Try using EXISTS clause (or in some sense, reduce the comparison to

Creating custom primary keys in Rails application

跟風遠走 提交于 2019-12-11 11:47:14
问题 In my Rails application which works with Oracle database (version 11g) I need primary keys to be String s, not Integer s. For example, in my Product model I need primary keys like "p001" , "p002" etc. 回答1: class AddProductWithDifferentPrimaryKey < ActiveRecord:Migration def change create_table :table, id: false do |t| t.string :id, null: false # other columns t.timestamps end execute "ALTER TABLE table ADD PRIMARY KEY (id);" end end Don't forget to also add this line to your table model so

MySQL table PRIMARY KEY question?

主宰稳场 提交于 2019-12-11 11:08:29
问题 I' was wondering should my primary key look like this PRIMARY KEY (id, category_id, posts_id) or look like this PRIMARY KEY (id) ? Here is my MySQL table. CREATE TABLE posts_categories ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, category_id INT UNSIGNED NOT NULL, posts_id INT UNSIGNED NOT NULL, date_created DATETIME NOT NULL, PRIMARY KEY (id, category_id, posts_id), UNIQUE KEY (category_id, posts_id) ); 回答1: The multiple column key is called a Composite Key or a Compound Key As stated they are

Adding a multi-column primary key to a table with 40 million records

谁都会走 提交于 2019-12-11 10:39:23
问题 I'm working on maintaining a database which stores data transfer information between different networks. Essentially, each data transfer is logged and at the end of each month I run a perl script that loads the log files into a table in the database. I did not design the perl script or the database schema. It was done before I started working on this project. I used this link to retrieve the primary keys of the table (usage_detail is the name of the table) and it gave me nothing. Since, there

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

Database PK-FK design for future-effective-date entries?

ぐ巨炮叔叔 提交于 2019-12-11 08:02:18
问题 Ultimately I'm going to convert this into a Hibernate/JPA design. But I wanted to start out from purely a database perspective. We have various tables containing data that is future-effective-dated. Take an employee table with the following pseudo-definition: employee id INT AUTO_INCREMENT ... data fields ... effectiveFrom DATE effectiveTo DATE employee_reviews id INT AUTO_INCREMENT employee_id INT FK employee.id Very simplistic. But let's say Employee A has id = 1, effectiveFrom = 1/1/2011,

postgresql: error duplicate key value violates unique constraint

时光怂恿深爱的人放手 提交于 2019-12-11 07:42:56
问题 This question have been asked by several people but my problem seems to be different. Actually I have to merge same structured tables from different databases in postgresql into a new DB. What I am doing is that I connect to remote db using dblink, reads that table in that db and insert it into the table in the current DB like below INSERT INTO t_types_of_dementia SELECT * FROM dblink('host=localhost port=5432 dbname=snap-cadence password=xxxxxx', 'SELECT dementia_type, snapid FROM t_types_of