Hello i am a student and not familiar with MySQL.
I am new in mysql and i need help!
I have a database and have some troubles inserting data into one table.
The table name is makina
CREATE TABLE `makina` (
`lloji` varchar(20) DEFAULT NULL,
`vitprodhimi` int(9) DEFAULT NULL,
`ngjyra` enum('bardhe','blu','kuqe','zeze') DEFAULT 'bardhe'
) ENGINE=InnoDB DEFAULT CHARSET=latin1
And for the column ngjyra i doesn't accept the value red.
The error is because of ENUM data type, it accept only your specific values.
Read more on : https://dev.mysql.com/doc/refman/8.0/en/enum.html
If you want to accept more values you should alter your table : How do I add more members to my ENUM-type column in MySQL?
ALTER TABLE
`makina`
MODIFY COLUMN
`ngjyra` enum(
'existing_value1',
'existing_value2',
'existing_value3',
'existing_value4',
'new_value1',
'new_value2'
)
DEFAULT `bardhe`;
Related
I'm using AWS Aurora MySQL 5.7. I have a column that is of type MEDIUMTEXT that I'd like to convert to JSON. After using an ALTER to accomplish this, I can neither update the newly JSON-typed field of an existing record nor insert new records. In both cases, a Cannot CAST value to JSON error is thrown. the default character set for this database is latin1, but the character set on this table is utf8.
The table is defined like:
CREATE TABLE `table_sample` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`config` mediumtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT
I've tried changing the column type via:
ALTER TABLE table_sample
ADD COLUMN config2 JSON DEFAULT NULL;
UPDATE table_sample
SET config2 = IF(JSON_VALID(config), config, NULL);
ALTER TABLE table_sample
DROP COLUMN config;
ALTER TABLE table_sample
CHANGE config2 config JSON;
Attempting to duplicate a record like this, after the conversion to JSON, is one way to cause the aforementioned CAST error:
INSERT INTO table_sample (config)
SELECT config FROM table_sample WHERE id = 1;
I'm struggling a bit understanding spark and writing dataframes to a mysql database. I have the following code:
forecastDict = {'uuid': u'8df34d5a-ce02-4d02-b282-e10363690122', 'created_at': datetime.datetime(2014, 12, 31, 23, 0)}
forecastFrame = sqlContext.createDataFrame([forecastDict])
forecastFrame.write.jdbc(url="jdbc:mysql://example.com/example_db?user=bla&password=blabal123", table="example_table", mode="append")
The last line in the code throws the following error:
Incorrect datetime value: '8df34d5a-ce02-4d02-b282-e10363690122' for column 'created_at' at row 1
I can post the entire stack trace if necessary, but basically what's happening here is that the pyspark is mapping the uuid field to the wrong column in mysql. Here's the mysql definition:
mysql> show create table example_table;
...
CREATE TABLE `example_table` (
`uuid` varchar(36) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
...
If we change the mysql definition to the following (notice that only the order of the columns is different):
CREATE TABLE `example_table` (
`created_at` datetime NOT NULL,
`uuid` varchar(36) NOT NULL,
PRIMARY KEY (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The insert works fine. Is there a way to implement this without being dependent on the order of the columns, or what's the preferred way of saving data to an external relational database from spark?
Thanks!
--chris
I would simply force expected order on write:
url = ...
table = ...
columns = (sqlContext.read.format('jdbc')
.options(url=url, dbtable=table)
.load()
.columns())
forecastFrame.select(*columns).write.jdbc(url=url, dbtable=table, mode='append')
Also be careful with using schema inference on dictionaries. This is not only deprecated but also rather unstable.
I am trying to add a column to a table.To do so I am trying
ALTER TABLE requirements Modify COLUMN parent_id int(11);
but when I try to execute this query mysql does not respond for long.So each time I have to kill the query.
I have created the table using
CREATE TABLE requirements (requirement_id smallint(6) NOT NULL AUTO_INCREMENT,
product_id smallint(6) NOT NULL,
name varchar(255) CHARACTERSET latin1 NOT NULL DEFAULT '',
PRIMARY KEY (requirement_id),
UNIQUE KEY requirement_product_id_name_idx (product_id,name),
UNIQUE KEY requirement_product_idx (requirement_id,product_id),
KEY requirement_name_idx_v2 (name) )
ENGINE=InnoDB
AUTO_INCREMENT=7365
DEFAULT CHARSET=utf8;
Please help me know why I am not able to execute the Alter table query.I am new to database is there something wrong with my alter table query.
According to your table defintion parent_id seems to be a new column which you want to add so your query should be to add the column not modify.
Try this:
alter table requirements add column parent_id int(11);
SQL FIDDLE DEMO
On a side note:
There needs to be a space between CHARACTERSET here
name varchar(255) CHARACTERSET latin1 NOT NULL DEFAULT '',
should be
name varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '',
In one of my tables, I have a column with some null values and some integer values. When I use MySQL Workbench to set a default value and non-null for that column, I get a truncation error.
When my colleague performs the same operation, his apply works and the null value are set to 0.
The only obvious difference is that he is running MySQL Server 5.6 and I am running 5.5.
An example table can be created with the following CREATE:
CREATE TABLE 'resource`.`test_table` (
`idtest_table` INT NOT NULL ,
`bad_column` BIGINT(20) NULL ,
PRIMARY KEY (`idtest_table`) );
Insert some values as follows:
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (1);
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (2);
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (3);
The command that is failing is:
ALTER TABLE `resource`.`test_table` CHANGE COLUMN `bad_column` `bad_column` BIGINT(20) NOT NULL DEFAULT 0 ;
with the following error:
ERROR 1265: Data truncated for column 'bad_column' at row 1
SQL Statement:
ALTER TABLE `resource`.`test_table` CHANGE COLUMN `bad_column` `bad_column` BIGINT(20) NOT NULL DEFAULT 0
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'test_table' already exists
SQL Statement:
CREATE TABLE `test_table` (
`idtest_table` int(11) NOT NULL,
`bad_column` bigint(20) DEFAULT NULL,
PRIMARY KEY (`idtest_table`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Is there a setting in the MySQL config that allows this? Is it the version?
The error is occurring because bad_columns has a value of NULL after the insert.
You are changing the column type to "not NULL", so what can the database do? It can generate an error. The default value doesn't apply to existing rows.
If you want to do this, then update the column first:
update resource.test_table
set bad_column = 0
where bad_column is null;
I am using migration toolkit for the migration but i am getting these errors in the process of migration
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Incorrect string value: '\xEF\xBF\xBDs d...' for column 'MESSAGE' at row 5
0 row(s) transferred.
For the fixing the first error i got something here http://terrencemiao.com/Webmail/msg00949.html
but i am not getting the second error what it is and why is it there how to fix it also suggest me some better ideas for fixing the first one if there any apart from what mentioned in the link
USE `MyDB`
Creating tables ...
Creating table MyTable...
DROP TABLE IF EXISTS `MyTable`
Creating table MyTable ...
SET NAMES UTF8;
CREATE TABLE `MyTable` (
`PrimaryKey` INT(10) NOT NULL AUTO_INCREMENT,
`FK_QUESTION_ID` INT(10) NOT NULL,
`ANSWER` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL
PRIMARY KEY (`PK_ID`)
)
ENGINE = INNODB
i am getting error for answer column
*Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause*
This is right, you should not create more then one such fields.
Incorrect string value: '\xEF\xBF\xBDs d...' for column 'MESSAGE' at row 5 0 row(s) transferred.
Possible encoding error, try to run 'SET NAMES UTF8;' before inserting data
Try this statement,
CREATE TABLE `MyTable` (
PK_ID INT(11) NOT NULL AUTO_INCREMENT,
FK_QUESTION_ID INT(11) NOT NULL,
ANSWER LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
PRIMARY KEY (`PK_ID`)
)
ENGINE = INNODB;
You missed a comma and it was wrong field name. Be careful with migration toolkit. Check generated field types, for example if you do not need 4GB text values, you could use simple VARCHAR instead of LONGTEXT.