Incorrect string value: '\xE6\x8B\x93\xE6\xB5\xB7' for column 'bookName' at row 1 - navicat

When I add row in my table: book use navicat, there comes an issue:
Error
Incorrect string value: '\xE6\x8B\x93\xE6\xB5\xB7' for column 'bookName' at row 1
Why?
EDIT
I run show create table book;
CREATE TABLE `book` (
`bookName` varchar(50) NOT NULL,
`InsertTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`bookstore_Id` int(11) DEFAULT NULL,
`Id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`Id`),
KEY `fk_book_bookstore` (`bookstore_Id`),
CONSTRAINT `fk_book_bookstore` FOREIGN KEY (`bookstore_Id`) REFERENCES `bookstore` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='book'

You see the CHARSET=latin1 at the table, your book table default charset is latin1, you should change the charset.
alter table book default character set utf8;

Related

SELECT with yb_hash_code() and DELETE in YugabyteDB

[Question posted by a user on YugabyteDB Community Slack]
We have below schema in postgresql (yugabyte DB 2.8.3) using YSQL:
CREATE TABLE IF NOT EXISTS public.table1
(
customer_id uuid NOT NULL ,
item_id uuid NOT NULL ,
kind character varying(100) NOT NULL ,
details character varying(100) NOT NULL ,
created_date timestamp without time zone NOT NULL,
modified_date timestamp without time zone NOT NULL,
CONSTRAINT table1_pkey PRIMARY KEY (customer_id, kind, item_id)
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_item_id ON table1(item_id);
CREATE UNIQUE INDEX IF NOT EXISTS unique_item ON table1(customer_id, kind) WHERE kind='NEW' OR kind='BACKUP';
CREATE TABLE IF NOT EXISTS public.item_data
(
item_id uuid NOT NULL,
id2 integer NOT NULL,
create_date timestamp without time zone NOT NULL,
modified_date timestamp without time zone NOT NULL,
CONSTRAINT item_data_pkey PRIMARY KEY (item_id, id2)
);
Goal:
Step 1) Select item_id’s from table1 WHERE modified_date < someDate
Step 2) DELETE FROM table item_data WHERE item_id = any of those item_id’s from step 1
Currently we use query
SELECT item_id FROM table1 WHERE modified_date < $1
Can the SELECT query apply yb_hash_code(item_id) with the SELECT query? Because table1 is indexed on item_id ? to enhance the performance of the SELECT query
Currently we perform:
DELETE FROM item_data x WHERE x.item_id IN the listOfItemIds(provided in Step1 above).
With the given listOfItemIds, can we use yb_hash_code(item_id) to enhance performance of DELETE operation?
Yes, it should work out. Something like:
SELECT item_id FROM item_data WHERE yb_hash_code(customer_id, kind, item_id) <= 128 AND yb_hash_code(customer_id, kind, item_id) >= 0 AND modified_date < x;
While you can combine the SELECT + DELETE in 1 query (like a subselect), this is probably better because it will result in smaller transactions.
Also, no need to use yb_hash_code. The db should be able to find the correct rows since you’re sending the columns that are used for partitioning.

Spark SQL : INSERT Statement with JDBC does not support default value

I am trying to read/write data from other databases using JDBC.
just following the doc https://spark.apache.org/docs/latest/sql-data-sources-jdbc.html
But I found Spark SQL does not work well with Default value or AUTO_INCREMENT
CREATE TEMPORARY VIEW jdbcTable
USING org.apache.spark.sql.jdbc
OPTIONS (
url "jdbc:postgresql:dbserver",
dbtable "schema.tablename",
user 'username',
password 'password'
)
INSERT INTO TABLE jdbcTable (id) values (1)
Here is my DDL
CREATE TABLE `tablename` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`age` int(11) NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE
)
The error org.apache.spark.sql.AnalysisException: unknown requires that the data to be inserted have the same number of columns as the target table: target table has 2 column(s) but the inserted data has 1 column(s), including 0 partition column(s) having constant value(s).
Is there any way to support Default value or AUTO_INCREMENT? thx
I have discovered this same issue with columns with DEFAULT and also COMPUTED columns. If you are using SQL Server you can consider an AFTER INSERT TRIGGER otherwise you may need to calculate the id on the INSERT side.

SQL Table has primary key but error "Diesel only supports tables with primary keys. Table errors has no primary key" [duplicate]

This is my up.sql file:
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(255),
`last_name` VARCHAR(255),
`user_name` VARCHAR(255) NOT NULL UNIQUE,
`email` VARCHAR(255) NOT NULL UNIQUE,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
When I run diesel migration I receive the following error:
Diesel only supports tables with primary keys. Table users has no primary key
Not sure what I'm doing wrong, any help would be greatly appreciated!
I ended up manually creating the table in mysql directly, however when I tried to generate the schema I once again received the "no primary key" error message from the diesel cli. So I began playing around with my sql to see if I could make it work, and eventually it did using the following:
CREATE TABLE users (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`first_name` VARCHAR(255),
`last_name` VARCHAR(255),
`user_name` VARCHAR(255) NOT NULL UNIQUE,
`email` VARCHAR(255) NOT NULL UNIQUE,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
It seems that in order for diesel to recognize a column as having a PRIMARY KEY, the primary key attribute must be the last one specified.
I'm not sure if this is a bug, a known quirk, or valid syntax. In any case it was fairly annoying to track this down. I will do some more testing and accept this answer if it seems to be a robust solution.
(edit: I also tested this with a similar SQL query where AUTO_INCREMENT came after PRIMARY KEY, and it did not work)

How to add a NOT NULL foreign key to SQlite3 table in node.js

I am having some trouble adding a foreign key column to an already existing table on SQLite.
Here's my SQL code:
CREATE TABLE IF NOT EXISTS director (
director_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS movie (
movie_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
ALTER TABLE movie ADD COLUMN director_id INTEGER NOT NULL REFERENCES
director(director_id);
This give me the following error:
Uncaught Error: Cannot add a NOT NULL column with default value NULL
However, if I remove the "NOT NULL" constraint it runs fine:
ALTER TABLE movie ADD COLUMN director_id INTEGER REFERENCES
director(director_id);
The following also works just fine but I already have an existing table:
CREATE TABLE IF NOT EXISTS director (
director_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS movie (
movie_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
director_id INTEGER NOT NULL,
FOREIGN KEY(director_id) REFERENCES director(director_id)
);
Am I missing something or it cannot be made in SQLite?
When altering a table to add a column with NOT NULL, you need to add a DEFAULT value. Otherwise, it has no idea what to put in an existing record when adding the column (since NULL is not valid).
Docs ref: https://sqlite.org/lang_altertable.html
For example:
CREATE TABLE IF NOT EXISTS movie (
movie_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
ALTER TABLE movie ADD COLUMN director_id INTEGER NOT NULL DEFAULT 0 REFERENCES director(director_id);

Alter column in table to auto increment in Sybase ASE 16.0

I am using Sybase ASE16.0 database in which I am trying to alter a column in an existing USER table so that it autoincrements every time a row is added to the table. The column: user_id is set as primary key and not null.
I have gone through many sybase tutorials and have tried many approaches but of no avail. Here are some queries that I wrote to make this change:
ALTER TABLE USER (user_id smallint IDENTITY not null)
ALTER TABLE USER ALTER user_id smallint IDENTITY not null
ALTER TABLE USER MODIFY user_id smallint NOT NULL IDENTITY
ALTER TABLE USER MODIFY user_id smallint NOT NULL AUTO_INCREMENT
ALTER TABLE USER MODIFY user_id smallint NOT NULL AUTOINCREMENT
ALTER TABLE USER ALTER user_id smallint NOT NULL AUTOINCREMENT
ALTER TABLE USER user_id smallint AUTOINCREMENT
I expect a SYBASE DB compliant query that would alter the user_id column in the table to autoincrement it by 1 on adding a new record
From documentation:
Adds an IDENTITY column to a table. For each existing row in the table, Adaptive Server assigns a unique, sequential column value. The IDENTITY column could be type numeric or integer, and a scale of zero. The precision determines the maximum value (10 5 -1, or 99,999) that can be inserted into the column:
alter table sales_daily
add ord_num numeric (5,0) identity
Found here

Resources