Length or precision specification 0 is invalid - sql

I have an MFC app which run some SQL statements, and I have run them on several PCs, without any problem.
But, I have met an PC, where when I have tried to run these SQLs, I have encountered an strange error (strange for me, I know SQL a little):
Length or precision specification 0 is invalid
This error fired on the following SQL:
CREATE TABLE mytable (
nmytable_ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
nAsset_ID int FOREIGN KEY REFERENCES t_asset (nAsset_ID) DEFAULT NULL,
dtInit date NOT NULL,
dRest decimal(10, 2) NULL,
nOdo int NULL,
sObs varchar(1024) NULL,
dtDateC datetime2(0) NULL,
dtDateU datetime2(0) NULL,
nUserC int FOREIGN KEY REFERENCES ntp_appuserx (nAppUserx_ID) DEFAULT NULL,
nUserU int FOREIGN KEY REFERENCES ntp_appuserx (nAppUserx_ID) DEFAULT NULL,
CONSTRAINT nAsset_ID_mytable UNIQUE (nAsset_ID, dtInit)
)
Ok, I have changed DATETIME2(0) data type to DATETIME, but then I get the following error:
DATE data type is unknown
There is a question: why this error(s), but on an PC, and not for others ? Is there some local settings which affect my app ? I am using SQL Server 2012.
Solved
I have tried to create the table on older DB installed on this PC, without to notice that.

Related

Syntax error in CREATE TABLE statement - SQL (MS Access 2016)

I took this example from my class textbook and pretty much copied word for word from the text. The following is the code that I wrote into MS Access:
CREATE TABLE PRODUCT(
P_CODE VARCHAR(10) NOT NULL UNIQUE,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE NUMBER(8, 2) NOT NULL,
P_DISCOUNT NUMBER(5, 2) NOT NULL,
V_CODE INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY (V_CODE) REFERENCES VENDOR ON UPDATE CASCADE
);
This code produces a syntax error every time it's run within MS Access.
I tried running this query to create the table PRODUCT within my db. When reviewing the code, I couldn't find anything specific that would have caused the error, but I could be wrong due to my inexperience with SQL.
Any help would be greatly appreciated!
Per the comments the problem is the Decimal type. I'm updating the answer here for Access 2016: How do I create a decimal field in Access with Alter Table?
First enable the SQL Ansi 92 standard. In 2016 this has moved to File-Options-Object Designers-Query Design. It seems you can only enable it for new databases. So do so and create a new database then open the sql-tab of a blank query and paste the following code:
'make sure you have a VENDOR table first for instance:
CREATE TABLE VENDOR
(V_CODE AutoIncrement CONSTRAINT PrimaryKEY PRIMARY KEY);
'Then with slightly less old syntax (varchar would usually be text, smallint would usually be integer with a size, and PrimaryKey includes Not null and unique)
CREATE TABLE PRODUCTS(
P_CODE VARCHAR(10) CONSTRAINT PrimaryKey PRIMARY KEY,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE DECIMAL(8, 2) NOT NULL,
P_DISCOUNT DECIMAL(5, 2) NOT NULL,
V_CODE LONG REFERENCES VENDOR(V_CODE)
);
'Alter Table also works now
ALTER TABLE PRODUCTS ADD COLUMN P_PRICE DECIMAL(8,2) NOT NULL;
Caveat
At least the sql-pane tells you that something in your DDL is wrong if not what. if you are not using the default Jet database as your backend then you are stuck concatenating strings using vba and ADO according to here: https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/create-table-statement-microsoft-access-sql
At that point I would manually try manually adding the decimal columns if there where not too many.

Error 1064 from trying to forward engineer an ERD

I'm still learning SQL at the moment and i keep getting this error when i try to forward engineer my tables. Could really use some assistance on figuring out what's causing the error.
Executing SQL script in server
ERROR: Error 1064: 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 'VISIBLE,
CONSTRAINT fk_Player_Team1
FOREIGN KEY (team_id)
REFERENC' at line 11
SQL Code:
CREATE TABLE IF NOT EXISTS `sts18`.`Player` (
`player_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`team_id` INT UNSIGNED NOT NULL,
`player_lname` VARCHAR(45) NOT NULL,
`player_fname` VARCHAR(45) NOT NULL,
`player_age` TINYINT UNSIGNED NOT NULL,
`player_phone` BIGINT UNSIGNED NOT NULL,
`player_email` VARCHAR(100) NULL,
`player_notes` VARCHAR(255) NULL,
PRIMARY KEY (`player_id`),
INDEX `fk_Player_Team1_idx` (`team_id` ASC) VISIBLE,
CONSTRAINT `fk_Player_Team1`
FOREIGN KEY (`team_id`)
REFERENCES `sts18`.`Team` (`team_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 17 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch

Oracle SQL Developer - renaming attributes causes errors

I'm having a weird situation going on in Oracle SQL Developer. I had to rename some attributes to match my school's variable names standards and when I am executing the program, the new attribute names doesn't seem to have been taken into account.
For example, I'm creating a table named VARIANT like this :
create table VARIANT(
ID_VAR varchar(9) not null,
NUCL_VAR char(1) not null,
NUCL_REF_VAR char(1) not null,
CHROMOSOME_VAR number(2) not null,
GENE_VAR varchar(5) not null,
POSITION_VAR number(4) not null,
EFFET_VAR varchar(100) not null,
INDEX_EFF_VAR number(2, 2) not null,
URL_DRUGBANK_VAR varchar(500) not null,
constraint PK_VARIANT primary key(ID_VAR),
constraint FK_URL_DRUGBANK_VAR foreign key(URL_DRUGBANK_VAR)
references DRUG(URL_DRUGBANK_DRO),
constraint DOM_INDEX_EFF_VAR check(INDEX_EFF_VAR > 0)
);
My problem occurs when I execute the table creation request. The output in SQLDev's console doesn't match my code :
create table VARIANT(
ID_VAR varchar(9) not null,
NUCL_VAR char(1) not null,
NUCL_REF_VAR char(1) not null,
CHROMOSOME_VAR number(2) not null,
GENE_VAR varchar(5) not null,
POSITION_VAR number(4) not null,
EFFET_VAR varchar(100) not null,
INDEX_EFF_VAR number(2, 2) not null,
URL_DRUGBANK_VAR varchar(500) not null,
constraint PK_VARIANT primary key(ID_VAR),
constraint FK_URL_DRUGBANK_VAR foreign key(URL_DRUGBANK_VAR)
references DROGUE(URL_DRUGBANK_DRO),
constraint DOM_INDEX_EFF check(INDEX_EFF > 0)
)
Error report -
00904. 00000 - "INDEX_EFF: invalid identifier"
*Cause:
*Action:
As you can see, in my code, the attribute INDEX_EFF is renamed INDEX_EFF_VAR in the constraint below but the change does not seem to have taken effect in the console's output for the constraint, even though the attribute name has been changed properly.
If someone could help me figure out what is going on here, that would be appreciated! Thanks!
EDIT:
Found my way out of it. Seems that rebooting SQL Developer fixed the issue. But still, I'd like to know if there is another way of fixing this.
As we can clearly see in the output, there is no such column name "INDEX_EFF". There might be some kind of user error here, you may try rerunning the SQL after clearing the console output to avoid any confusions.
Please note, you may try the options like drop and recreate table. If you don't want to loose the existing data, then follow these steps
Drop the constraints
Rename the column
then finally add the constraint
Hope this helps you, though i can't say what actually gone wrong.
Primary key name is also wrong, check the key names.
INDEX_EFF should be INDEX_EFF_VAR

Executing SQL script in server ERROR: Error 1215: Cannot add foreign key constraint

I've checked other concerns like this. Checked if there were duplicates-none, if same data type -yep. relationships- yep. Not using "On Delete Set Null" checked.
I'm still lost how this error came to be
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
-- Table Apo.PurchasingOrder
CREATE TABLE IF NOT EXISTS Apo.PurchasingOrder (
PurchaseNo. INT NOT NULL AUTO_INCREMENT,
ModelNo. INT NULL,
UnitPrice INT NULL,
Product/Name VARCHAR(45) NULL,
Quantity INT NULL,
Source VARCHAR(45) NULL,
PRIMARY KEY (PurchaseNo.),
INDEX Source (Source ASC),
CONSTRAINT Supplier_Source
FOREIGN KEY (`PurchaseNo.`)
REFERENCES `Apo`.`Supplier` (`SupplierID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Could you check whether all your PurchaseNo values in Apo.PurchasingOrder are valid values of SupplierID in Apo.Supplier?

Error with auto_increment while conneted to Postgres via psql and puTTY

I'm getting this error in puTTY. Not sure why, looks right to me ...
psql:pierre.sql:10: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
^
psql:pierre.sql:18: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
--DROP TABLE customer, reservation;
CREATE TABLE customer(
c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_ref VARCHAR(30) NOT NULL,
f_name VARCHAR(30) NOT NULL,
l_name VARCHAR(30) NOT NULL,
address VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(11) NOT NULL
);
CREATE TABLE reservation(
r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_id VARCHAR(30) NOT NULL REFERENCES customer(c_id),
book_date DATE NOT NULL CHECK (book_date <= now()),
s_time DOUBLE NOT NULL,
e_time DOUBLE NOT NULL,
amount INTEGER NOT NULL
);
Any ideas why?
auto_increment looks like something you'd use with MySQL.
But, here, it seems you are using PostgreSQL.
According to the datatype serial section of the manual, postgresql's equivalent of auto_increment is serial or bigserial.
Quoting that page :
The data types serial and bigserial are not true types, but merely
a notational convenience for setting up unique identifier columns
(similar to the AUTO_INCREMENT property supported by some other databases).
In Postgres 10 or later consider an IDENTITY column:
CREATE TABLE customer(
c_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
...
(PRIMARY KEY also makes it NOT NULL automatically.)
Details:
Auto increment table column
In Postgres 9.6 or older consider a serial like Pascal already suggested.
Works in pg 10 or later, too, but IDENTITY is generally superior.