mysql importing error on mysql host - mysql

Error
SQL query:
CREATE TABLE `usersinputs` (
`ID` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`message` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
MySQL said: Documentation
#1050 - Table 'usersinputs' already exists

Your error says it all. There is already a table named as userinputs in your database. You need to create a table with a different name or if there is any datatype change or any other alteration to the table then you need to ALTER the table.

Instead of creating a table which already exists try one with another name.
You can however alter or check if the table exists:
CREATE TABLE IF NOT EXISTS `usersinputs` (
`ID` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`message` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The error is due to the table usersinputs is already exists in your database.
If you really want to CREATE a new table with the different columns and data types you can DROP the existing table and CREATE the new table by DROP TABLE IF EXISTS `usersinputs`;
So your code will be:
DROP TABLE IF EXISTS `usersinputs`;
CREATE TABLE `usersinputs` (
`ID` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`message` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Or simply want to change any column name or data type use ALTER TABLE

Related

Is it possible to make a batch insert/update if the uniqueness of the record is a bundle of two fields?

I have the following table structure (example)
CREATE TABLE `Test` (
`id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`position_id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`price` decimal(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `Test` ADD PRIMARY KEY (`id`);
ALTER TABLE `Test` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
This table contains data that is constantly in need of updating. There is also new data that needs to be entered. Since there is a lot of data, it will take quite a long time to check each record to make it insert or update.
After studying the question, I realized that I need to use batch insert/update with:
INSERT on DUPLICATE KEY UPDATE
But the documentation says that the fields must have a unique index. But I don't have any unique fields, I can't use the ID field. The uniqueness of the record can only be in a combination of two fields order_id and position_id.
Is it possible to make a batch insert/update if the uniqueness of the record is a bundle of two fields?
You need a composite primary-key. You also don't need your AUTO_INCREMENT id column, so you can drop it.
Like so:
CREATE TABLE `Test` (
`order_id` int NOT NULL,
`position_id` int NOT NULL,
`name` varchar(255) NOT NULL COLLATE utf8mb4_unicode_ci,
`price` decimal(10,2) NOT NULL,
CONSTRAINT PK_Test PRIMARY KEY ( `order_id`, `position_id` )
) ENGINE=InnoDB
Then you can use INSERT ON DUPLICATE KEY UPDATE.

MYSQL error #1072 - Key column 'id ' doesn't exist in table in XAMPP, phpmyadmin

I am trying to create a new table using the mysql GUI in phpmyadmin. This is the error I am getting #1072 - Key column 'id ' doesn't exist in table
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id `)
) ENGINE = InnoDB
Please correct the extra space in when you're defining PRIMARY KEY(id )
This should be your query :-
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB
simply run the query without "`"
CREATE TABLE loginsys.groups(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
permission TEXT NOT NULL,
PRIMARY KEY(id))

Got the ERROR 1046* CREATE TABLE IF NOT EXISTS `activity` (

Table structure for table activity
CREATE TABLE IF NOT EXISTS `activity` (
`fnum` int(10) unsigned NOT NULL auto_increment,
`fid` int(25) default NULL,
`fdate` datetime default NULL,
`ftask` varchar(50) default NULL,
`username` varchar(255) NOT NULL default '',
PRIMARY KEY (`fnum`),
KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1204 ;
MySQL said:
1046 - No database selected
As the error says "1046 - No database selected". You should start your script with selecting a database. E.g.:
use my_database;

`INSERT INTO` query is not working

Please have a look at the following code
INSERT INTO `test2.key_word` SELECT * from `test.key_word`
I am using PHPMyAdmin. This query simply gives the error No Database Selected when run inside the server 127.0.0.1 area. If I run this inside query inside the SQL Query in test2.key_word, then it says Table 'test2.test2.key_word' doesn't exist.
Below is how the test.key_word table is created
CREATE TABLE `key_word` (
`primary_key` bigint(20) NOT NULL AUTO_INCREMENT,
`indexVal` int(11) NOT NULL,
`hashed_word` char(3) NOT NULL,
PRIMARY KEY (`primary_key`),
KEY `hashed_word` (`hashed_word`,`indexVal`)
) ENGINE=InnoDB AUTO_INCREMENT=28570982 DEFAULT CHARSET=latin1
Below is how the test2.key_word table is created
CREATE TABLE `key_word` (
`primary_key` bigint(20) NOT NULL,
`indexVal` int(11) NOT NULL,
`hashed_word` char(3) NOT NULL,
PRIMARY KEY (`primary_key`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Why this is not getting copied? Please help.
Use as per below, you have made db and tablename as a single name encsoling by db.table.
INSERT INTO test2.key_word SELECT * from test.key_word;
Note: Make sure no of columns should be same in source and target table.

How can I create a trigger for table logs using mysql?

Can you help me with my code? Because I am creating a table logs for my project. So I include a TRIGGER in my table but there's an error with my sql code. Here's my sql codes.
CREATE TABLE `sales_category` (
`salescatid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`salescatname` VARCHAR(128) NOT NULL,
`salescatdesc` VARCHAR(512) NOT NULL,
UNIQUE INDEX `salescatname` (`salescatname`),
UNIQUE INDEX `salescatid` (`salescatid`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
CREATE TABLE `category_log` (
`action` ENUM('CREATE','UPDATE','DELETE') NULL DEFAULT NULL,
`id` INT(10) UNSIGNED NOT NULL,
`salescatname` VARCHAR(255) NOT NULL,
`salescatdesc` VARCHAR(255) NOT NULL,
INDEX `id` (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
//Here's the error: SQL error 1054: Unknown column 'id' in 'NEW'
DELIMITER #
CREATE TRIGGER ai_category
AFTER INSERT ON sales_category
FOR EACH ROW
BEGIN
INSERT INTO category_log(action,id,salescatname,salescatedesc)
VALUES('CREATE',NEW.id,NEW.salescatname,NEW.salescatdesc);
END;#
Please help me with this thanks. I can't spot where did I go wrong with my code.
Ok i found my error. I just rename the fields in category_log exactly the sa me as my sales_category table.