Please have a look:
CREATE TABLE Uzytkownik(
user_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
login VARCHAR(25) NOT NULL,
password VARCHAR(16) NOT NULL,
CONSTRAINT user_pk PRIMARY KEY (user_id)
);
CREATE TABLE Wizytowka(
wizytowka_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
tytul VARCHAR(60) NOT NULL,
tresc VARCHAR(400) NOT NULL,
premium_w INTEGER(1) NOT NULL,
id_user INTEGER NOT NULL,
CONSTRAINT wizytowka_id PRIMARY KEY (wizytowka_id),
FOREIGN KEY (id_user) REFERENCES Uzytkownik(user_id)
);
I got Syntax Error: Encountered "(" at line 5, column 26.
Line 8, column 1
The problem is in your second create statement. Derby does not support a length attribute for the integer type. Therefore
premium_w INTEGER(1) NOT NULL,
results in an error. Modify it to
premium_w INTEGER NOT NULL,
and it will work.
Related
I am new to java. I am trying to create two SQL codes to create a table in Java DB (I am using NetBeans 12.5). I set it up how I thought it should be set up but I am still getting an error.
CREATE TABLE CheckInLocation(
CheckInLocationID int NOT NULL PRIMARY KEY,
StationName VARCHAR(30) NOT NULL,
);
CREATE TABLE Passenger (
PassengerID int NOT NULL PRIMARY KEY,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
CheckInDateTime TIMESTAMP NOT NULL,
FOREIGN KEY (CheckInLocationID) REFERENCES CheckInLocation(CheckInLocationID),
);
The error I am getting is this:
[Exception, Error code 30,000, SQLState 42X01] Syntax error: Encountered ")" at line 7, column 1.
Any help at all I will appreciate. Thank you.
Problem is the final comma in both your create tables. For example, replace
StationName VARCHAR(30) NOT NULL,
with
StationName VARCHAR(30) NOT NULL
When I am using this command to generate mybatis xml file:
java -jar mybatis-generator-core-1.3.4.jar -configfile generatorConfig.xml -overwrite
everything works fine but finnaly i found the user mapper result file did not genreate SelectByPrimaryKey function. this is part of my generate file config:
<table tableName="users"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true"
selectByPrimaryKeyQueryId="true"
selectByExampleQueryId="true">
<generatedKey column="ID" sqlStatement="JDBC" identity="true" />
</table>
my database is PostgreSQL 13. what should I do to fix it? This is my user table DML:
CREATE TABLE public.users (
id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
nickname varchar NULL,
avatar_url varchar NULL,
phone varchar NOT NULL,
updated_time int8 NOT NULL,
created_time int8 NOT NULL,
salt varchar NULL,
pwd varchar NULL,
sex int4 NULL,
level_type varchar NULL,
phone_region varchar NULL,
country_code int4 NULL,
user_status int4 NULL DEFAULT 1,
last_login_time int8 NULL,
first_login_time int8 NULL,
app_id int4 NOT NULL,
register_time int8 NULL,
apple_iap_product_id varchar NULL,
CONSTRAINT unique_phone UNIQUE (phone)
);
The DML you posted shows that the table doesn't have a primary key. You have two options.
You could define a primary key in the table by adding this to the create table statement:
create table public.users (
...
primary key (id)
);
Or, if you don't want to define a primary key in the database, you can use the "VirtualPrimaryKeyPlugin" in MyBatis Generator. See here for details: https://mybatis.org/generator/reference/plugins.html
application_user
-- auto-generated definition
create table application_user
(
id bigint auto_increment
primary key,
email varchar(255) not null,
is_active bit null,
name varchar(255) not null,
password varchar(255) not null,
surname varchar(255) not null,
username varchar(255) not null
)
engine = MyISAM;
I have a table that generated by hibernate.
I want to create a table and add a foreign key manually.
So far I tried this
application_user_log
CREATE TABLE application_user_log (
log_id BIGINT NOT NULL AUTO_INCREMENT,
fk_user_id BIGINT NOT NULL,
old_user_name BIGINT NOT NULL,
new_user_name BIGINT NOT NULL,
PRIMARY KEY (log_id),
FOREIGN KEY (fk_user_id) REFERENCES application_user(id)
) ;
And I got this error message.: [HY000][1215] Cannot add foreign key constraint
Why I got this error?
Well I don't know why my answer got converted into a comment, but I knonw that MyISAM doesn't support foreign keys. You can read details here.
All answers to the issue "Incorrect string value" point to using utf8mb4. However, I'm still using that I still get that error.
Running
SELECT CCSA.character_set_name, collation_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "test"
AND T.table_name = "products";
produces
character_set_name collation_name
utf8mb4 utf8mb4_unicode_ci
And running
SELECT character_set_name, collation_name FROM information_schema.`COLUMNS`
WHERE table_schema = "test"
AND table_name = "products"
AND column_name = "description";
produces the same result. Then, when I try to update the column description:
UPDATE test.products SET description='Esperienza molto negativa... 😁😁😁😁SCONSIGLIATISSIMO' WHERE id='50'
I get the abovementioned error
1366: Incorrect string value: '\xF0\x9F\x98\x81\xF0\x9F...' for column 'description' at row 1
I've tried this update both through a jdbc connection jdbc:mysql://localhost:3306/test?useUnicode=yes;characterEncoding=utf8; and directly through MySQLWorkbench. I don't know what else to try of look for. What am I missing?
EDIT
Output of SHOW CREATE TABLE
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`manufacturerId_fk` int(11) NOT NULL,
`type_fk` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'TEST',
`ownerId_fk` int(11) DEFAULT NULL,
`rating_fk` int(11) NOT NULL,
`description` varchar(2000) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`authorName` varchar(75) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`authorUrl` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `manufacturer_authorUrl_UNIQUE` (`manufacturerId_fk`,`authorUrl`),
KEY `fk_products_manufacturers_idx` (`manufacturerId_fk`),
KEY `fk_products_users_idx` (`ownerId_fk`),
KEY `fk_products_validRatings_idx` (`rating_fk`),
KEY `fk_products_productTypes` (`type_fk`),
CONSTRAINT `fk_products_productTypes` FOREIGN KEY (`type_fk`) REFERENCES `productTypes` (`type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_products_manufacturers` FOREIGN KEY (`manufacturerId_fk`) REFERENCES `manufacturers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_products_validRatings` FOREIGN KEY (`rating_fk`) REFERENCES `validRatings` (`value`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=418 DEFAULT CHARSET=utf8mb4
A similar question occurred to me with a text field.
(MySQL 1366 error incorrect string value)
I confirmed the database, table, column character set were all utf8mb4.
The problem was solved by changing the field type to longtext.
Hope this could help someone.
Here are my table definition
CREATE TABLE users_registration
(user_id int4 NOT NULL UNIQUE,
request_id int4 NOT NULL UNIQUE,
status bpchar NOT NULL,
session_id varchar(100) NOT NULL,
attempts int4,
PRIMARY KEY(user_id, request_id));
In one place it is taking request_id=1 everytime when new record creating but another new MYSQL Database i am getting thi exception
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'request_id'
Can anyone tell what is the issue using Hibernate in my application.