I have an old SQL4 database and I'm trying to re-upload it to our newly created database on Phpmyadmin. The characters in the tables are latin and japanese. I tried to change those specific columns but the result is still broken characters for the columns I need to display in Japanese.
Here is a screenshot of my problem : https://imgur.com/a/P6GWrnF
As an example, the SQL code looks like this :
CREATE TABLE `bdd` (
`id` int(11) NOT NULL,
`ville` varchar(50) NOT NULL DEFAULT '',
`nom_fr` varchar(80) NOT NULL DEFAULT '',
`nom_jp` varchar(250) CHARACTER SET sjis NOT NULL DEFAULT '',
`adr_fr` text NOT NULL,
`adr_jp` varchar(3000) CHARACTER SET sjis NOT NULL,
`tel` varchar(20) NOT NULL DEFAULT '0',
`plan` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `bdd` (`id`, `ville`, `nom_fr`, `nom_jp`, `adr_fr`, `adr_jp`, `tel`, `plan`) VALUES
(47, 'Tokyo', 'THE KNOT TOKYO Shinjuku', '?U ?m?b?g ?????V?h', '4-31-1 Nishi Shinjuku, Shinjuku Ku, Tokyo', '?????s?V?h???V?h4-31-1', '03-3375-6511', 'the knot.JPG'),
(3546, 'Tokyo', 'HOSHINOYA Tokyo', '???????', '1-9-1 Otemachi, Chiyoda-ku, Tokyo 100-0004', '??100-0004 ?????s?????c??????????9??1', '0570-073-066', 'HOSHINOYA TOKYO.JPG'),
SET NAMES "utf8";
CREATE TABLE `bdd` (
`id` int(11) NOT NULL,
`ville` varchar(50) NOT NULL DEFAULT '',
`nom_fr` varchar(80) NOT NULL DEFAULT '',
`nom_jp` varchar(250) CHARACTER SET sjis NOT NULL DEFAULT '',
`adr_fr` text NOT NULL,
`adr_jp` varchar(3000) CHARACTER SET sjis NOT NULL,
`tel` varchar(20) NOT NULL DEFAULT '0',
`plan` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT whatever you want
It would be better if you stored everything in utf8 as you have multiple langueages. And before insertion you neeed to set your connection parameters accordingly so server can understand what you're sending. Btw maybe you'll have to
SET NAMES 'sjis';
Problem solved.
I didn't try to change encoding any longer but created a new table with all rows in utf8mb4_unicode_ci and imported the data within this new table.
Related
I get "A comma or a closing bracket was expected" error when trying to execute this SQL code:
CREATE TABLE `player_vehicles` (
`#` int(11) NOT NULL,
`steam` varchar(50) DEFAULT NULL,
`citizenid` varchar(50) DEFAULT NULL,
`vehicle` varchar(50) DEFAULT NULL,
`hash` varchar(50) DEFAULT NULL,
`mods` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
`plate` varchar(50) NOT NULL,
`fakeplate` varchar(50) DEFAULT NULL,
`garage` varchar(50) DEFAULT NULL,
`fuel` int(11) DEFAULT 100,
`engine` float DEFAULT 1000,
`body` float DEFAULT 1000,
`state` int(11) DEFAULT 1,
`depotprice` int(11) NOT NULL DEFAULT 0,
`drivingdistance` int(50) DEFAULT NULL,
`status` text DEFAULT NULL,
`health` longtext NOT NULL '[{"value":100,"part":"electronics"},{"value":100,"part":"fuelinjector"},{"value":100,"part":"brakes"},
{"value":100,"part":"radiator"},{"value":100,"part":"driveshaft"},{"value":100,"part":"transmission"},{"value":100,"part":"clutch"}]'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Error message:
A comma or a closing bracket was expected. (near "'[{"value":100,"part":"electronics"},{"value":100,"part":"fuelinjector"},{"value":100,"part":"brakes"},{"value":100,"part":"radiator"},{"value":100,"part":"driveshaft"},{"value":100,"part":"transmission"},{"value":100,"part":"clutch"}]'" at position 816)
Can someone please help, I can't figure this out?
It seems like you want to use a string as a default for your longtext column, but you are missing the DEFAULT keyword.
But it won't work anyway. https://dev.mysql.com/doc/refman/8.0/en/blob.html says:
BLOB and TEXT columns cannot have DEFAULT values.
If I add the DEFAULT keyword to your example, and try it in the MySQL client, I get this error:
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'health' can't have a default value
MariaDB, a fork of MySQL, added the capability to add a DEFAULT value to a BLOB or TEXT column in MariaDB 10.2.1, but this is not supported in MySQL.
If you need to assign some default value to LONGTEXT column then use BEFORE INSERT trigger.
CREATE TABLE test (id INT, txt LONGTEXT NOT NULL);
CREATE TRIGGER tr_bi_setdef
BEFORE INSERT ON test
FOR EACH ROW
SET NEW.txt = COALESCE(NEW.txt, '["default value"]');
INSERT INTO test VALUES (1, '["specified value"]'), (2, NULL);
INSERT INTO test (id) VALUES (3);
SELECT * FROM test;
id | txt
-: | :------------------
1 | ["specified value"]
2 | ["default value"]
3 | ["default value"]
db<>fiddle here
So, I have two tables filled with for the most part very similar data, for example, a row in each table may have the same first name, last name, and address, but have a different phone number or email address based on the most recently available data which was updated in a separate excel worksheet (out of my hands, my job is just to merge this data into our latest database which they plan to use from here on out, not the excel sheet). I just need a good way to merge these tables with the same column names without doing it manually (about 24,000+) records.
Here is the Create Table Syntax for both tables:
CREATE TABLE `UsersUpdated` (
`FULLNME` longtext,
`LSTNME` varchar(23) CHARACTER SET utf8 DEFAULT NULL,
`FSTNME` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`MID` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`SUFF` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`STAT` varchar(2) CHARACTER SET utf8 DEFAULT NULL,
`PTY` varchar(3) CHARACTER SET utf8 DEFAULT NULL,
`PH` bigint(20) DEFAULT NULL,
`ALTPH` bigint(20) DEFAULT NULL,
`DOB` datetime DEFAULT NULL,
`REGDTE` datetime DEFAULT NULL,
`ADDR` text,
`ST` int(11) DEFAULT NULL,
`STNME` varchar(19) CHARACTER SET utf8 DEFAULT NULL,
`APT` varchar(7) CHARACTER SET utf8 DEFAULT NULL,
`TWN` varchar(6) CHARACTER SET utf8 DEFAULT NULL,
`ZIP` int(11) DEFAULT NULL,
`W` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
`G17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`SIGN` tinyint(1) NOT NULL DEFAULT '0',
`SUPP` tinyint(1) NOT NULL DEFAULT '0',
`NOTES` longtext,
`LTR` tinyint(1) DEFAULT NULL,
`REGISTERED` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `Users` (
`FULLNME` longtext,
`LSTNME` varchar(23) CHARACTER SET utf8 DEFAULT NULL,
`FSTNME` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`MID` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`SUFF` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`STAT` varchar(2) CHARACTER SET utf8 DEFAULT NULL,
`PTY` varchar(3) CHARACTER SET utf8 DEFAULT NULL,
`PH` bigint(20) DEFAULT NULL,
`ALTPH` bigint(20) DEFAULT NULL,
`DOB` datetime DEFAULT NULL,
`REGDTE` datetime DEFAULT NULL,
`ADDR` text,
`ST` int(11) DEFAULT NULL,
`STNME` varchar(19) CHARACTER SET utf8 DEFAULT NULL,
`APT` varchar(7) CHARACTER SET utf8 DEFAULT NULL,
`TWN` varchar(6) CHARACTER SET utf8 DEFAULT NULL,
`ZIP` int(11) DEFAULT NULL,
`W` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
`G17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`SIGN` tinyint(1) NOT NULL DEFAULT '0',
`SUPP` tinyint(1) NOT NULL DEFAULT '0',
`NOTES` longtext,
`LTR` tinyint(1) DEFAULT NULL,
`REGISTERED` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
As you can see, they are basically the same exact tables, I just need to merge them correctly.
Perhaps this is helpful.
update Users
set ADDR = (
select ADDR from UsersUpdated uu
where uu.FULLNME = Users.FULLNME and uu.DOB = Users.DOB
), STNME = (
select STNME from UsersUpdated uu
where uu.FULLNME = Users.FULLNME and uu.DOB = Users.DOB
) ...
;
You can add all the columns to a single update. Depending on the size of the database it might just be as easy to do them individually.
Many platforms allow for a from clause with update that permits a join and a shorter query but it can be problematic. This way you will get errors if any of the subqueries don't return just a single value.
I would modify the Users table to add a UNIQUE key on the fields that should be the same (presumably FSTNME, LSTNME and ADDR from your description but perhaps you might use some other columns e.g. DOB as suggested by #shawnt00), then INSERT the data from UsersUpdated into Users using an ON DUPLICATE KEY UPDATE clause to copy updated data into Users where the user already exists in that table. This query will also work when there are users in UsersUpdated who are not already in Users. So,
ALTER TABLE Users ADD UNIQUE KEY NameAddr (FSTNME, LSTNME, ADDR);
INSERT INTO Users
SELECT * FROM UsersUpdated
ON DUPLICATE KEY UPDATE
FULLNME=VALUES(FULLNME),
LSTNME=VALUES(LSTNME),
FSTNME=VALUKES(FSTNME),
...
LTR=VALUES(LTR),
REGISTERED=VALUES(REGISTERED);
I have read through many entries that people have claimed to have this problem and they've solved their issue but none of their answers solve MY issue. I am using phpMyAdmin to update the email address of a user. The "user_email" field is marked as UNIQUE. Whenever I update the email address to his actual email, I get:
Duplicate entry 'user#example.com' for key 'user_email'
I have Analyzed, Optimized, and Repaired the table and no errors appear -- everything comes up as OK.
I have run several SQL statements to find any duplication only to find out that none exists.
I even exported the table and imported the records again. I add the indexes and try and update... duplicate entry message. Here's the table structure:
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) NOT NULL,
`md5_id` varchar(200) NOT NULL DEFAULT '',
`full_name` tinytext,
`user_name` varchar(200) DEFAULT NULL,
`user_email` varchar(220) DEFAULT NULL,
`user_level` tinyint(4) NOT NULL DEFAULT '1',
`pwd` varchar(220) NOT NULL DEFAULT '',
`address` text COLLATE latin1_general_ci,
`country` varchar(200) DEFAULT NULL,
`tel` varchar(200) NOT NULL DEFAULT '',
`fax` varchar(200) DEFAULT NULL,
`website` text,
`date` date NOT NULL DEFAULT '0000-00-00',
`users_ip` varchar(200) NOT NULL DEFAULT '',
`approved` int(1) NOT NULL DEFAULT '0',
`activation_code` int(10) NOT NULL DEFAULT '0',
`banned` int(1) NOT NULL DEFAULT '0',
`ckey` varchar(220) NOT NULL DEFAULT '',
`ctime` varchar(220) NOT NULL DEFAULT '',
`location` tinyint(4) NOT NULL DEFAULT '9'
) ENGINE=MyISAM AUTO_INCREMENT=210 DEFAULT CHARSET=latin1;
ALTER TABLE `users` ADD PRIMARY KEY (`id`);
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=210;
Even now that I have REMOVED the UNIQUE index from the 'user_email' field, the error is STILL coming up. I REALLY don't understand that (Maybe something residual...? I'm just guessing).
Picture me with wads of hair in my hands. Can anyone please help?
UPDATE:
Here's the output from SHOW CREATE TABLE users
Here's the output from SHOW INDEX FROM users
Error message while editing:
Error message without using database name:
Output of: SHOW CREATE TABLE proctor.users
Does anyone know where there might be a copy of the MySQL "World" example database online somewhere that is Microsoft SQL compatible? I don't have a running MySQL server on hand, just the SQL text file that SQL Server 2008 rejects.
It is now :-) http://pastebin.com/6ATaLuNs
I have been able to load the database into my own SQL Server installation by converting the data types from MySql to SQL Server version, removing MySql-specific code (such as engine specification), and using CHECK constraints in place of the enum's. Here's my rough attempt at converting the table structure script:
BEGIN TRANSACTION
CREATE TABLE city (
ID int NOT NULL,
Name char(35) NOT NULL DEFAULT '',
CountryCode char(3) NOT NULL DEFAULT '',
District char(20) NOT NULL DEFAULT '',
Population int NOT NULL DEFAULT '0',
PRIMARY KEY (ID)
)
CREATE TABLE country (
Code char(3) NOT NULL DEFAULT '',
Name char(52) NOT NULL DEFAULT '',
Continent varchar(20) CHECK(Continent in ('Asia','Europe','North America','Africa','Oceania','Antarctica','South America')) NOT NULL DEFAULT 'Asia',
Region char(26) NOT NULL DEFAULT '',
SurfaceArea decimal(10,2) NOT NULL DEFAULT '0.00',
IndepYear smallint DEFAULT NULL,
Population int NOT NULL DEFAULT '0',
LifeExpectancy decimal(3,1) DEFAULT NULL,
GNP decimal(10,2) DEFAULT NULL,
GNPOld decimal(10,2) DEFAULT NULL,
LocalName char(45) NOT NULL DEFAULT '',
GovernmentForm char(45) NOT NULL DEFAULT '',
HeadOfState char(60) DEFAULT NULL,
Capital int DEFAULT NULL,
Code2 char(2) NOT NULL DEFAULT '',
PRIMARY KEY (Code)
)
CREATE TABLE countrylanguage (
CountryCode char(3) NOT NULL DEFAULT '',
Language char(30) NOT NULL DEFAULT '',
IsOfficial char(1) CHECK(IsOfficial IN ('T','F')) NOT NULL DEFAULT 'F',
Percentage decimal(4,1) NOT NULL DEFAULT '0.0',
PRIMARY KEY (CountryCode,Language)
)
COMMIT TRANSACTION
You can run the INSERT statements as-is if you make the following changes (you will have to manually pull out the INSERT statements by removing the CREATE TABLE code in the MySql script):
Remove all instances of back-tick (`) (Find-Replace ` with nothing in an editor)
Replace all instances of \' with '' for SQL Server's quote escaping
I have this table:
CREATE TABLE IF NOT EXISTS `events` (
`evt_id` int(11) NOT NULL AUTO_INCREMENT,
`evt_name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT 'ucfirst',
`evt_description` varchar(100) DEFAULT NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date DEFAULT NULL,
`evt_starttime` time DEFAULT NULL,
`evt_endtime` time DEFAULT NULL,
`evt_amtpersons` int(11) DEFAULT NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) DEFAULT NULL,
`evt_amtPersonsSubs` tinyint(4) NOT NULL DEFAULT '0',
`evt_photo` varchar(50) DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
But when I add in data into evt_name my first character is not capitalized. Any ideas?
Further information:
MySQL client version: 5.1.37
I want to do this in the database so that I don't have to do ucfirst with php always.
...what?
If I'm reading this right, it will just set the default value of evt_name to the string ucfirst. Try inserting a blank row and see if I'm reading that right.
If you're really against using ucfirst in PHP, you'll probably have to still call ucfirst every time in the query. Or you could only use ucfirst on display, and not in the database.
ucfirst is a function...you defined it as Default-Value for the cell. Use it like this:
INSERT INTO events SET evt_name = UCFIRST($value)