mysql duplicate key error during restoration through mysqldump data - primary-key

I have a mysql database which is having one table for geo location and saves latitude and longitude values of users. The fields are as followed
TABLE USER_LOCATION (
LATITUDE float NOT NULL DEFAULT '0',
LONGITUDE float NOT NULL DEFAULT '0',
STATE varchar(50) NOT NULL,
COUNTRY varchar(255) DEFAULT NULL,
ADDRESS varchar(255) DEFAULT NULL,
CITY varchar(45) DEFAULT NULL,
DISTRICT varchar(45) DEFAULT NULL,
PRIMARY KEY (LATITUDE,LONGITUDE),
KEY latlong (LATITUDE,LONGITUDE),
KEY longlat (LONGITUDE,LATITUDE)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Problem 1:- Some time fields get duplicate value and the insertion happens successfully.
Problem 2:- If I have taken mysqldump of data and try to restore with the same duplicate rows , then I receive duplicate PRIMARY KEY error like this.
ERROR 1062 (23000): Duplicate entry '17.4273-78.3316' for key 'PRIMARY'
Now I am not able to understand that why the first problem happens and why this is detected in problem 2.

Related

PostgreSQL: How to link 2 tables?

I have a table defined like this:
CREATE TABLE public.journeys (
journey_id uuid NOT NULL UNIQUE DEFAULT uuid_generate_v4(),
name text NOT NULL,
user_id uuid NOT NULL,
date_created timestamptz NOT NULL,
date_deleted timestamptz NULL,
route_id uuid NOT NULL,
CONSTRAINT fk_users
FOREIGN KEY(user_id)
REFERENCES users(user_id)
);
What I want to do now is create a second table that will connect to this table above. Here's its definition:
CREATE TABLE public.routes (
route_id uuid NOT NULL UNIQUE DEFAULT uuid_generate_v4(),
idx smallint NOT NULL,
date timestamptz NULL,
latitude real NOT NULL,
longitude real NOT NULL,
CONSTRAINT route_key
PRIMARY KEY (route_id, idx),
CONSTRAINT fk_journeys
FOREIGN KEY(route_id)
REFERENCES journeys(route_id)
);
The notion is that for every Journey there will be a connected Route that simply consists of a series of Latitude, Longitude points. So for a given route_id in journeys there will be N records in routes. Every record in a given route will share the same route_id but each one will have a unique idx (ie. 0, 1, 2, ...).
This is the error I'm getting when I try creating public.routes:
SQL Error [42830]: ERROR: there is no unique constraint matching given keys for referenced table "journeys"
What am I doing wrong and how do I fix this?
Robert
I read several more threads on the subject and then realized that that journeys.route_id was not being declared as UNIQUE.
So within the public.journeys declaration, this fixed the problem:
route_id uuid NOT NULL UNIQUE,

Why does it keep giving me te same errors?

So I wanted to put a sql file in a database (es_extended.sql) and it keeps giving me errors like: duplicate column name name etc. and
SQL error (1050): Table 'items' already exists
I have tried to change the names of columns and all but it keeps giving the same errors.
here is the code:
ALTER TABLE `users`
ADD COLUMN `money` VARCHAR(50) DEFAULT NULL,
ADD COLUMN `name` VARCHAR(50) NULL DEFAULT '' AFTER `money`,
ADD COLUMN `skin` LONGTEXT NULL AFTER `name`,
ADD COLUMN `job` VARCHAR(50) NULL DEFAULT 'unemployed' AFTER `skin`,
ADD COLUMN `job_grade` INT NULL DEFAULT 0 AFTER `job`,
ADD COLUMN `loadout` LONGTEXT NULL AFTER `job_grade`,
ADD COLUMN `position` VARCHAR(36) NULL AFTER `loadout`
;
CREATE TABLE `items` (
`name` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
`limit` int(11) NOT NULL DEFAULT '-1',
`rare` int(11) NOT NULL DEFAULT '0',
`can_remove` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`name`)
);
CREATE TABLE `job_grades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_name` varchar(50) DEFAULT NULL,
`grade` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
`salary` int(11) NOT NULL,
`skin_male` longtext NOT NULL,
`skin_female` longtext NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `job_grades` VALUES (1,'unemployed',0,'unemployed','Unemployed',200,'{}','{}');
CREATE TABLE `jobs` (
`name` varchar(50) NOT NULL,
`label` varchar(50) DEFAULT NULL,
PRIMARY KEY (`name`)
;
INSERT INTO `jobs` VALUES ('unemployed','Unemployed');
CREATE TABLE `user_accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identifier` varchar(22) NOT NULL,
`name` varchar(50) NOT NULL,
`money` double NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
CREATE TABLE `user_inventory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identifier` varchar(22) NOT NULL,
`item` varchar(50) NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
Sql databases are a way to implement the persistence layer: data that should be kept permanently or at least independently from sessions.
The error you're getting: SQL error (1050): Table 'items' already exists means that you've already created a table with the same name you're using to try and create a new table.
You do not need to recreate the table each time you try to add data to it. The table is persistent as long as you do not DROP (delete) it or the database it belongs to.
If you want to recreate the table (either because you need to change its schema or because you want to start over), you can run the command DROP TABLE items which will delete the table and all data in the table.
After dropping, you can run the CREATE TABLE items... command again.
If you don't want to delete the table, you can just run the INSERT INTO items... command to add data to the existing table.
The SQL Error (xxxx): duplicate column name 'name' error means you are trying to add a column that already exists in the table. If the users table already has a column called name, then you cannot run the command ALTER TABLE users ADD COLUMN 'name'... without an error.
If the column doesn't meet your needs anymore, you can use ALTER TABLE users ALTER COLUMN 'name'... to change the schema for the column.

SQL Database Design (Manager and Workers relationship)

CREATE TYPE VALID_ACCOUNT_TYPE AS ENUM ('Admin', 'Manager', 'Worker');
CREATE TYPE VALID_LANGUAGE AS ENUM ('en', 'th');
CREATE TABLE IF NOT EXISTS users (
user_id SMALLSERIAL PRIMARY KEY,
username TEXT NOT NULL UNIQUE CONSTRAINT username_constraint CHECK (username ~ '^[A-Z0-9_-]{1,15}$'),
disabled BOOLEAN NOT NULL DEFAULT FALSE,
password TEXT NOT NULL,
plain_password TEXT NOT NULL,
created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
modified TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
CREATE TABLE IF NOT EXISTS user_details (
user_id SMALLINT PRIMARY KEY REFERENCES users,
account_type VALID_ACCOUNT_TYPE NOT NULL,
language VALID_LANGUAGE NOT NULL DEFAULT 'en',
nickname TEXT NOT NULL CONSTRAINT nickname_constraint CHECK (nickname ~ '^.{1,15}$') DEFAULT 'No Nickname',
phone_number TEXT NOT NULL CONSTRAINT phone_number_constraint CHECK (phone_number ~ '^[0-9]{10}$') DEFAULT 'No Phone Number',
line_username TEXT NOT NULL CONSTRAINT nickname_constraint CHECK (nickname ~ '^.{1,15}$') DEFAULT 'No Line Username',
created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
modified TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
I have following table and after creating these I want to have another table that describes relationship between Manager(One) to Workers(Many). One manager can have many workers. And one worker can only have one manager. How should I approach the table creation?
I currently have
CREATE TABLE IF NOT EXISTS user_relationships (
user_id1 SMALLINT REFERENCES users NOT NULL,
user_id2 SMALLINT REFERENCES users NOT NULL,
created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
modified TIMESTAMP WITHOUT TIME ZONE NOT NULL,
UNIQUE (user_id1, user_id2)
);
but this doest not have any constraint about Manager/Worker at all.

SQL messages that can be read by specified users

Task:
At present, the database knows two types of messages:
Messages that a user posts and that are public for anyone and everyone to read
Messages that a user posts and that are non-public.
These messages can only be read by users that the posting user has marked as friends.
In this step, you should add a third type of message. This third type of message should be readable by specified recipients only.
This means the database needs to provide the following:
A way of distinguishing between the three types of messages. This involves a change to the Message table.
A way of specifying who the recipients of a particular message are. This will probably require an additional table.
Your job is to implement the necessary changes and additional table for this purpose and any keys and foreign key
relationships required.
here are two existing tables witch relate to the task(copies from my db).
User table
CREATE TABLE IF NOT EXISTS `User` (
`user_id` int(10) unsigned NOT NULL auto_increment,
`given_name` varchar(60) default NULL,
`surname` varchar(60) default NULL,
`address` varchar(255) default NULL,
`city_id` int(10) unsigned NOT NULL,
`date_of_birth` datetime default NULL,
`email` varchar(80) default NULL,
PRIMARY KEY (`user_id`),
KEY `ix_user_surname` (`surname`),
KEY `ix_user_given_name` (`given_name`),
KEY `ix_user_name` (`given_name`,`surname`),
KEY `ix_user_date_of_birth` (`date_of_birth`),
KEY `ix_user_email` (`email`),
KEY `ix_user_city_id` (`city_id`)
) ENGINE=InnoDB
Message table
CREATE TABLE IF NOT EXISTS `Message` (
`message_id` int(10) unsigned NOT NULL auto_increment,
`owner_id` int(10) unsigned default NULL,
`subject` varchar(255) default NULL,
`body` text,
`posted` datetime default NULL,
`is_public` tinyint(4) default '0',
PRIMARY KEY (`message_id`),
KEY `ix_message_owner_id` (`owner_id`)
) ENGINE=InnoDB
Ok, so is_public give you the ability to distinguish between two types (e.g. is_public = '0' means private, and is_public = '1' means public). But now you have a new concept of specified receipts, so the yes/no model won't work anymore b/c you have 3 types. Usually in this situation you can switch to a flag or type column.
So maybe make a message_type column that is one of 'PUBLIC', 'PRIVATE', 'SPECIFIED' or something like that.
After that it sounds like you need at least two more tables. Users must be able to specify friends and users must be able to specify users to receive particular messages.

I am not able to create foreign key in mysql Error 150. Please help

i am trying to create a foreign key in my table. But when i executes my query it shows me error 150.
Error Code : 1025
Error on create foreign key of '.\vts\#sql-6ec_1' to '.\vts\tblguardian' (errno: 150)
(0 ms taken)
My Queries are
Query to create a foreign Key
alter table `vts`.`tblguardian` add constraint `FK_tblguardian` FOREIGN KEY (`GuardianPickPointId`) REFERENCES `tblpickpoint` (`PickPointId`)
Primary Key table
CREATE TABLE `tblpickpoint` (
`PickPointId` int(4) NOT NULL auto_increment,
`PickPointName` varchar(500) default NULL,
`PickPointLabel` varchar(500) default NULL,
`PickPointLatLong` varchar(100) NOT NULL,
PRIMARY KEY (`PickPointId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC
Foreign Key Table
CREATE TABLE `tblguardian` (
`GuardianId` int(4) NOT NULL auto_increment,
`GuardianName` varchar(500) default NULL,
`GuardianAddress` varchar(500) default NULL,
`GuardianMobilePrimary` varchar(15) NOT NULL,
`GuardianMobileSecondary` varchar(15) default NULL,
`GuardianPickPointId` int(4) default NULL,
PRIMARY KEY (`GuardianId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Your problem is the type of the columns in your constraint are different. They must be the same.
`PickPointId` int(4) NOT NULL auto_increment,
`GuardianPickPointId` varchar(100) default NULL,
For more information see the documentation:
Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.
Have a look at this step by step issue list
MySQL Error Number 1005 Can’t create table ‘.\mydb#sql-328_45.frm’ (errno: 150)