Error executing Create table statement in Java? - java

I am trying to create 2 tables in MySql using Java JDBC,the first one runs fine but I get an error when I execute the second create table command. The problem looks like in the Foreign key definition not sure what's missing ?
Error :
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server
version for the right syntax to use near 'FOREIGN KEY(UniqueBusID)
REFERENCES BUS_3_CLEARBROOK_UFV_GOLINE_TO_UFV_BusDetai' at line 1
Code
String table_UniqueBusNameTimings = BusDetails+"_BusTimings";
String timings= "Timings";
String dayofweek = "DayOfWeek";
String FuniqueBusID = "UniqueBusID";
String table_UniqueBusNameDetails = BusDetails+"_BusDetails";
String PuniqueBusID = "UniqueBusID";
String StopNames = "StopNames" ;
String sql1 = "CREATE TABLE "+table_UniqueBusNameDetails+
"(UniqueBusID VARCHAR(255) not NULL, " +
" StopNames VARCHAR(1000), " +
" PRIMARY KEY ( UniqueBusID ))";
String sql2 = "CREATE TABLE "+table_UniqueBusNameTimings+
"(Timings VARCHAR(255) , " +
" DayOfWeek VARCHAR(25), " +
" UniqueBusID VARCHAR(255) "+
" FOREIGN KEY(UniqueBusID) REFERENCES "+table_UniqueBusNameDetails+"(UniqueBusID))";
stmt.executeUpdate(sql1);
stmt.executeUpdate(sql2);

You have a comma missing after UniqueBusID VARCHAR(255). Just change it to UniqueBusID VARCHAR(255), and your code shall be fine.
For details on syntax related to CREATE TABLE with FOREIGN KEY, you can explore this page: http://www.w3schools.com/sql/sql_foreignkey.asp

We should use CREATE TABLE IF NOT EXISTS... then if we are having foreign key relation then we should use SET FOREIGN_KEY_CHECKS=0; Finally we will execute a batch what having all the statement and then commit.

Every attribute like column, index, foreign key etc. should be seprated by comma ',' in table creation syntax. so here a comma missing after UniqueBusID VARCHAR(255). It will be fine after adding comma here.
#Suzon: We should not set foreign_key_checks=0 specially during creating table or adding any constraint at it will skip any constraint checking by mysql and create major issue in database. We can use it only we are sure its impact for example we want to delete some rows from master table even child table contains corresponding rows and we are fine with this etc.

Related

Creating a table in mysql with java named by the user?

The question is, is there a way to create a new table named by the user from a text field. I know its a huge injection port, but i really need new tables, it will work only offline. I tried
String newtable = jTextField1.getText();
PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS '"+newtable+"'(ID INTEGER NOT NULL AUTO_INCREMENT, IDapol INTEGER, ΗΜΕΡΟΜΗΝΙΑ DATE, ΕΣΟΔΑ DOUBLE, PRIMARY KEY(ID), CONSTRAINT IDapol FOREIGN KEY(IDapol) REFERENCES apol(IDapol)");
but i get an error saying: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "1718"(ID INTEGER NOT NULL AUTO_INCREMENT, IDapol, INTEGER, ΗΜΕΡΟΜΗΝΙΑ' at line 1
1718 is the value of my textField1.
Any help i could use? Thanks
As per here: https://dev.mysql.com/doc/refman/5.7/en/identifiers.html , "Identifiers may begin with a digit but unless quoted may not consist solely of digits."
Also, currently your code is wide open for an SQL injection attack.
Table names are never in '. Either use backticks (`) or simply nothing:
"CREATE TABLE IF NOT EXISTS " + newtable + " (...)
Your table's name should also start with a character!
CREATE TABLE IF NOT EXISTS 1718
(ID INTEGER NOT NULL AUTO_INCREMENT,
IDapol INTEGER,
ΗΜΕΡΟΜΗΝΙΑ DATE,
ΕΣΟΔΑ DOUBLE,
PRIMARY KEY(ID),
CONSTRAINT IDapol
FOREIGN KEY(IDapol) REFERENCES apol(IDapol);
give table name in `` codes

create table user("user_id int auto_increment"); not working in derby (embedded database)

I am new in derby library. why I got this error when I use the auto_increment in my query?
here is my java code
this.conn.createStatement().execute(create table user("user_id int auto_increment, PRIMARY KEY(user_id))");
I tried this in mysql server and its works but in derby I got this error
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "auto_increment" at line 1
why I got this error?
Derby does not have auto_increment as a keyword. In derby you need to use identity columns to implement auto increment behaviour
For example
CREATE TABLE students
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(24) NOT NULL,
address VARCHAR(1024),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;
Above statement will create Student table with id as auto increment column and primary key as well.
Hope this helps

how to make non primary key column is auto incremented

i am using sqlite with java JDBC, and i created the following table:
private final String sqlTableNode = "CREATE TABLE "+this.TABLE_NODE+
" ( "+this.NODE_TABLE_ID_COL+" INTEGER, " +
this.NODE_TABLE_NODE_ID_COL+" TEXT NOT NULL, " +
this.NODE_TABLE_LAT_COL+" TEXT NOT NULL, " +
this.NODE_TABLE_LNG_COL+" TEXT NOT NULL, " +
"PRIMARY KEY ("+this.NODE_TABLE_LAT_COL+", "+this.NODE_TABLE_LNG_COL+") );";
my question is, how can i make the first column "this.NODE_TABLE_PK_COL" is auto incremented while it is not a primary key??
Unfortunately, it's not possible. The SQLite documentation on Autoincrement says at the bottom of the page:
Because AUTOINCREMENT keyword changes the behavior of the ROWID selection algorithm, AUTOINCREMENT is not allowed on WITHOUT ROWID tables or on any table column other than INTEGER PRIMARY KEY. Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in an error.
As the documentation shows, autoincrement works only for the primary key.
#rmaik:
No. You can't. You can find the documentation here : https://www.sqlite.org/autoinc.html
It clearly states :
AUTOINCREMENT is not allowed on WITHOUT ROWID tables or on any table column other than INTEGER PRIMARY KEY. Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in an error.
If I'm understanding your requirement, what you can do in this scenario is that, create primary key on
this.NODE_TABLE_ID_COL.
Since, it is integer, it fits the criteria for Primary key.
And create unique key on :
UNIQUE KEY ("+this.NODE_TABLE_ID_COL+", "+this.NODE_TABLE_LAT_COL+", "+this.NODE_TABLE_LNG_COL+") )

UCanAccessSQLException: constraint violation

I'm having problems with my Java program.
I'm using MS Access as a database and UCanAccess to connect to the database.
When I'm trying to insert a text into the database, I get an Exception:
net.ucanaccess.jdbc.UcanaccessSQLException: integrity constraint violation: unique constraint or index violation; ENTRIES_PRIMARYKEY table: ENTRIES
This is the SQL statement, that results in the exception:
"INSERT INTO Entries (Text, Title, Date, Time) VALUES"
+ "(\"" + text + "\", \"" + title + "\", \"" + date + "\", \"" + time + "\");";
The primary keys of table Entries is (Title, Date).
The information I'm inserting does not exist in the table.
I've made a System.out.println() containing the same string to make sure the variables contain the correct information, and they do.
Can someone tell me what I'm doing wrong?
(from comment to question:)
I tried inserting a row, and it worked. But when I tried to insert more rows, the problem occurred.
If the Primary Key for the table is (Title, Date) then the values in those two columns (fields) must be unique across all rows. That is, you can insert a row that looks like
Text Title Date Time
---------- ------ ---------- --------
some text Title1 2015-06-01 05:56:15
but if you try to insert another row like this
Text Title Date Time
---------- ------ ---------- --------
other text Title1 2015-06-01 06:15:44
the insert will fail because that combination of Title and Date already exists in the table. Yes, that row "doesn't exist" because the Text and Time values are different, but the Primary Key constraint doesn't care; it only looks at the Title and Date columns.
If you really need to insert that second row then you'll need to change the Primary Key for the table.

How to programmatically transfer a lot of data between tables?

i have two tables where in the first one i have 14 millions and in the second one i have 1.5 million of data.
So i wonder how could i transfer this data to another table to be normalized ?
And how do i convert some type to another, for example: i have a field called 'year' but its type is varchar, but i want it an integer instead, how do i do that ?
I thought about do this using JDBC in a loop while from java, but i think this is not effeciently.
// 1.5 million of data
CREATE TABLE dbo.directorsmovies
(
movieid INT NULL,
directorid INT NULL,
dname VARCHAR (500) NULL,
addition VARCHAR (1000) NULL
)
//14 million of data
CREATE TABLE dbo.movies
(
movieid VARCHAR (20) NULL,
title VARCHAR (400) NULL,
mvyear VARCHAR (100) NULL,
actorid VARCHAR (20) NULL,
actorname VARCHAR (250) NULL,
sex CHAR (1) NULL,
as_character VARCHAR (1500) NULL,
languages VARCHAR (1500) NULL,
genres VARCHAR (100) NULL
)
And this is my new tables:
DROP TABLE actor
CREATE TABLE actor (
id INT PRIMARY KEY IDENTITY,
name VARCHAR(200) NOT NULL,
sex VARCHAR(1) NOT NULL
)
DROP TABLE actor_character
CREATE TABLE actor_character(
id INT PRIMARY KEY IDENTITY,
character VARCHAR(100)
)
DROP TABLE director
CREATE TABLE director(
id INT PRIMARY KEY IDENTITY,
name VARCHAR(200) NOT NULL,
addition VARCHAR(150)
)
DROP TABLE movie
CREATE TABLE movie(
id INT PRIMARY KEY IDENTITY,
title VARCHAR(200) NOT NULL,
year INT
)
DROP TABLE language
CREATE TABLE language(
id INT PRIMARY KEY IDENTITY,
language VARCHAR (100) NOT NULL
)
DROP TABLE genre
CREATE TABLE genre(
id INT PRIMARY KEY IDENTITY,
genre VARCHAR(100) NOT NULL
)
DROP TABLE director_movie
CREATE TABLE director_movie(
idDirector INT,
idMovie INT,
CONSTRAINT fk_director_movie_1 FOREIGN KEY (idDirector) REFERENCES director(id),
CONSTRAINT fk_director_movie_2 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT pk_director_movie PRIMARY KEY(idDirector,idMovie)
)
DROP TABLE genre_movie
CREATE TABLE genre_movie(
idGenre INT,
idMovie INT,
CONSTRAINT fk_genre_movie_1 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT fk_genre_movie_2 FOREIGN KEY (idGenre) REFERENCES genre(id),
CONSTRAINT pk_genre_movie PRIMARY KEY (idMovie, idGenre)
)
DROP TABLE language_movie
CREATE TABLE language_movie(
idLanguage INT,
idMovie INT,
CONSTRAINT fk_language_movie_1 FOREIGN KEY (idLanguage) REFERENCES language(id),
CONSTRAINT fk_language_movie_2 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT pk_language_movie PRIMARY KEY (idLanguage, idMovie)
)
DROP TABLE movie_actor
CREATE TABLE movie_actor(
idMovie INT,
idActor INT,
CONSTRAINT fk_movie_actor_1 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT fk_movie_actor_2 FOREIGN KEY (idActor) REFERENCES actor(id),
CONSTRAINT pk_movie_actor PRIMARY KEY (idMovie,idActor)
)
UPDATE:
I'm using SQL Server 2008.
Sorry guys i forgot to mention that are different databases :
The not normalized is call disciplinedb and the my normalized call imdb.
Best regards,
Valter Henrique.
If both tables are in the same database, then the most efficient transfer is to do it all within the database, preferably by sending a SQL statement to be executed there.
Any movement of data from the d/b server to somewhere else and then back to the d/b server is to be avoided unless there is a reason it can only be transformed off-server. If the destination is different server, then this is much less of an issue.
Though my tables were dwarfs compared to yours, I got over this kind of problem once with stored procedures. For MySQL, below is a simplified (and untested) essence of my script, but something similar should work with all major SQL bases.
First you should just add a new integer year column (int_year in example) and then iterate over all rows using the procedure below:
DROP PROCEDURE IF EXISTS move_data;
CREATE PROCEDURE move_data()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE orig_id INT DEFAULT 0;
DECLARE orig_year VARCHAR DEFAULT "";
DECLARE cur1 CURSOR FOR SELECT id, year FROM table1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
PREPARE stmt FROM "UPDATE table1 SET int_year = ? WHERE id = ?";
read_loop: LOOP
FETCH cur1 INTO orig_id, orig_year;
IF done THEN
LEAVE read_loop;
END IF;
SET #year= orig_year;
SET #id = orig_id;
EXECUTE stmt USING #orig_year, #id;
END LOOP;
CLOSE cur1;
END;
And to start the procedure, just CALL move_data().
The above SQL has two major ideas to speed it up:
Use CURSORS to iterate over a large table
Use PREPARED statement to quickly execute pre-known commands
PS. for my case this speeded things up from ages to seconds, though in your case it can still take a considerable amount of time. So it would be probably best to execute from command line, not some web interface (e.g. PhpMyAdmin).
I just recently did this for ~150 Gb of data. I used a pair of merge statements for each table. The first merge statement said "if it's not in the destination table, copy it there" and the second said "if it's in the destination table, delete it from the source". I put both in a while loop and only did 10000 rows in each operation at a time. Keeping it on the server (and not transferring it through a client) is going to be a huge boon for performance. Give it a shot!

Categories