Key (owner_id)=(0) already exists.
Getting the above error relating to the owners table when I try to run both tables, although the first table is created fine on its own. Any guidance would be greatly appreciated.
DROP TABLE pet CASCADE;
DROP TABLE owner CASCADE;
CREATE TABLE pet(
pet_id SERIAL4 primary key,
pet_type varchar(255),
pet_name varchar(255),
pet_age INT2,
pet_weight INT4,
behaviour_score INT4
);
CREATE TABLE owners(
owner_id SERIAL4 primary key,
owner_name varchar(255),
owner_email varchar(255)
);
Related
I am trying to make an init script for MariaDB, using docker-compose.
Only the 3 first tables are created, so player_team_relation and player table are not created.
create table if not exists user
(
user_id varchar(255) primary key,
username varchar(30) not null,
password_hash varchar(255) not null
);
create table if not exists history
(
history_id bigint auto_increment primary key,
user_id varchar(255) not null,
game_date timestamp not null,
constraint user_history_constraint foreign key (user_id) references user (user_id)
);
create table if not exists team
(
team_id bigint auto_increment primary key,
history_id bigint not null,
player_name varchar(30) not null,
score bigint not null,
hasWon bool not null,
constraint history_team_constraint foreign key (history_id) references history (history_id)
);
create table if not exists player_team_relation
(
player_id bigint not null primary key,
team_id bigint not null primary key,
constraint player_ptr_constraint foreign key (player_id) references player (player_id),
constraint team_ptr_constraint foreign key (team_id) references team (team_id)
);
create table if not exists player
(
player_id bigint auto_increment primary key,
user_id varchar(255) not null,
player_name varchar(30) not null,
wins int not null,
losses int not null,
score bigint not null,
constraint user_player_constraint foreign key (user_id) references user (user_id)
);
I have tried creating the database many times by deleting the volume and creating a new container.
Another example why error checking is always recommended:
The statement
create table if not exists player_team_relation
(
player_id bigint not null primary key,
team_id bigint not null primary key,
...
will fail, since it's not possible to define multiple primary keys per table. For more information on primary keys please check the documentation.
About your column definitions: Are you sure you want to use BIGINT (signed integer) instead of BIGINT UNSIGNED for auto_increment? Do you really need BIGINT? The maximum value of INTEGER UNSIGNED is 4294967295 - more than half of the world's population.
I have created two tables in Snowflake.
create or replace TRANSIENT TABLE TESTPARENT (
COL1 NUMBER(38,0) NOT NULL,
COL2 VARCHAR(16777216) NOT NULL,
COL3 VARCHAR(16777216) NOT NULL,
constraint UNIQ_COL3 unique (COL3)
);
create or replace TRANSIENT TABLE TESTCHILD3 (
COL_A NUMBER(38,0) NOT NULL,
COL_B NUMBER(38,0) NOT NULL,
ABCDEF VARCHAR(16777216) NOT NULL,
constraint FKEY_1 foreign key (COL_A, COL_B) references TEST_DB.PUBLIC.TESTPARENT1(COL1,COL2),
constraint FKEY_2 foreign key (ABCDEF) references TEST_DB.PUBLIC.TESTPARENT(COL3)
);
Now I want to execute a query and see the names of columns that are involved in FKEY_2 FOREIGN KEY
in Table TESTCHILD3, but it seems like there are no DB Table/View that keeps this information. I can find out the column names for UNIQUE KEY & PRIMARY KEY but there is nothing for FOREIGN KEYS.
EDIT
I have already tried INFORMATION_SCHEMA.TABLE_CONSTRAINTS, along with INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS and all the other system tables. No luck. Only DESC TABLE is giving me some info related to CONSTRAINTS and COLUMNS but that also has FOREIGN KEY CONSTRAINTS information missing.
SHOW IMPORTED KEYS IN TABLE <fk_table_name>;
Updated answer:
I was checking on something unrelated and noticed a very efficient way to list all primary and foreign keys:
show exported keys in account; -- Foreign keys
show primary keys in account;
When you limit the call to a table, it appears you have to request the foreign keys that point to the parent table:
show exported keys in table "DB_NAME"."SCHEMA_NAME"."PARENT_TABLE";
You can check the documentation for how to limit the show command to a specific database or schema, but this returns rich information in a table very quickly.
maybe you can try to query this view: INFORMATION_SCHEMA.TABLE_CONSTRAINTS
Note: TABLE_CONSTRAINTS only displays objects for which the current role for the session has been granted access privileges.
For more see: https://docs.snowflake.net/manuals/sql-reference/info-schema/table_constraints.html
I'm having a problem with my ActiveRecord exception handling and I suspect some of the indexes haven't been created as I thought they did. How can I view the indexes that have been created for my model?
Thanks so much in advance for your wisdom!
You can just look in your db/schema.rb where all the structure of the database is explicitly listed.
Assuming you are using MySQL, from your command line:
$ rails dbconsole
mysql> show create table users;
users | CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT '',
`email` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_users_on_email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=latin1 |
Any "KEY" lines are your indexes.
I use sqlite(3.7.4) in iphone.
I create a table like:
create table A (UserName varchar (50) primary key, Num integer);
Then I insert the record below twice:
('abc',1);
Normally there should be only one record in DB.
However I found in the DB
(abc,1);
( ,1);
I'm confused that as UserName is primary key and why there are two records!
I don't know what's the problem.
Can anyone help me?
thank you.
Why you want use username as primary key. Primary key should unique identify record. What is why when you inserted twice in second time you have primary key constraint violation
create table A (UserName varchar (50) primary key, Num integer, unique (UserName));
I have a table called messages and here is the table structure, I don’t want id is auto increment field but it should be a primary key for that table.
Here is table structure for messages
CREATE TABLE `messages` (
`id` INT(11) NOT NULL,
`user_id` INT(11) NOT NULL,
`text` VARCHAR(255) NOT NULL,
`source` VARCHAR(100),
`created_at` DATETIME DEFAULT NULL,
`updated_at` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`)
);
while insert the data into table I am using below hash object
msg['id'] = 12345;
msg['user_id'] = 1;
msg['text'] = 'Hello world';
If I save this hash into messages table, id is not inserting
message = Message.new(msg);
message.save!
Rails is building insert sql with out id, so id value is not inserting messages table.
How insert the id value in table, This the insert sql rails build with out using id field
INSERT INTO `users` (`updated_at`, `user_id `, `text`, `created_at`) VALUES('2010-06-18 12:01:05', '1', 'Hello world', '2010-06-18 12:01:05');
Setting ID value is often useful when migrating from legacy data or - as I am doing right now - merging two apps while preserving FK integrity.
I just scratched my head for a while and it seems you have to set the PK value before calling save. After the record is saved, ActiveRecord ignores #id= or update_attribute . So while setting up the record from an attribute hash I use:
article = Article.new(attrs)
article.id = attrs["id"]
article.save!
You're working against the way rails works. ActiveRecord reserves the use of the id column and manages it for you.
Why should id not be an auto-incrementing column if it's the primary key?
Why do you need to control its value?
If you need an id column you can control yourself, add another one. It won't be the primary key, but you can make it a unique index too.