Weird behavior of timestamp size in hsqldb - hsqldb

I have sample ddl script:
CREATE TABLE PERSON
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL
, FIRST_NAME VARCHAR(100) NOT NULL
, LAST_NAME VARCHAR(100) NOT NULL
, DATE_OF_BIRTH DATE
, GENDER VARCHAR(1) NOT NULL
, SSN VARCHAR(100)
, LAST_LOGIN TIMESTAMP
, VERSION INT DEFAULT 0 NOT NULL
);
When I open this database in db visualiser I see timestamp has size 26 . I really dont know why because I read default is 6. So I want to change it for example:
, LAST_LOGIN TIMESTAMP(1)
now is size 21. This is really weird. Probably last integer mean size but what is the meaning of first number "2" ?

What you call "size" is the subsecond precision of the timestamp, which is 6 by default. Your example of TIMESTAMP(1) defines a timestamp with subsecond precision of 1.
The number you see in DB Visualiser is the display size of the TIMESTAMP column.
TIMESTAMP(1), such as '2013-06-19 01:01:01.1', is always respresented using 21 characters. Timestamps defined as TIMESTAMP or TIMESTAMP(6) are displayed with five more digits at the end.

Related

Calculate ticket resolution times in a PostgreSQL ticketing system based on time stamps from notes

I have three tables in my pgsql database ticket, ticket_notes_link and notes.
I'm trying to calculate the amount of time it takes to resolve any given ticket.id by subtracting the last written in notes.create_time timestamp from the timestamp of the ticket.start_time. There is always a sign off note entered on completion of ticket but the text is not fixed so I can't use notes.notes. All timestamps are Epoch Unix Times.
Any help on how to structure this query greatly appreciated.
Table public.ticket
Column
Type
Modifiers
id
bigint
not null
Name
bigint
start_time
bigint
Table public.ticket_notes_link
Column
Type
Modifiers
id
bigint
not null default nextval('ticket_notes_link_seq'::regclass)
note_id
bigint
ticket_id
bigint
Table public.notes
Column
Type
Modifiers
id
bigint
not null default nextval('notes_seq'::regclass)
notes
character varying
username
character varying
create_time
bigint
ticket.id maps to ticket_notes_link.ticket_id and there can be multiple records.
ticket_notes_link.note_id maps to notes.id

How to create a column with the datatype float which only stores up to 3 decimals in PostgreSQL?

I want to create a table in my PostgreSQL:
CREATE TABLE my_table(
id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
description TEXT,
score FLOAT NOT NULL
);
How do I limit the number of decimals stored in the "score" column to a maximum of 3 decimals?
You would use numeric. However, you need a precision as well, which limits the maximum value:
CREATE TABLE my_table(
id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
description TEXT,
score NUMERIC(10, 3) NOT NULL
);
This will store numbers up to 9,999,999.999.

keep getting invalid identifier and incorrect datatype in apex

create table Schedule (
lms_code varchar (100),
bcode varchar (100),
start_date varchar (100),
end_date varchar (100),
timings time
);
The way I see it, you'd
use VARCHAR2 datatype (not VARCHAR). Why? Oracle says
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
start_date and end_date should be DATE, not VARCHAR2. Always store dates into DATE datatype column, never store them as strings into VARCHAR2. Why? Today is 6th of November 2020 and you'd store it into DATE datatype column as e.g. date literal: date '2020-11-06'. If it were a VARCHAR2 column, you could put 06.11.2020 into it, or 1f.scx3.f4, or Littlefoot ... all those are strings and you'll have problems if you insist on storing dates as strings.
as of the timings column, question is: what will you store in there? There's no time datatype in Oracle so you'd use date (if you're happy with times up to seconds), timestamp (if you need fractional seconds), or ... I don't know, something else - again, depending on what you'll be storing into that column
So: to make it work, you could
SQL> create table Schedule (
2 lms_code varchar2 (100),
3 bcode varchar2 (100),
4 start_date date,
5 end_date date,
6 timings date
7 );
Table created.
SQL>
you are using wrong data type , we don't have time datatype , you can use TIMESTAMP:
CREATE TABLE Schedule
(
lms_code VARCHAR(100)
, bcode VARCHAR(100)
, start_date VARCHAR(100)
, end_date VARCHAR(100)
, timings TIMESTAMP
);

How is this SQL leading to ON UPDATE CURRENT_TIMESTAMP?

Here's a snippet of my PHP that is creating the table:
$sql = 'CREATE TABLE '.$table.' (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 55 ) NOT NULL ,
`venue` VARCHAR( 55 ) NOT NULL ,
`time` TIMESTAMP NOT NULL ,
`desc` TEXT NOT NULL
)';
This is making the time column become the current timestamp when I add or change a row. How can I prevent this?
That's the default behavior of the TIMESTAMP column type. I'd recommend changing it to a DATETIME column.
You can also alter the behavior by explicitly specifying how you want it to behave.
added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
The above column specification will not have the ON UPDATE behavior. You can also specify a NULL value or 0 as the default.

mysql warnings

I have the following table:
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL AUTO_INCREMENT,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) DEFAULT NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date DEFAULT NULL,
`evt_starttime` time DEFAULT NULL,
`evt_endtime` time DEFAULT NULL,
`evt_amtpersons` int(11) DEFAULT NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) DEFAULT NULL,
`evt_amtPersonsSubs` int(11) DEFAULT NULL,
`evt_photo` varchar(50) DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT NULL,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`),
CONSTRAINT `sts_id` FOREIGN KEY (`sts_id`) REFERENCES `statusses` (`sts_id`) O
N DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
Now I have got two problems:
Here is my query:
INSERT INTO `events`(`evt_name` , `evt_description` , `evt_startdate` , `evt_enddate` , `evt_starttime` , `evt_endtime` , `evt_amtpersons` , `sts_id` , `adr_id` , `evt_amtPersonsSubs` , `evt_photo` , `sys-mut-user` , `sys-mut-id`) VALUES ('asf' , 'asf' , '2009-04-02' , '2009-04-22' , '00:00:00' , '00:00:00' , '3' , '1' , '' , '' , '' , 'test' , '1')
When I execute this query through my php programs I get no error. But when I execute the query in a shell directly on the mysql database I get two warnings. How can I get PHP to alert me when there are warnings because if there are warnings mysql doesn't do the insert.
About the warnings:
| Warning | 1366 | Incorrect integer value: '' for column 'adr_id' at row 1
| Warning | 1366 | Incorrect integer value: '' for column 'evt_amtPersonsSubs' a t row 1
How can I get rid of these warnings. Tried to make some changes but it didn't work out so far.
You are inserting an empty string. You should remove the '' and put a number in that field
As you said, the column does not have to have a value specified when you insert. The fact is indicated by the "DEFAULT NULL" for that column at table creation. This fact, however, means that if you do not specify the column name in your list of columns while doing INSERT (and therefore you will not specify the corresponding value either), then the tuple can be inserted anyway, and for that column value you will get a NULL automagically by default.
However, in your query you specify that you are going to insert that column value, and the column value you say is '' (an empty string). This is of course not valid, because that column accepts integers (or NULL, because you havent' declared the column NOT NULL), and an empty string is an empty string, not an integer.
The SQL server is generous and accepts the empty string anyway (probably it casts it to zero) but reports you a warning. If you set a strict mode for the server (something I strongly suggest you to do), you will get an error and the insert will fail.
Please note that if you follow my suggestion of setting strict mode, this is server wide, involving all your databases and all your tables (at least with the mysql released one year ago). If you have awfully written software that need a forgiving server, then you cannot use it.
The error message tells you that that the empty string ('') is not a valid value for an integer field - in this case the fields adr_id and evt_amtPersonsSubs. Did you mean to put NULL instead?
In PHP, you can retrieve the error or warning message, for the most recent query only, using the mysql_error() function.
'' is not an integer.... how about using NULL in the query if you actually want a null value?
The warnings tell you that you're trying to insert a string value into an integer column.
In all the places where you have an int column you must not put the value between ' but just put the value as is
[...]'00:00:00' , '00:00:00' , 3 , 1 , [...]
If you don't want to provide a value for a certain column you should define the column with NULL. Then you can even leave your '' for the insert.
BUT
In general it's bad practice to do inserts like that. What if you one day need to add a column to your table? Then you have to go and rewrite your code as well.
Therefore you should do inserts like that:
INSERT INTO tbl_name (col1, col2) VALUES(value1, value2);
This way your code will still work, even if you decide to add columns. Plus the code is easier to read!!
Implicit defaults are defined as follows:
For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT
attribute, the default is the next value in the sequence.
Reference:
MySQL 5.7 Reference Manual / Data Types / Data Type Default Values