Create two tables in same schema.sql H2 - spring-boot

I'm using Spring boot, and I have to initiate two tables for testing. I'm using the schema.sql inside the resource folder. But when I'm trying to create two tables in the same script and run the app, it can't load the application context.
Here is my schema.sql which I have placed at resource folder:
CREATE TABLE JobStatus_FO
(
id int(11) NOT NULL AUTO_INCREMENT,
businessDate timestamp NOT NULL,
label varchar(50),
);
CREATE TABLE JobStatusDetails_FO
{
id int(11) NOT NULL,
name varchar(50),
};

Please find the correct scripts as
CREATE TABLE JobStatus_FO
(
id INT(11) NOT NULL AUTO_INCREMENT,
businessDate TIMESTAMP NOT NULL,
label VARCHAR(50),
KEY id(id)
);
CREATE TABLE JobStatusDetails_FO
(
id INT(11) NOT NULL,
NAME VARCHAR(50)
);
Your syntax is not correct of create table .
1) In your scripts you have used extra comma "," before closing parentheses
2) Auto increment column should be used as a key in table
3) Curly braces "{" is not used within create table.
Hope this will work in to your project.

Related

Display the page title dynamically

I've an existing CodeIgniter Application with lot of pages where all the page titles( h1 tag ) are hard coded in the views. Now I'm looking to populate the titles dynamically from the below table structure with as much less effort as possible.
CREATE TABLE `page_title` (
`id` int(11) NOT NULL,
`page_no` tinytext NOT NULL,
`page_title` varchar(255) NOT NULL,
`date` tinytext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `page_title`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
Need suggestion on how to display the title with a good approach. I was thinking to create a common model and call the model in the view with page_id pass to get the page_no and page_title for that id.

Oracle SQL: Missing right parenthesis

I'm migrating a database from mySQL to Oracle SQL but I'm getting a "ORA-00907: missing right parenthesis" error when creating a table. I've tried everything I can think of but still keep getting the same error.
Create table statement:
CREATE TABLE menu
(id int(11) NOT NULL AUTO_INCREMENT,
restaurant_id varchar(30) DEFAULT NULL,
menu_name varchar(30) DEFAULT NULL,
menu_description varchar(500) DEFAULT NULL,
menu_price varchar(30) DEFAULT NULL,
quantity int(11) DEFAULT '1',
PRIMARY KEY (id))
I think the problem is with the PRIMARY KEY as it's only table with PRIMARY KEYs that I get the error on. Apologies if this is an obvious question, I'm new to Oracle SQL. Thanks in advance!
Oracle != MySQL:
CREATE TABLE menu
( id number(11,0) GENERATED AS IDENTITY, --IDENTITY <=> AUTO_INCREMENT
restaurant_id varchar2(30) DEFAULT NULL, --VARCHAR2 instead of VARCHAR
menu_name varchar2(30) DEFAULT NULL,
menu_description varchar2(500) DEFAULT NULL,
menu_price varchar2(30) DEFAULT NULL,
quantity number(11,0) DEFAULT '1', --NUMBER(11,0) instead of INT(11)
PRIMARY KEY (id)
);

Missing fields in a joomla sql schema

I exported my joomla.sql file through phpMyAdmin and one of the sql statement below is not complete.
CREATE TABLE XXXX_postinstall_messages (
postinstall_message_id bigint(20) UNSIGNED NOT NULL,
extension_id bigint(20) NOT NULL DEFAULT '700' COMMENT 'FK to #__extensions',
title_key varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''COMMENT
) ;
Can someone assist to provide the full schema for this particular table?
here you'll find all joomla tables https://github.com/joomla/joomla-cms/blob/staging/installation/sql/mysql/joomla.sql

H2 Schema initialization. Syntax error in SQL statement

I have a spring boot application and I trying to initialize some data on application startup.
This is my application properties:
#Database connection
spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=schema.sql
#Hibernate configuration
#spring.jpa.hibernate.ddl-auto = none
This is schema.sql:
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
and data.sql
INSERT INTO `Person` (
`id`,
`first_name`,
`age`
) VALUES (
1,
'John',
20
);
But I got 'Syntax error in SQL statement' on application startup:
19:08:45.642 6474 [main] INFO o.h.tool.hbm2ddl.SchemaExport - HHH000476: Executing import script '/import.sql'
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: CREATE TABLE Person (
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"
Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"; SQL statement:
I can't understand, what's wrong with this SQL.
Try this code. Remove PRIMARY KEY(id) and execute it.
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL
);
This error results from the structure of the CREATE TABLE declaration.
It will be the result when you have an extra comma in the end of your SQL declaration--no column declaration following the comma. For example:
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL, --note this line has a comma in the end
);
That's because CREATE TABLE expects a list of the columns that will be created along with the table, and the first parameter of the column is the identifier. As you check here, the column declaration follows the structure:
identifier datatype <constraints> <autoincrement> <functions>
Thus, in your case, as #budthapa and #Vishwanath Mataphati have mentioned, you could simply remove the PRIMARY KEY(id) line from the CREATE TABLE declaration. Moreover, you have already stated that id is a primary key on the first line of the column definitions.
In case you do not have a statement as the PRIMARY KEY declaration, be sure to check for the extra comma following your last column declaration.
Try this, as you have used Table_name
CREATE TABLE IF NOT EXISTS Person (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
age INTEGER NOT NULL
);
I was add below in to application.properties and it work for me
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
What helped in my case was removing single quotes from the table name in my insert query
I had to change this:
INSERT INTO 'translator' (name, email) VALUES ('John Smith', 'john#mail.com');
to this:
INSERT INTO translator (name, email) VALUES ('John Smith', 'john#mail.com');
You set auto increment id, so you can't insert new record with id.
Try INSERT INTO `Person` (
`first_name`,
`age`
) VALUES (
'John',
20
);
I ran into same issue. I fixed that with these application.properties:
spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
Some issue with multi-line and default encoding.

Table creation with h2 database

I am new to h2.I just using h2 in spring embedded mode with hibernate.I am trying to execute the following scripts using h2.
CREATE TABLE acct_authority (
id bigint(20) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
value varchar(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
The table acct_authority is created without any error.But if i create another table with the following script.
CREATE TABLE acct_role (
id bigint(20) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
It shows error as constraint name allready exists.What mistake i did.
You tried to create two constraints with same name. As you see, both CREATE TABLE statements contain following:
UNIQUE KEY name (name)
Result is that first one creates constraint named name, and second one fails because constraint name already exists. Problem can be solved by using unique names. Also in general it makes sense have little bit more descriptive names for database objects. Maybe you can use for example something like following:
UNIQUE KEY acct_authority_name_UNIQUE (name)
...
UNIQUE KEY acct_role_name_UNIQUE (name)

Resources