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

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.

Related

Bulk Insert but the Primary Key is Null

So I know that PK's can't be null, but I've been tasked with bulk inserting data from a .txt file(s) but the PK's in the files are NULL. I'm stumped and I don't actually know how to get around this.
The table creation:
CREATE TABLE BILLING (
FolioBillingID smallint NOT NULL PRIMARY KEY,
FolioID smallint NOT NULL FOREIGN KEY REFERENCES
FOLIO(FolioID),
BillingCategoryID smallint NOT NULL FOREIGN KEY REFERENCES
BILLINGCATEGORY(BillingCategoryID),
BillingDescription char(30) NOT NULL,
BillingAmount smallmoney NOT NULL,
BillingItemQty tinyint NOT NULL,
BillingItemDate date NOT NULL)
Here's an example from the .txt file I need to insert:
|1|1|Room|99|1|5/2/2018
|1|2|Lodging Tax|11.14|1|5/2/2018
|1|1|Room|99|1|5/3/2018
And this is how I'm trying to bulk insert:
BULK INSERT BILLING FROM 'c:\stage\farms1-1\Billing.txt'
WITH (FIELDTERMINATOR='|', FIRSTROW=1)
Is there a way around this? And if so, how would I go about it?
Any help would be greatly appreciated! :)
Make FolioBillingID autoincrement

ORA-02291 parent key not found, first

My first programming program is an Oracle Database graduate certificate program, and the textbook is not Oracle friendly in some places (it is a generic database textbook). I had to rewrite the CREATE table commands and what I came up with is written below.
The tables are created in my database, and I can insert values into the vendor table; however, every time I insert values into the product table I receive the ORA-02291 integrity error.
I realize that the parent key is not being found in the vendor table, but I am at a loss as to why. I have tried a combination of column and table constraints on both tables, and nothing works. If someone could help me set up this relationship so I can practice that would be great!
CREATE TABLE VENDOR(
V_CODE INTEGER NOT NULL CONSTRAINT VENDOR_P_K PRIMARY KEY,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(25) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,
V_PHONE CHAR(8) NOT NULL,
V_STATE CHAR(2) NOT NULL,
V_ORDER CHAR(1) NOT NULL
);
----------------------------------------------------------------------------
CREATE TABLE PRODUCT(
P_CODE VARCHAR2(10) CONSTRAINT PRODUCT_P_CODE_PK PRIMARY KEY,
P_DESCRIPT VARCHAR2(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH NUMBER NOT NULL,
P_MIN NUMBER NOT NULL,
P_PRICE NUMBER(8,2) NOT NULL,
P_DISCOUNT NUMBER(5,2) NOT NULL,
V_CODE INTEGER NOT NULL,
CONSTRAINT V_CODE_FK FOREIGN KEY (V_CODE) REFERENCES VENDOR (V_CODE)
);
EDIT
INSERT INTO VENDOR 2
VALUES (21225, 'Bryson, Inc.', 'Smithson', '615','223-3234','TN','Y');
INSERT INTO VENDOR 2
VALUES (21226,'Superloo, Inc.','Flushing','904','215-8995','FL','N');
INSERT INTO PRODUCT 2
VALUES ('11QER/31','Power painter, 15 psi., 3-nozzle','03-Nov-13',8,5,109.99,0.00,25595);
The relationship is set up correctly.
You can only insert foreign key values matching a primary key value in the master table. In your example, you are inserting a product with V_CODE = 25595 but you are never inserting a vendor with this V_CODE.
Maybe you intended to insert the vendor later. This does not work, as the database is enforcing the constraints for every command. Therefore, insert the vendor first and append his products later.
If you want to delete vendors, first delete its products, then delete the vendor unless you are using Foreign Keys with Cascade Delete.

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

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
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.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);

SQL server 2012 tables issue, sysdate and to_date issue

I have two issues while trying to create tables.
For **sysdate*** it says invalid column name
for *TO_DATE('01-JAN-2008','DD-MON-YYYY')));* it says TO_DATE is not a reconigized built-in function name.
^ both are in the Table Invoice.
This is using SQL SERVER 2012
CREATE TABLE VENDOR(
V_CODE INTEGER NOT NULL UNIQUE,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(15) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,
V_PHONE CHAR(8) NOT NULL,
V_STATE CHAR(2) NOT NULL,
v_ORDER CHAR(1) NOT NULL,
PRIMARY KEY (V_CODE));
CREATE TABLE PRODUCT(
P_CODE VARCHAR(10) NOT NULL,
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 INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY(V_CODE) REFERENCES VENDOR ON UPDATE CASCADE);
CREATE TABLE CUSTOMER(
CUS_CODE DECIMAL PRIMARY KEY,
CUS_LNAME VARCHAR(15) NOT NULL,
CUS_FNAME VARCHAR(15) NOT NULL,
CUS_INITIAL CHAR(1),
CUS_AREACODE CHAR(3) DEFAULT '615' NOT NULL,
CHECK(CUS_AREACODE IN ('615','713','931')),
CUS_PHONE CHAR(8) NOT NULL,
CUS_BALANCE DECIMAL(9,2) DEFAULT 0.00,
CONSTRAINT CUS_UI1 UNIQUE (CUS_LNAME, CUS_FNAME));
CREATE TABLE INVOICE (
INV_NUMBER DECIMAL PRIMARY KEY,
CUS_CODE DECIMAL NOT NULL REFERENCES CUSTOMER(CUS_CODE),
INV_DATE DATE DEFAULT SYSDATE NOT NULL,
CONSTRAINT INV_CK1 CHECK (INV_DATE > TO_DATE('01-JAN-2008','DD-MON-YYYY')));
You have two issues:
the function to get the current system date and time is called SYSDATETIME() in T-SQL/SQL Server (not sysdate)
the way to convert a string to a date or datetime in T-SQL/SQL Server is using CAST or CONVERT (not TO_DATE - there is no such function in T-SQL)
Use something like
SELECT CAST('01-JAN-2008' AS DATE)
or something like that (it's highly dependent on your language/date format settings in SQL Server whether it'll work or not). If you need to specify a specific format, you can use CONVERT which allows you to use one of the many predefined formats (see relevant details in the MSDN documentation).
If that's still not enough - SQL Server 2012 has a new function called PARSE which allows you to specify any arbitrary date format that your string is formatted in. Again, see the relevant MSDN documentation for details.
The best thing is to avoid converting dates back and forth to and from strings if ever possible, and in your case, this should be easily doable! Just use:
INV_DATE DATE DEFAULT SYSDATETIME() NOT NULL,
CONSTRAINT INV_CK1 CHECK (INV_DATE > '20080101');