How can I create a table that includes auto increment and NOT NULL. I am using the sql database console that bluemix offers.
The following query gives me an error.
CREATE TABLE discounts (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
expired_date DATE NOT NULL,
amount DECIMAL(10,2) NULL,
PRIMARY KEY (id)
);
The error is:
DDL failed with message
_ Exception. _ state = 42601; error code = -104; error Message = Error for batch element #1: An unexpected token "," was found following
"NULL AUTO_INCREMENT". Expected tokens may include: "".. _CODE=-104,
_STATE=42601, DRIVER=3.66.46
DB2 does not support the "AUTO_INCREMENT" statement.
You can use the "GENERATED ALWAYS AS IDENTITY" command instead.
CREATE TABLE discounts ( id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), title VARCHAR(255) NOT NULL, expired_date DATE NOT NULL, amount DECIMAL(10,2) NULL, PRIMARY KEY (id) );
Further details about creating automatic values are detailed here
Related
I'm trying to import a csv into PostgreSQL database table.When i executing the following query:
My table name is trendmania_video
COPY public.trendmania_video FROM 'C:\Users\Shahnawaz Irfan\Desktop\0.csv' DELIMITER ',' CSV HEADER;
a following error occurred:
ERROR: null value in column "v_id" violates not-null constraint
DETAIL: Failing row contains (null, null, UgzYr_WZlR73yFBnRdx4AaABAg, yar
kindly ap urdu m b toturial bna lety wordpress k liye to hma..., null, null,
null, null, null, null, null, null, null, null).
CONTEXT: COPY trendmania_video, line 10: ",,UgzYr_WZlR73yFBnRdx4AaABAg,yar
kindly ap urdu m b toturial bna lety wordpress k liye to hmari b he..."
SQL state: 23502
I also tried manually by using import button, but same error occurs.
In your table trendmania_video, you have v_id to be not null which causes this issue. You one option is to get ride of the not null constrain:
ALTER TABLE public.trendmania_video ALTER COLUMN v_id DROP NOT NULL;
If this is a new table then it's better to recreate it with a new table with an auto-cremented id while v_id is another value.
CREATE TABLE trendmania_video(
id SERIAL PRIMARY KEY,
v_id VARCHAR
--the rest of the columns
);
I am using Postgres 11 and SymmetricDS 3.9.14.
I have a database table with primary key of UUID type. It seems like SymmetricDS is not able to cast 'UUID' correctly. One of SymmetricDS replication query is failing with the below error
JdbcSqlTemplate - SQL caused exception: [select "id" from "dbo"."groups" where "id"=?]
sql args: [ ]
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type uuid: " "
my insert statement :-
INSERT INTO dbo.groups(
id, sortorder, name, hidden, sessionid, creationtime, modificationtime, regionid)
VALUES ('5A171D3F-F6A6-4D09-AE89-73B5793DA171', 1, 'abc', false, null,'2018-11-20 20:25:49.663', null, null);
my database table is :-
CREATE TABLE dbo.groups
(
id uuid NOT NULL,
sortorder integer NOT NULL DEFAULT 0,
name character varying(80) COLLATE pg_catalog."default",
hidden boolean NOT NULL DEFAULT false,
sessionid uuid,
creationtime timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
modificationtime timestamp without time zone,
regionid uuid,
CONSTRAINT "PK_dbo.Groups" PRIMARY KEY (id)
)
EDIT:
My source database is MS SQL Server and target database is Postgres
The value of UUID in this case is a space or tab as it could be seen from the error message
invalid input syntax for type uuid: " "
Try finding why instead of the concrete UUID value this space/tab value is passed
I'm populating a database in PostgreSQL for a Newspaper Online. Now my doubt lies on how to insert a value into a table which only attribute is both a primary and a foreign key.
In this context, the admin is the first person to ever register an account. So idAdmin = idA = 1:
CREATE TABLE AUTENTICADO (
idA serial NOT NULL ,
login VARCHAR(45) NOT NULL,
password VARCHAR(45) NOT NULL,
email VARCHAR(40) NOT NULL,
PRIMARY KEY (idA) );
CREATE TABLE ADMIN (
idAdmin INT NOT NULL REFERENCES AUTENTICADO (idA),
PRIMARY KEY (idAdmin) );
It would be logical to insert values into 'ADMIN' as I tried below, although it is obviously not possible considering 'idAdmin' is a primary key (and a foreign key).
INSERT INTO AUTENTICADO VALUES ('john','adadfsfsdfs', 'john#random.com')
INSERT INTO ADMIN VALUES (1)
Is there a way to register that the first user to create an account (idA = 1) is the admin (idAdmin = idA = 1) ?
although it is obviously not possible considering 'idAdmin' is a
primary key (and a foreign key).
So what?
If you fix the first query to list the columns and use a returning clause to get the auto-generated value for the SERIAL ID, it just works:
INSERT INTO AUTENTICADO(login,password,email)
VALUES ('john','adadfsfsdfs', 'john#random.com')
returning idA;
Result:
ida
-----
1
(1 row)
Second query:
insert into admin values(1);
select * from admin;
Result:
idadmin
---------
1
Hy I have problem. I wanna to create table with some atributes, and some of them shoud be specified as NOT NULL.. And here comes the problem. When I insert some data into table, and when I insert '' (empty single string) it input data into table, but I dont want this... How to restrict inserting data from inputing single string or inputing nothing..
here are my table
CREATE TABLE tbl_Film
(
ID INT UNSIGNED PRIMARY KEY,
Naziv VARCHAR(50) NOT NULL,
Zanr VARCHAR(50) NOT NULL,
Opis VARCHAR(150) NULL,
Kolicina INT UNSIGNED NOT NULL
);
INSERT INTO tbl_Film VALUES (1,'','Animirani','Mala ribica',2)
This input blank data into Naziv, and I don't want that.. I need to restrict that..
http://prntscr.com/21gfgd
I dont know if this is possible in SQL, but why dont you exchange the '' in your application into the String NULL?
In SQL, NULL is not the same as '' (with the exception of MS SQL via OleDB AFAIR, in which '' should be stored as NULL).
NULL values represent missing unknown data.
See http://www.w3schools.com/sql/sql_null_values.asp
In regular SQL, you should use a CHECK constraint, e.g.
CREATE TABLE tbl_Film (
ID INT UNSIGNED PRIMARY KEY,
Naziv VARCHAR(50) NOT NULL,
Zanr VARCHAR(50) NOT NULL,
Opis VARCHAR(150) NULL,
Kolicina INT UNSIGNED NOT NULL,
CHECK (Naziv <> '')
);
Sadly, this CHECK constraint is NOT implemented by MySQL:
The CHECK clause is parsed but ignored by all storage engines.
See http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
So the only solution I see, at DB level, is to write a P/SQL trigger...
I am developing a CakePHP app, and I would like to use UUID as a primary keys, since the application will be distributed accross multiple databases and I would also like to take advantage of the integrated ACL framework in CakePHP 2.1
I am going according to the tutorial and I have modified DB scheme to following
CREATE TABLE acos (
id uuid NOT NULL,
parent_id uuid DEFAULT NULL,
model VARCHAR(255) DEFAULT '',
foreign_key uuid DEFAULT NULL,
alias VARCHAR(255) DEFAULT '',
lft uuid DEFAULT NULL,
rght uuid DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE aros_acos (
id uuid NOT NULL,
aro_id uuid NOT NULL,
aco_id uuid NOT NULL,
_create CHAR(2) NOT NULL DEFAULT 0,
_read CHAR(2) NOT NULL DEFAULT 0,
_update CHAR(2) NOT NULL DEFAULT 0,
_delete CHAR(2) NOT NULL DEFAULT 0,
PRIMARY KEY(id)
);
CREATE TABLE aros (
id uuid NOT NULL,
parent_id uuid DEFAULT NULL,
model VARCHAR(255) DEFAULT '',
foreign_key uuid DEFAULT NULL,
alias VARCHAR(255) DEFAULT '',
lft uuid DEFAULT NULL,
rght uuid DEFAULT NULL,
PRIMARY KEY (id)
);
However now I am getting an error:
Error: SQLSTATE[42883]: Undefined function: 7 ERROR: function max(uuid) does not exist LINE 1: SELECT MAX("Aro"."rght") AS "rght" FROM "public"."aros" AS "... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
The version of CakePHP is 2.1.0-beta and I'm using PostgreSQL with UUID data type.
Have anyone succesfully used CakePHP ACL framework with UUID's? I would like to get this working with minimal modification in CakePHP framework, for future supportability of this app.
There is no aggregate function max() defined for the data type UUID. No UUID is considered "bigger" than another UUID.
Consider the following demo:
CREATE TEMP TABLE t(id uuid);
INSERT INTO t VALUES
('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11')
,('b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11');
SELECT max(id) FROM t;
Yields:
ERROR: function max(uuid) does not exist
LINE 1: SELECT max(id) FROM t;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
You can circumvent the problem. Cast the id to text if you want the alphabetically biggest value:
SELECT max(id::text) FROM t;
Yields:
b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
But be aware that that is just the standard text representation of a UUID. The same UUID could be represented in many other forms.