CREATE TABLE IF NOT EXISTS `user` (
`USER_ID` bigint(20) NOT NULL auto_increment,
`USER_ABOUT_YOU` varchar(255) default NULL,
`USER_COMMUNITY` tinyblob,
`USER_COUNTRY` varchar(255) default NULL,
`USER_GENDER` varchar(255) default NULL,
`USER_MAILING_LIST` bit(1) default NULL,
`USER_NAME` varchar(255) default NULL,
`USER_PASSWORD` varchar(255) default NULL,
PRIMARY KEY (`USER_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Why is the USER_COMMUNITY defined as tiny blob. This field accepts a value of checkbox. When i change it to some other datatype i get an error? Why is to so?
http://www.vaannila.com/spring/spring-hibernate-integration-1.html
That's how it is defined:
#Column(name="USER_COMMUNITY")
public String[] getCommunity() {
return community;
}
public void setCommunity(String[] community) {
this.community = community;
}
The table doesn't store the checks but an array of Strings. And it looks like, TINYBLOB is the correct datatype on MYSQL for storing arrays.
There is no 'value of checkbox'. A checkbox returns a string as do most other HTML input controls. These checkboxes actually return a list of values, because they all have the same name. I don't know how this value is stored actually, but I can imagine that is is stored as an array of string with some accompanieing meta data. This kind of data will be hard to store in another field type.
In what field type did you want to change it, and what was the error you got?
I would actually not store this data this way, but give user a detail table instead in which you can store the communities the user is a member of. But this tutorial seems to focus more on jsp than database normalisation. ;)
Related
I recently upgraded h2 version from 1.4.200 to 2.0.206. Some of the queries that used to work in the older version are not working properly after the upgrade.
CREATE TABLE SOMETABLE (
ID INT(11) NOT NULL AUTO_INCREMENT,
SOURCE_ID VARCHAR(255) NOT NULL,
MESSAGE VARCHAR(255) NOT NULL,
PRIMARY KEY (`ID`)
);
CREATE TABLE IF NOT EXISTS SOMEOTHERTABLE (
ID VARCHAR(255) NOT NULL,
NAME VARCHAR(255) NOT NULL,
CREATED_TIME TIMESTAMP NOT NULL,
LAST_MODIFIED TIMESTAMP NOT NULL,
HAS_FILE BOOLEAN(1) NOT NULL,
PRIMARY KEY (ID)
);
For both these, I get similar errors
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement " CREATE TABLE SOMETABLE ( ID INT([*]11) NOT NULL AUTO_INCREMENT, SOURCE_ID VARCHAR(255) NOT NULL, MESSAGE VARCHAR(255) NOT NULL, PRIMARY KEY (`ID`) )"; expected "ARRAY, INVISIBLE, VISIBLE, NOT, NULL, AS, DEFAULT, GENERATED, ON, NOT, NULL, AUTO_INCREMENT, DEFAULT, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY, UNIQUE, NOT, NULL, CHECK, REFERENCES, AUTO_INCREMENT, ., )";
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement " CREATE TABLE IF NOT EXISTS SOMEOTHERTABLE ( ID VARCHAR(255) NOT NULL, NAME VARCHAR(255) NOT NULL, CREATED_TIME TIMESTAMP NOT NULL, LAST_MODIFIED TIMESTAMP NOT NULL, HAS_FILE BOOLEAN([*]1) NOT NULL, PRIMARY KEY (ID) )"; expected "ARRAY, INVISIBLE, VISIBLE, NOT, NULL, AS, DEFAULT, GENERATED, ON, NOT, NULL, AUTO_INCREMENT, DEFAULT, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY, UNIQUE, NOT, NULL, CHECK, REFERENCES, AUTO_INCREMENT, ., )";
It seems that in both these cases, having INT(11) and BOOLEAN(1) is the issue. Are those not allowed anymore in the new version? If so, how should I change those? Any help regarding this is appreciated.
I was facing the same issue when updating the h2 version from 1.4.200 to 2.0.206. The project is based on Spring Boot and uses Hibernate.
In my case the problem was, that i had an entity with a field referencing a table column called "VALUE".
#Column(name = "VALUE")
private BigDecimal value;
According to the docs https://h2database.com/html/advanced.html#keywords, VALUE is a reserved keyword. Changing the column reference "VALUE" to something like "VALUE1" solved the issue.
Why do you have such definitions? Documentation of H2 1.4.200 doesn't allow any parameters for these data types.
INT(11) is allowed only in MySQL and MariaDB compatibility modes, but the specified precision is ignored by H2. This definition is rejected in all other compatibility modes in H2 2.0, you need to use INT or INTEGER.
BOOLEAN(1) is not allowed at all, if it worked in 1.4.200, it was a bug in the parser. You need to use BOOLEAN.
AUTO_INCREMENT clause also should normally be used only in MySQL and MariaDB compatibility modes, but it works in Regular mode too. The proper clause is GENERATED BY DEFAULT AS IDENTITY and explicit NOT NULL constraint isn't required for primary key and identity columns, you can remove it. Constraints also should normally be specified after all other clauses, NOT NULL before identity options is actually accepted by H2, but this wrong order of clauses isn't documented and isn't supported.
Had a similar issue with Hibernate and Spring Boot and as #Plumstone mentioned it was due to a reserved keyword (in my case constraint). Adding backticks to the name, solved the issue:
#Column(name = "`constraint`")
private String constraint;
I followed this complete beginners tutorial:
http://www.homeandlearn.co.uk/java/databases_and_java_forms.html and now have a question:
I want to have a field that only can hold a limited number of predefined values?
You can use a CHECK Constraint:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
DegreeLevel varchar(30) CHECK (DegreeLevel IN ('Msc.', 'Phd.', 'Without studies'))
);
Take a look:
https://www.w3schools.com/sql/sql_check.asp
I have 2 tables. First one holds the total values of some shopping lists and the second table holds the products in that list. When a shopping list is done the total value is added into the total table together with some informations like the list number(nrList which is some kind of list id) and the number of products on that list nrProducts while the products go into the listproducts table.Lets say there are 3 products tomato,oranges and apples.They will all share the same nrList which,as mentioned before,is something like the list id.
First table totals:
CREATE TABLE IF NOT EXISTS `totals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nrList` int(11) NOT NULL,
`nrProducts` int(11) DEFAULT NULL,
`total` double NOT NULL,
`data` date DEFAULT NULL,
`ora` time DEFAULT NULL,
`dataora` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Operator` varchar(50) DEFAULT NULL,
`anulat` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
Second table listproducts:
CREATE TABLE IF NOT EXISTS `listproducts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nrList` int(11) DEFAULT NULL,
`product` varchar(50) DEFAULT NULL,
`quantity` double DEFAULT NULL,
`price` double DEFAULT NULL,
`data` date DEFAULT NULL,
`operator` varchar(50) NOT NULL,
`anulat` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
Now,i have two things i want to do,they are very similar.
Lets say i have a list with 3 products.In the totals table there will be a row with some info and with the total=10$ nrProducts=3 and nrList=1.In the listproducts table i will have 3 rows all having nrList=1 and each having price=3$,3$,4$.
Now,i want the check the following :
1.That if the value of nrProducts=3 then i have products for that list in the other table.
2.Check if the total in the first table is equal to the sum of the products in the second table.(quantity*price SUM)
I've done some stuff but i don't know what to do next.
I managed to get the number of products for each list from the second table by using this:
SELECT nrList,operator,COUNT(*) as count FROM listproducts GROUP BY nrList
But i don't know how to compare if the values are equal without doing two queries.
For the second thing again, I know how to get the sum but i don't know how to compare them without doing two separate queries.
SELECT SUM(price*quantity) FROM `listproducts` WHERE nrList='10' and operator like '%x%'
I can also do something like what i've done in the other select,this is not the issue.
The issue is that i don't know how to do the things i want in a single select instead of doing two and comparing them.I'm doing this in java so i can compare but i'd like to know if and how i can do this in a single query.
Thanks and sorry for the long post.
You can try something like this:
SELECT totals.nrList,
IF (totals.nrProducts = t.nrProductsActual, 'yes', 'no') AS matchNrProducts,
IF (totals.total = t.totalActual, 'yes', 'no') AS matchTotal
FROM totals INNER JOIN
(SELECT nrList,
COUNT(*) AS nrProductsActual,
SUM(quantity*price) AS totalActual
FROM listproducts
GROUP BY nrList) AS t ON totals.nrList = t.nrList
The documentation says that the table of ratings should look like this:
CREATE TABLE taste_preferences (
user_id BIGINT NOT NULL,
item_id BIGINT NOT NULL,
preference REAL NOT NULL,
PRIMARY KEY (user_id, item_id)
);
However, in my implementation table of ratings is as follows:
CREATE TABLE taste_preferences (
profile_id BIGINT NOT NULL,
event_id BIGINT NOT NULL,
status_id BIGINT NOT NULL,
PRIMARY KEY (user_id, item_id)
);
Where the grade is in the form status_id (go, no go, maybe I'll go).
The users table as follows:
CREATE TABLE user (
profile_id_1 BIGINT NOT NULL,
profile_id_2 BIGINT NOT NULL,
profile_id_3 BIGINT NOT NULL,
...
);
A user can have multiple profiles, I need to compare these data to users.
I need to write its own implementation of data model? Which way do I see, that would solve this problem? Thanks!
You don't have rating data here, in any form. So, you can't use ratings in recommendations. That's fine; you just have "boolean" data.
Or, you're saying you need to use status but I'm not clear how you want to use it.
You can certainly use your taste_preferences table. Just use MySQLBooleanPrefJDBCDataModel or similar. The user table is irrelevant.
I'm trying to update the personal detail of a user through a java panel.Panel has fields like user_id(autogenerated),name,date of birth.
problem is when i enter nothing to the date of birth field in java panel and then save it. It gives me the above mentioned error.
i tried to verify it by inserting null to the date of birth(Date datatype) field directly using the mysql database.There it gives no error.
Why is it not taking null string when i insert through java panel but is taking when insert directly using mysql.
CREATE TABLE `userpersonaldetail` (
`User_Id` int(10) unsigned NOT NULL auto_increment,
`Name` varchar(45) default NULL,
`Date_Of_Birth` date default NULL,
`Address` varchar(300) default NULL,
`Phone` varchar(20) default NULL,
`Mobile` varchar(20) default NULL,
`Email` varchar(50) default NULL,
PRIMARY KEY (`User_Id`),
CONSTRAINT `FK_userpersonaldetail_1` FOREIGN KEY (`User_Id`) REFERENCES `usermaster` (`User_Id`)
)
And the portion of the code where exception occurs is:
try
{
con=(Connection) DBConnection.getConnection();
pstmt=(PreparedStatement) con.prepareStatement("Update userpersonaldetail set "+
"name=?,date_of_birth=?,address=?,phone=?,mobile=?,email=? where user_id=?");
pstmt.setInt(7,perBean.getUserId());
pstmt.setString(1,perBean.getName());
pstmt.setString(2,perBean.getDateOfBirth());
pstmt.setString(3,perBean.getAddress());
pstmt.setString(4,perBean.getPhone());
pstmt.setString(5,perBean.getMobile());
pstmt.setString(6,perBean.getEmail());
int i=pstmt.executeUpdate();
}
here perBean is the javaBean which retrieves values from the gui.In one of the test case i kept the date_of_birth text box null which is giving error while storing in DB.
My initial guess would be the field has been defined as 'NOT NULL' which means it will force you to enter a value...
If you were to do a mysql dump of the table (or view it in some tool) you'll probably find it defined such as:
`someDT` datetime NOT NULL
I replaced older version of my-sql-connector jar (to be found in lib folder of the server) with the latest. That solved my problem.
Ahh, i see now, you can't insert '' as a date. you will need to pass a date.
If an attribute does not have a value but you have it mentioned in the column list, forcing you to give something there, you need to use
statement.setNull(index, datatype)
to it. Setting to "" is not the same thing as setting to null.