Create Domain Error - sql

I am trying to define a new domain "rating_int" that I would be able to use in the table "Review". Anytime I try to execute this query, it returns this message:
ERROR: column "rating_int" does not exist
********** Error **********
ERROR: column "rating_int" does not exist
SQL state: 42703
I followed the exact same code format as the postgresql example and I do not know why my code is returning an error.
CREATE DOMAIN rating_int AS INTEGER
CHECK(rating_int > 0 AND rating_int <=10);
CREATE TABLE Review
(
paper_id INTEGER,
rv_email VARCHAR(55) NOT NULL,
reccomendation TEXT,
a_comments TEXT,
c_comments TEXT,
technical_merit rating_int,
readability rating_int,
orginality rating_int,
relevance rating_int,
PRIMARY KEY(paper_id, rv_email),
FOREIGN KEY(paper_id) REFERENCES Paper
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY(rv_email) REFERENCES Reviewer
ON DELETE SET NULL ON UPDATE CASCADE
);

You use value, not the name of the domain. Like this:
CREATE DOMAIN rating_int AS INTEGER
CONSTRAINT chk_rating_int CHECK (value > 0 AND value <= 10);
You don't actually need to give the constraint a name -- your error was rating_int instead of value. So this works:
CREATE DOMAIN rating_int AS INTEGER
CHECK (value > 0 AND value <= 10);
However, I think it is a good idea to give constraints names.
Also, you might want to make the type a smallint or decimal(2), if you want to save on space.

Related

How to name NOT NULL constraint in CREATE TABLE query

For a test tomorrow we're told to name our constraints
I know it's possible to create a constraint when you use ALTER TABLE
but can you add a name to a not null constraint when you CREATE TABLE?
f.e.
CREATE TABLE test (
test1 VARCHAR
CONSTRAINT nn_test1 NOT NULL (test1)
)
I get an error when trying to run this query. Am I writing it wrong?
The error I get is
ERROR: syntax error at or near "NOT"
LINE 3: CONSTRAINT nn_test1 NOT NULL (test1))
^
SQL state: 42601
Character: 56
You have two options to define a named not null constraint:
Inline with the column:
CREATE TABLE test
(
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL,
test2 integer --<< no comma because it's the last column
);
Or at the end of columns as an out-of-line constraint. But then you need a check constraint:
CREATE TABLE test
(
test1 VARCHAR,
test2 integer, --<< comma required after the last column
constraint nn_test1 check (test1 is not null)
);
This has become irrelevant, since you're not using SQL Server
First of all, you should always specify a length for a VARCHAR. Not doing so (in SQL Server variables, or parameters) may result in a string of just exactly ONE character in length - typically NOT what you want.
Then, you need to just specify the NOT NULL - there's no need to repeat the column name (actually this is the error) - if you're specifying the CONSTRAINT "inline" with the column definition (which is a perfectly legal and in my opinion the preferred way of doing this).
Try this code:
CREATE TABLE test
(
test1 VARCHAR(50)
CONSTRAINT nn_test1 NOT NULL
)
At least this is the CREATE TABLE statement that works in T-SQL / SQL Server - not sure about PostgreSQL (don't know it well enough, don't have it at hand to test right now).
I, a_horse_with_no_name, the two syntax:
constraint nn_test1 check (test1 is not null)
and
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL
are equivalent ? performance correctly ecc.
Because in first case the SQL server exception return the name nn_test so the system know exactly error.

Create table with attribute which must be in range of 1-10 (Postgres)

How can i constrain an attribute in a table to only allow the value to be between 1-10?
This is the statement so far.. Have no idea how to make the OfficeNumber only accept values in that interval
CREATE TABLE OfficeStaff(
EID INT PRIMARY KEY,
OfficeNumber INT NOT NULL
);
Use a check constraint:
CREATE TABLE OfficeStaff (
EID INT PRIMARY KEY,
OfficeNumber INT NOT NULL,
CHECK (OfficeNumber BETWEEN 1 AND 10)
);
Note, though, that there is another, perhaps better approach. You should have an OfficeNumbers table with the valid office numbers. Then you can use a foreign key relationship to enforce the numbering, without having to hard-code the numbers.
You can use domains for this purpose:
create domain mydomain as integer check(value between 1 and 10)
create table mytable(id serial primary key, md mydomain not null)
-- this two will succeed
insert into mytable(md) values(1)
insert into mytable(md) values(2)
-- that one will fail
insert into mytable(md) values(12)
ERROR: value for domain mydomain violates check constraint "mydomain_check"
********** Error **********
ERROR: value for domain mydomain violates check constraint "mydomain_check"
More information can be found here: http://www.postgresql.org/docs/9.1/static/sql-createdomain.html
You can add constraints for the definition. In this case you can add
CHECK (OfficeNumber BETWEEN 1 AND 10)

update postgresql with syntax errors

I have one table called test, which has 4 columns:
id INT
v_out INT
v_in INT
label CHARACTER
I'm trying to update the table with the following query:
String sql = "
update
test
set
v_out = temp.outV
, v_in = temp.inV
, label = temp.label
from (
values
(1,234,235,'[abc]') // these value are read from other places
,(2,234,5585,'[def]') //[abc] = object.toString();
) as temp (e_id, outV, inV, label)
where
id = temp.e_id;
When I execute it, I got the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "["
before get this error, i already update over 3000 rows.
so whats the reason caused this? is it because "[" this character?
this is the original table:
create table edges(
id serial not null primary key,
vertex_out int,
vertex_in int,
label character varying(255),
constraint fk_vertex_out foreign key (vertex_out) references vertices(id) on delete cascade,
constraint fk_vertex_in foreign key (vertex_in) references vertices(id) on delete cascade
);
The most likely problem here is string interpolation in SQL query (it is a very bad practice).
Look at this example:
values
(1,234,235,'[abc]''),
(2,234,5585,'[def]')
The ' symbol in the name of the first object breaches the string boundaries causing ERROR: syntax error at or near "[": on the second line.
You can search for SQL Injection in the internet to get details about this problem.

sql code for firebird doesn't compile

I am getting strange errors while trying to create a simple database using isql tools from the Firebird package.
The same code for creating a table works for other tables with other names.
I've tried with and without quotes surrounding fields and table names, no success.
It is Firebird 2.5 server version.
The code I'm trying to execute:
SET SQL DIALECT 3;
SET NAMES UTF8;
CREATE DATABASE 'localhost:C:\fuzzdb.fdb' user 'SYSDBA' password 'masterkey'
DEFAULT CHARACTER SET UTF8;
CREATE TABLE RULES (
RULE_ID INTEGER NOT NULL,
IF_FUZZY SMALLINT,
CONSTRAINT PK_RULE_ID
PRIMARY KEY (RULE_ID),
);
CREATE TABLE VARS (
VAR_ID INTEGER NOT NULL,
VRULE_ID INTEGER,
INPOUTP SMALLINT,
RANGE_STRT INTEGER,
RANGE_END INTEGER,
VAR_NAME VARCHAR(40),
FUZ_SET INTEGER,
CONSTRAINT PK_VAR_ID
PRIMARY KEY (VAR_ID)
);
CREATE TABLE FUZZSETS (
FS_ID INTEGER NOT NULL,
FS_NAME VARCHAR(40),
INPOUTP SMALLINT,
PAR1 FLOAT,
PAR2 FLOAT,
PAR3 FLOAT,
PAR4 FLOAT,
PAR5_HEDGE INTEGER,
FUZ_SET INTEGER,
CONSTRAINT PK_FS_ID
PRIMARY KEY (FS_ID)
);
CREATE TABLE FRULES (
FRULE_ID INTEGER NOT NULL,
RULE_ID INTEGER,
VAR_ID INTEGER,
FS_ID INTEGER,
INPOUTP SMALLINT,
CONSTRAINT PK_FRULE_ID
PRIMARY KEY (FRULE_ID)
);
CREATE TABLE LINK_RV (
LINK_RULES INTEGER,
LINK_VARS INTEGER,
CONSTRAINT FK_LINK_RV
PRIMARY KEY (LINK_RULES, LINK_VARS)
);
CREATE TABLE LINK_VARFS (
LINK_VRS INTEGER,
LINK_FS INTEGER,
CONSTRAINT FK_LINK_VARFS
PRIMARY KEY (LINK_VRS, LINK_FS)
);
CREATE TABLE LINK_RLVR (
LINK_RULE INTEGER NOT NULL,
LINK_VR INTEGER NOT NULL,
CONSTRAINT FK_LINK_RLVR
PRIMARY KEY (LINK_RULE, LINK_VR)
);
CREATE TABLE LINK_FRL_RL (
LINK_FRULE INTEGER NOT NULL,
LINK_RULE INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_RL
PRIMARY KEY (LINK_FRULE, LINK_RULE)
);
CREATE TABLE LINK_FRL_VAR (
LINK_FRULE INTEGER NOT NULL,
LINK_VAR INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_VAR
PRIMARY KEY (LINK_FRULE, LINK_VAR)
);
CREATE TABLE LINK_FRL_FS (
LINK_FSRULE INTEGER NOT NULL,
LINK_FS INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_FS
PRIMARY KEY (LINK_FRULE, LINK_FS)
);
ALTER TABLE LINK_FRL_FS
ADD CONSTRAINT FK_LINK_FSRULE
FOREIGN KEY(LINK_FSRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_FS
ADD CONSTRAINT FK_LINK_FS
FOREIGN KEY(LINK_FS)
REFERENCES FUZZSETS(FS_ID);
ALTER TABLE LINK_FRL_VAR
ADD CONSTRAINT FK_LINK_FRULE
FOREIGN KEY(LINK_FRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_VAR
ADD CONSTRAINT FK_LINK_VAR
FOREIGN KEY(LINK_VAR)
REFERENCES FUZZSETS(VAR_ID);
ALTER TABLE LINK_FRL_RL
ADD CONSTRAINT FK_LINK_FRULE
FOREIGN KEY(LINK_FRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_RL
ADD CONSTRAINT FK_LINK_RULE
FOREIGN KEY(LINK_RULE)
REFERENCES RULES(RULE_ID);
CREATE GENERATOR GEN_RULE_ID;
CREATE GENERATOR GEN_VAR_ID;
CREATE GENERATOR GEN_FS_ID;
CREATE GENERATOR GEN_FRULE_ID;
SET TERM ^ ;
CREATE TRIGGER BI_RULES FOR RULES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.RULE_ID IS NULL) THEN
NEW.RULE_ID = GEN_ID(GEN_RULE_ID, 1);
END^
CREATE TRIGGER BI_VARS FOR VARS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.VAR_ID IS NULL) THEN
NEW.VAR_ID = GEN_ID(GEN_VAR_ID, 1);
END^
CREATE TRIGGER BI_FUZZSETS FOR FUZZSETS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.FS_ID IS NULL) THEN
NEW.FS_ID = GEN_ID(GEN_FS_ID, 1);
END^
CREATE TRIGGER BI_FRULES FOR FRULES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.FRULE_ID IS NULL) THEN
NEW.FRULE_ID = GEN_ID(GEN_FRULE_ID, 1);
END^
SET TERM ; ^
COMMIT;
The output from the isql commmand:
Use CONNECT or CREATE DATABASE to specify a database
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 6, column 3
-)
At line 10 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Unknown columns in index FK_LINK_FRL_FS
After line 82 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-LINK_FRL_FS
-At line 1, column 13.
After line 89 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-LINK_FRL_FS
-At line 1, column 13.
After line 94 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-could not find UNIQUE or PRIMARY KEY constraint in table FUZZSETS with specifie
d columns
After line 104 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S11
unsuccessful metadata update
-Index FK_LINK_FRULE already exists
After line 109 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Table RULES not found
After line 114 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-RULES
-At line 1, column 29
At line 130 in file c:\fdb.sql
I don't get why it's impossible to create the first table "RULES" although the commands are similar to other tables.
Even without all the triggers and foreign keys (alter table..) I am getting at leaast the last error.
it says "Unknown columns in index FK_LINK_FRL_FS" but no mention of other similar indexing tables.
I am just starting working with databases and it could be that I missed or mixed something,
but I tried to compile with too many changes and still getting errors.
I've found more or less similar code here
http://sergworks.wordpress.com/category/firebird/
and I was able to compile it without problems.
Could somebody point me in the right direction or show how it is done in another way?
You have an unnessesary comma in the end of the PK constraint:
CONSTRAINT PK_RULE_ID
PRIMARY KEY (RULE_ID),
So the parser expexts definition of field or constraint but it finds ")". Delete the comma and you should be OK.

Replace into equivalent for postgresql and then autoincrementing an int

Okay no seriously, if a PostgreSQL guru can help out I'm just getting started.
Basically what I want is a simple table like such:
CREATE TABLE schema.searches
(
search_id serial NOT NULL,
search_query character varying(255),
search_count integer DEFAULT 1,
CONSTRAINT pkey_search_id PRIMARY KEY (search_id)
)
WITH (
OIDS=FALSE
);
I need something like REPLACE INTO for MySQL. I don't know if I have to write my own procedure or something?
Basically:
check if the query already exists
if so, just add 1 to the count
it not, add it to the db
I can do this in my php code but I'd rather all that be done in postgres C engine
You have to add a unique constraint first.
ALTER TABLE schema.searches ADD UNIQUE (search_query);
The insert/replace command looks like this.
INSERT INTO schema.searches(search_query) VALUES ('a search query')
ON CONFLICT (search_query)
DO UPDATE SET search_count = schema.searches.search_count + 1;