I am getting this error message for the below code, however, can not see where the error is referring to. I thought that parenthesis referred to semicolons and commas. I assume there is something I am missing, any direction would be greatly appreciated.
CREATE TABLE BRANCH(
BranchID Int NOT NULL,
Street Char(50) NOT NULL,
City Char(30) NOT NULL,
Postcode Char(10) NOT NULL,
TelephoneNumber Int(11) NOT NULL,
ManagerName VarChar(40) NOT NULL,
CONSTRAINT BRANCH_PK PRIMARY KEY(BranchID)
);
Try this:
CREATE TABLE BRANCH(
BranchID Int NOT NULL,
Street Char(50) NOT NULL,
City Char(30) NOT NULL,
Postcode Char(10) NOT NULL,
TelephoneNumber Int NOT NULL,
ManagerName VarChar(40) NOT NULL,
CONSTRAINT BRANCH_PK PRIMARY KEY(BranchID)
);
Related
My problem: I am trying to create a table named User_T in Oracle apex.
I can't get rid of this displaying error... I don't know why. I've already checked my codes a couple of times.
CREATE TABLE User_T(
IdUser NUMBER(10,0) NOT NULL,
FirstName VARCHAR2(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(50) NOT NULL,
UserName VARCHAR(30) NOT NULL UNIQUE,
UserPassword VARCHAR2(30) NOT NULL,
CreatedAt VARCHAR2(30),
UpdatedAt VARCHAR(30),
Enabled CHAR(10) CHECK,
CONSTRAINT IdUser_PK PRIMARY KEY(IdUser)
);
You have a CHECK keyword without specifying a constraint.
Either remove it:
CREATE TABLE User_T(
IdUser NUMBER(10,0) NOT NULL,
FirstName VARCHAR2(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(50) NOT NULL,
UserName VARCHAR(30) NOT NULL UNIQUE,
UserPassword VARCHAR2(30) NOT NULL,
CreatedAt VARCHAR2(30),
UpdatedAt VARCHAR(30),
Enabled CHAR(10), -- Remove the CHECK
CONSTRAINT IdUser_PK PRIMARY KEY(IdUser)
);
else, specify the constraint:
CREATE TABLE User_T(
IdUser NUMBER(10,0) NOT NULL,
FirstName VARCHAR2(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(50) NOT NULL,
UserName VARCHAR(30) NOT NULL UNIQUE,
UserPassword VARCHAR2(30) NOT NULL,
CreatedAt VARCHAR2(30),
UpdatedAt VARCHAR(30),
Enabled CHAR(10) CHECK (Enabled IN ('Foo ', 'Bar ')),
CONSTRAINT IdUser_PK PRIMARY KEY(IdUser)
);
db<>fiddle here
CREATE TABLE Parent
(
pID int IDENTITY(1,1) PRIMARY KEY,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Address varchar(50) NOT NULL,
PersonalNumber int NOT NULL,
Phone varchar(50),
)
CREATE TABLE Student
(
sID int IDENTITY(1,1) PRIMARY KEY,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Address varchar(50) NOT NULL,
PersonalNumber int NOT NULL,
Phone varchar(50),
-- causes error message
FOREIGN KEY (pID) REFERENCES Parent(pID),
);
I get the error message:
Foreign key references invalid table
How do I solve this?
Thanks.
You missed the pID column in student table. Try this:
CREATE TABLE Parent (
pID int IDENTITY(1,1) PRIMARY KEY,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Address varchar(50) NOT NULL,
PersonalNumber int NOT NULL,
Phone varchar(50)
);
CREATE TABLE Student(
sID int IDENTITY(1,1) PRIMARY KEY,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Address varchar(50) NOT NULL,
PersonalNumber int NOT NULL,
Phone varchar(50),
pID int,
FOREIGN KEY (pID) REFERENCES Parent(pID)
);
This may be a stupid mistake but I'm new to Informix and I can't seem to figure out why none of my CREATE TABLE statements won't run. I keep getting a syntax error on all of my CREATE TABLE statements.
CREATE TABLE customer(
store_num INTEGER NOT NULL,
store_name VARCHAR(20) NOT NULL,
addr VARCHAR(20),
addr2 VARCHAR(20),
city VARCHAR(15),
state VARCHAR(2),
zip-code VARCHAR(5),
contact_name VARCHAR(30),
phone VARCHAR(18),
CONSTRAINT cust_pk PRIMARY KEY(store_num)
);
create table orders(
order_num INTEGER NOT NULL,
order_date DATE NOT NULL,
store_num INTEGER NOT NULL,
fac_code CHAR(3),
ship_instr CHAR(10),
promo CHAR(1) NOT NULL,
CONSTRAINT orders_pk PRIMARY KEY(order_num)
);
create table factory(
fac_code CHAR(3) NOT NULL,
fac_name CHAR(15) NOT NULL,
CONSTRAINT fac_pk PRIMARY KEY(fac_code)
);
create table stock(
stock_num INTEGER NOT NULL,
fac_code CHAR(3) NOT NULL,
description CHAR(15) NOT NULL,
reg_price DECIMAL(8,2) NOT NULL,
promo_price DECIMAL(8,2),
price_updated DATE,
unit CHAR(4) NOT NULL,
CONSTRAINT stock_pk PRIMARY KEY(stock_num)
);
create table items(
order_num INTEGER NOT NULL,
stock_num INTEGER NOT NULL,
quantity SMALLINT NOT NULL,
price DECIMAL(8,2) NOT NULL,
CONSTRAINT items_pk PRIMARY KEY(order_num, stock_num)
);
create table state(
state_code CHAR(2) NOT NULL,
state_name CHAR(15) NOT NULL,
CONSTRAINT state_pk PRIMARY KEY(state_code)
);
Any help will be appreciated.
For reasons I don't understand, Informix requires the constraint name after the constraint, whereas standard SQL requires the constraint name before the constraint.
Thus, in addition to changing zip-code to zip_code (as pointed out by daniel.shih in an answer), you need:
CREATE TABLE customer(
store_num INTEGER NOT NULL,
store_name VARCHAR(20) NOT NULL,
addr VARCHAR(20),
addr2 VARCHAR(20),
city VARCHAR(15),
state VARCHAR(2),
zip_code VARCHAR(5),
contact_name VARCHAR(30),
phone VARCHAR(18),
PRIMARY KEY(store_num) CONSTRAINT cust_pk
);
You can try customer's field zip_code instead of zip-code
CREATE TABLE P_OWNER(
OWNERID CHAR(5) NOT NULL,
LNAME VARCHAR2(50) NOT NULL,
FNAME VARCHAR2(30) NULL,
SSN CHAR(9) NULL,
EMAIL VARCHAR2(50) NULL,
CONSTRAINT P_OWNER_PK PRIMARY KEY(OWNERID),
CONSTRAINT P_OWNER_AK1 ALTERNATE KEY(SSN),
CONSTRAINT P_OWNER_AK2 ALTERNATE KEY(EMAIL)
);
CREATE TABLE P_PROPERTY(
PROPERTYID CHAR(5) NOT NULL,
STREET1 VARCHAR2(50) NOT NULL,
STREET2 VARCHAR2(50) NULL,
CITY VARCHAR2(50) DEFAULT 'FITCHBURG',
STATE CHAR(2) DEFAULT 'MA',
ZIP CHAR(10) NULL,
SQFOOTAGE NUMBER(5,0) NULL,
HAS_AC CHAR(1) NULL,
ISHOUSE CHAR(1) NOT NULL,
CONSTRAINT P_PROPERTY_PK PRIMARY KEY(PROPERTYID),
CONSTRAINT P_PROPERTY_ADDRESS_AK1 ALTERNATE KEY(CITY, STATE, STREET1, STREET2, ZIP)
);
Error at Command Line:8 Column:57
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
You want a Unique Key instead of Alternate
CREATE TABLE P_OWNER(
OWNERID CHAR(5) NOT NULL,
LNAME VARCHAR2(50) NOT NULL,
FNAME VARCHAR2(30) NULL,
SSN CHAR(9) NULL,
EMAIL VARCHAR2(50) NULL,
CONSTRAINT P_OWNER_PK PRIMARY KEY(OWNERID),
CONSTRAINT P_OWNER_AK1 UNIQUE(SSN),
CONSTRAINT P_OWNER_AK2 UNIQUE(EMAIL)
);
CREATE TABLE P_PROPERTY(
PROPERTYID CHAR(5) NOT NULL,
STREET1 VARCHAR2(50) NOT NULL,
STREET2 VARCHAR2(50) NULL,
CITY VARCHAR2(50) DEFAULT 'FITCHBURG',
STATE CHAR(2) DEFAULT 'MA',
ZIP CHAR(10) NULL,
SQFOOTAGE NUMBER(5,0) NULL,
HAS_AC CHAR(1) NULL,
ISHOUSE CHAR(1) NOT NULL,
CONSTRAINT P_PROPERTY_PK PRIMARY KEY(PROPERTYID),
CONSTRAINT P_PROPERTY_ADDRESS_AK1 UNIQUE(CITY, STATE, STREET1, STREET2, ZIP)
);
Problem is with ALTERNATE KEY try with UNIQUE
CREATE TABLE P_OWNER(
OWNERID CHAR(5) NOT NULL,
LNAME VARCHAR2(50) NOT NULL,
FNAME VARCHAR2(30) NULL,
SSN CHAR(9) NULL,
EMAIL VARCHAR2(50) NULL,
CONSTRAINT P_OWNER_PK PRIMARY KEY(OWNERID),
CONSTRAINT P_OWNER_AK1 UNIQUE (SSN),
CONSTRAINT P_OWNER_AK2 UNIQUE (EMAIL)
);
CREATE TABLE P_PROPERTY(
PROPERTYID CHAR(5) NOT NULL,
STREET1 VARCHAR2(50) NOT NULL,
STREET2 VARCHAR2(50) NULL,
CITY VARCHAR2(50) DEFAULT 'FITCHBURG',
STATE CHAR(2) DEFAULT 'MA',
ZIP CHAR(10) NULL,
SQFOOTAGE NUMBER(5,0) NULL,
HAS_AC CHAR(1) NULL,
ISHOUSE CHAR(1) NOT NULL,
CONSTRAINT P_PROPERTY_PK PRIMARY KEY(PROPERTYID),
CONSTRAINT P_PROPERTY_ADDRESS_AK1 UNIQUE(CITY, STATE, STREET1, STREET2, ZIP)
);
An alternate key (or secondary key) is any candidate key which is not selected to be the primary key (PK). You cant create an alternate key in Oracle, its just conceptual.
could someone have a look at this for me, I can't seem to find why it is not working.
CREATE TABLE Person(
Person_ID int auto_increment NOT NULL,
Person_Type_ID int NOT NULL,
Create_Date datetime NOT NULL ,
Modify_Date datetime NOT NULL ,
First_Name varchar(50) NOT NULL,
Surname varchar(50) NOT NULL,
DOB date NOT NULL,
Gender char(1) NOT NULL CHECK (Gender ='f' OR Gender ='m'),
Archive char(1) NULL,
Allergies varchar(200) NOT NULL,
Dietry_Requirements varchar(200) NOT NULL,
Disabilities varchar(200) NOT NULL,
Medicine_Requirements varchar(200) NOT NULL,
username varchar (30) NOT NULL,
password varchar (30) NOT NULL,
CONSTRAINT PK_Person_ID PRIMARY KEY (Person_ID)
CONSTRAINT FK_Person_Type_ID FOREIGN KEY (Person_Type_ID)
REFERENCES Person_Type (Person_Type_ID));
You missed a comma! This should work...
CREATE TABLE Person(
Person_ID int auto_increment NOT NULL,
Person_Type_ID int NOT NULL,
Create_Date datetime NOT NULL ,
Modify_Date datetime NOT NULL ,
First_Name varchar(50) NOT NULL,
Surname varchar(50) NOT NULL,
DOB date NOT NULL,
Gender char(1) NOT NULL CHECK (Gender ='f' OR Gender ='m'),
Archive char(1) NULL,
Allergies varchar(200) NOT NULL,
Dietry_Requirements varchar(200) NOT NULL,
Disabilities varchar(200) NOT NULL,
Medicine_Requirements varchar(200) NOT NULL,
username varchar (30) NOT NULL,
password varchar (30) NOT NULL,
CONSTRAINT PK_Person_ID PRIMARY KEY (Person_ID),
CONSTRAINT FK_Person_Type_ID FOREIGN KEY (Person_Type_ID)
REFERENCES Person_Type (Person_Type_ID));