#1062 - Duplicate entry '1' for key 'PRIMARY' - wordpress

I am importing database and i got an error
1062 - Duplicate entry '1' for key PRIMARY.
i tried to upload table individually But it gives me error for those table which have at least one row.
i don't want to delete any record of any table.
here is one table which is giving error.
CREATE TABLE IF NOT EXISTS `wpbeta_aff_affiliates` (
`affiliate_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(512) DEFAULT NULL,
`from_date` date NOT NULL,
`thru_date` date DEFAULT NULL,
`status` varchar(10) NOT NULL DEFAULT 'active',
`type` varchar(10) DEFAULT NULL,
PRIMARY KEY (`affiliate_id`),
KEY `affiliates_afts` (`affiliate_id`,`from_date`,`thru_date`,`status`),
KEY `affiliates_sft` (`status`,`from_date`,`thru_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `wpbeta_aff_affiliates`
--
INSERT INTO `wpbeta_aff_affiliates` (`affiliate_id`, `name`, `email`, `from_date`, `thru_date`, `status`, `type`) VALUES
(1, 'Direct', NULL, '2013-07-03', NULL, 'active', 'direct');

You do not need to specify the record id (with the exception of some circumstances when you need to keep the connection between records in different tables). Try using this query:
INSERT INTO wpbeta_aff_affiliates (name, email, from_date, thru_date, status, type) VALUES ('Direct', NULL, '2013-07-03', NULL, 'active', 'direct');

Related

Cannot restart Strapi after making changes to Content Types (using SQLite)

I have followed the quickstart setup for Strapi, which as I understand sets up an SQLite Database. I created one collection type (painting) which worked without problems, but whenever I try to add a new one or make changes to the existing one the server does not restart and I receive the following error output in my console:
[2022-05-23 16:53:16.323] error: CREATE TABLE _knex_temp_alter889 (id integer not null primary key autoincrement PRIMARY KEY AUTOINCREMENT NOT NULL, title varchar(255) NULL, artist varchar(255) NULL, created_at datetime NULL, updated_at datetime NULL, published_at datetime NULL, created_by_id integer NULL, updated_by_id integer NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL) - table "_knex_temp_alter889" has more than one primary key
SqliteError: CREATE TABLE _knex_temp_alter889 (id integer not null primary key autoincrement PRIMARY KEY AUTOINCREMENT NOT NULL, title varchar(255) NULL, artist varchar(255) NULL, created_at datetime NULL, updated_at datetime NULL, published_at datetime NULL, created_by_id integer NULL, updated_by_id integer NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL) - table "_knex_temp_alter889" has more than one primary key
Deleting the changes I made in my content-types files is currently the only way I have of getting the server to run again. How do I fix this?
So, I ended up creating a new project. Problem seems to have been that I added a field named 'ID', which I assume messed everything up because it conflicted with the automatically assigned ID-System.

Create MySQL schema

I have a problem.
I created a MySQL schema with the MySQL Workbench.
But there is a error message.
A comma or closing bracket was expected. (near "VISIBLE" at position 288)
Unexpected start of statement. (near "`ShopID`" at position 330)
Unrecognised statement type. (near "ASC" at position 339)
As I said I created the scheme via the MySQL workbench. But what is wrong ?
Note: PK = Primary Key; FK = Foreign Key.
Fact table:
Contaminants (TimeID (PK/FK), ShopID (PK/FK), FoodID (PK/FK), substance, quantityPerOunce)
Dimension tables:
Time (TimeID (PK), dayNr, dayName, weekNr, monthNr, year)
Food (FoodID (PK), foodName, brand, foodType)
Place (ShopID (PK), name, street, region, country)
CREATE TABLE IF NOT EXISTS `mydb`.`Contaminants` (
`TimeID` INT NOT NULL,
`ShopID` INT NOT NULL,
`FoodID` INT NOT NULL,
`substance` VARCHAR(45) NULL,
`quantityPerOunce` INT NULL,
PRIMARY KEY (`TimeID`, `ShopID`, `FoodID`),
UNIQUE INDEX `TimeID_UNIQUE` (`TimeID` ASC) VISIBLE,
UNIQUE INDEX `ShopID_UNIQUE` (`ShopID` ASC) VISIBLE,
UNIQUE INDEX `FoodID_UNIQUE` (`FoodID` ASC) VISIBLE,
CONSTRAINT `TimeID`
FOREIGN KEY (`TimeID`)
REFERENCES `mydb`.`Time` (`TimeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ShopID`
FOREIGN KEY (`ShopID`)
REFERENCES `mydb`.`Shop` (`ShopID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FoodID`
FOREIGN KEY (`FoodID`)
REFERENCES `mydb`.`Food` (`FoodID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Food` (
`FoodID` INT NOT NULL,
`foodName` VARCHAR(45) NULL,
`brand` VARCHAR(45) NULL,
`foodType` VARCHAR(45) NULL,
PRIMARY KEY (`FoodID`))
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Shop` (
`ShopID` INT NOT NULL,
`INT` VARCHAR(45) NULL,
`street` VARCHAR(45) NULL,
`region` VARCHAR(45) NULL,
`country` VARCHAR(45) NULL,
PRIMARY KEY (`ShopID`))
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Time` (
`TimeID` INT NOT NULL,
`dayNr` INT NULL,
`dayName` VARCHAR(45) NULL,
`weekNr` INT NULL,
`monthNr` VARCHAR(45) NULL,
`year` INT NULL,
PRIMARY KEY (`TimeID`))
ENGINE = InnoDB

Syntax Error when I try to create trigger in mysql

I am trying to create an trigger for an audit log, where after an insert into manageMemberLog and the action column is equal to CREATE, then insert the associate columns of users table and then grab the select the newly created row and grab the userID and then insert the rest of the data into profile, but I get this error msg when I tried to submit the query.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 9
Line 9 = END IF;
I tried carefully re-typing what I wrote but same error msg.
UPDATE: used the formatting and it says WHERE userID = #newUserID is the error, which i still don't understand as I grab the data from select and set the value into #newUserID.
DELIMITER $$
CREATE TRIGGER usersDB_ai AFTER INSERT
ON manageMemberLog
FOR EACH ROW
IF NEW.action = 'CREATE' THEN
INSERT INTO users (firstName, lastName, userName, email, pwd) VALUES (NEW.firstName, NEW.lastName, NEW.userName, NEW.email, NEW.pwd);
SET #newUserID := (SELECT userID FROM users WHERE userName = NEW.userName AND email = NEW.email);
UPDATE profiles SET rankID = NEW.rankID WHERE userID = #newUserID;
END IF;
END$$
DELIMITER ;
The related tables:
CREATE TABLE `users` (
`userID` int(11) NOT NULL,
`firstName` varchar(128) NOT NULL,
`lastName` varchar(128) NOT NULL,
`userName` varchar(128) NOT NULL,
`email` varchar(128) NOT NULL,
`pwd` varchar(128) NOT NULL
)
DELIMITER $$
CREATE TRIGGER `userCreated` AFTER INSERT ON `users` FOR EACH ROW BEGIN
INSERT INTO profiles (userID) VALUES(NEW.userID);
END
$$
DELIMITER ;
CREATE TABLE `profiles` (
`profileID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`rankID` int(11) NOT NULL,
...
)
CREATE TABLE `manageMemberLog` (
`manageMemberLogID` int(11) NOT NULL,
`manageDate` datetime NOT NULL,
`managerID` int(11) DEFAULT NULL,
`action` varchar(6) CHARACTER SET utf8mb4 NOT NULL,
`userID` int(11) DEFAULT NULL,
`firstName` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`lastName` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`userName` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`email` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`pwd` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`rankID` int(11) NOT NULL
)
Can anyone help thanks in advance!
Silly Me, so what went wrong is I forgot to write "BEGIN" after FOR EACH ROW and does not involve any of the error that it says. Error messages are sometimes not reliable but I guess it did help by say END. A better error message is you can't end when it never began.
so correct query is:
DELIMITER $$
CREATE TRIGGER usersDB_ai AFTER INSERT
ON manageMemberLog
FOR EACH ROW BEGIN
IF NEW.action = 'CREATE' THEN
INSERT INTO users (firstName, lastName, userName, email, pwd) VALUES (NEW.firstName, NEW.lastName, NEW.userName, NEW.email, NEW.pwd);
SET #newUserID := (SELECT userID FROM users WHERE userName = NEW.userName AND email = NEW.email);
UPDATE profiles SET rankID = NEW.rankID WHERE userID = #newUserID;
END IF;
END$$
DELIMITER ;

SQL - how do I insert into these tables?

create table Employees
(
EmployeeID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
EmployeeName VARCHAR(255) NOT NULL,
EmployeeUsername VARCHAR(255) NOT NULL,
EmployeeEmail VARCHAR(255) NOT NULL,
GroupID INT FOREIGN KEY REFERENCES TypeOfGroup(GroupID),
Password VARCHAR(255) NOT NULL
);
create table TypeOfGroup
(
GroupID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
TypeGroup VARCHAR(255) NOT NULL,
Permission CHAR(1) NOT NULL
);
Question: I have a foreign key and wanted to make a connection with another table as I do this to insert into.
My foreign key is 'GROUPID'.
Try this...
insert into TypeOfGroup( TypeGroup, Permission) values ('hr', 1)
"this 1 is used for bit which is a data type in sql server 1 for true and 0 for false"
insert into Employees (EmployeeName, EmployeeUsername, EmployeeEmail,
GroupID, Password) values ('bruno', 'bruno', 'bruno#gmail.com', 1, 'urPassword')
and 1 in the 2nd query is primary of typeofgroup table.

Recursive foreign key 'on delete cascade'

CREATE TABLE IF NOT EXISTS type (
tid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (tid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_type_0 ON type (tid,uuid,name);
CREATE TABLE IF NOT EXISTS object (
oid VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
tid INTEGER NOT NULL,
CONSTRAINT FK_tid FOREIGN KEY (tid) REFERENCES type(tid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid)
);
CREATE INDEX IDX_object_0 ON object (oid,timestamp,tid);
CREATE TABLE IF NOT EXISTS object_user_owner (
uid INTEGER NOT NULL,
oid VARCHAR NOT NULL,
CONSTRAINT FK_uid FOREIGN KEY (uid) REFERENCES user(uid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_oid FOREIGN KEY (oid) REFERENCES object(oid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid,uid)
);
CREATE INDEX IDX_object_user_owner_0 ON object_user_owner (uid,oid);
CREATE TABLE IF NOT EXISTS user (
uid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
password VARCHAR NOT NULL,
salt VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
lastaccess VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (uid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_user_0 ON user (uid,uuid,name);
The above three sqlite3 tables contain foreign keys. The problem is the deletion of a key in the upper table type. When I try do delete I get 'FOREIGN KEY constraint failed' Error. Deleting a from the lowest table object_user_owner before deleting a type works. I think sqlite does not check any recursive cascade constraints. Does anyone have experienced this too or is anything wrong with my design?

Resources