I am currently working on some SQL coursework, this is my code so far:
CREATE TABLE VEHICLES
(
Vehicle_id VARCHAR(6) PRIMARY KEY NOT NULL,
Vehicle_Make VARCHAR(18),
Vehicle_Model VARCHAR(25),
Passenger_Number INT,
Number_Owned INT,
Registration_Date DATE, NOT NULL UNIQUE
Colour VARCHAR(10),
Rate INT
);
Each time I try and run the statement it gives me
SQL ERROR: ORA-00904: invalid identifier
You have a comma at the wrong place.
Here it is corrected:
CREATE TABLE VEHICLES
(
Vehicle_id VARCHAR(6) PRIMARY KEY NOT NULL,
Vehicle_Make VARCHAR(18),
Vehicle_Model VARCHAR(25),
Passenger_Number INT,
Number_Owned INT,
Registration_Date DATE NOT NULL UNIQUE,
Colour VARCHAR(10),
Rate INT
);
Related
I just can't see what needs to be changed in this code:
create table tblGirlScout (
GirlScout_ID varchar(10,0) not null,
GirlScoutFirstName varchar(25) not null,
GirlScoutLastName varchar(25),
GirlScoutAddress varchar(30),
GirlScoutCity varchar(20),
GirlScoutState char(2),
GirlScoutPostalCode varchar(9),
Constraint tblGirlScout_PK Primary Key(GirlScout_ID)
);
I believe you wanted to set GirlScout_ID column as a number, not a varchar. This is what causes the problem. Here is a fix
create table tblGirlScout (
GirlScout_ID number(10,0) not null,
GirlScoutFirstName varchar(25) not null,
GirlScoutLastName varchar(25),
GirlScoutAddress varchar(30),
GirlScoutCity varchar(20),
GirlScoutState char(2),
GirlScoutPostalCode varchar(9),
Constraint tblGirlScout_PK Primary Key(GirlScout_ID)
);
create table paydetails (
emp_id integer (20),
dept_id integer (20),
basic integer(20),
deductions integer(20),
additions integer (20),
joining_date date(20,20,20)
);
This should work in almost any database:
create table paydetails (
emp_id int,
dept_id int,
basic int,
deductions int,
additions int,
joining_date date
);
Most databases do not have a parameter for int (although some interpret that as a length).
As far as I know, none have three parameters for date.
This will work for you. Do yourself the favor of adding in the auto increment id as your unique/primary key.
CREATE TABLE `db_name`.`paydetails` (
`id` INT(20) NOT NULL AUTO_INCREMENT ,
`emp_id` INT(20) NOT NULL ,
`dept_id` INT(20) NOT NULL ,
`basic` INT(20) NOT NULL ,
`deductions` INT(20) NOT NULL ,
`additions` INT(20) NOT NULL ,
`joining_date` DATE NOT NULL ,
PRIMARY KEY (`id`)) ENGINE = MyISAM;
This is the code that created the table.
CREATE TABLE CUSTOMERS
(
Customer_ID INT NOT NULL,
CHECK(Customer_ID <= 11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS PRIMARY KEY(Customer_ID);
Then I try to insert data (using this code) into the table and that is where I get this error.
INSERT INTO dbo.CUSTOMERS(Customer_ID, First_Name, Last_Name, Home_Street, Home_City, Home_State, Home_Zip, PhoneNumber)
VALUES (11223344556, 'John', 'Doe', '1234 Hand Street', 'Wahiawa', 'HI', 96786, 2535551267);
What am I doing wrong and what can I do to fix this issue?
AS per my understanding you are checking length of Customer_ID <=11 so you should mention len(Customer_ID)<=11 it will work and you should alter datatype of Customer_ID int to bigint
CREATE TABLE CUSTOMERS
(
Customer_ID bigINT NOT NULL,
CHECK(len(Customer_ID)<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);
INSERT INTO dbo.CUSTOMERS(Customer_ID,First_Name,Last_Name,Home_Street,
Home_City,Home_State,Home_Zip,PhoneNumber)
VALUES(11223344556,'John','Doe','1234 Hand Street',
'Wahiawa','HI',96786,2535551267);
You need customer id to be bigint:
CREATE TABLE CUSTOMERS
(
Customer_ID BIGINT NOT NULL,
CHECK(Customer_ID<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);
The problem may be due to CHECK(Customer_ID<=11) since Customer_ID id integer datatype the server may check for integer validation and not length validation. Try to change the validation.
this is my code
CREATE TABLE orders(
id integer NOT NULL,
name varchar(12),
orderno int,
city varchar(12),
price int
PRIMARY KEY ('id')
);
please check this image for error
try this
CREATE TABLE orders(
id int NOT NULL PRIMARY KEY,
name varchar(12),
orderno int,
city varchar(12),
price int
);
While Creating Table you can Declare the primary key Attribute with the type of the attribute. like.
CREATE TABLE orders(
id integer PRIMARY KEY NOT NULL,
name varchar(12),
orderno int,
city varchar(12),
price int);
Here are my two tables I created.
CREATE TABLE Employee
(
EmpID int IDENTITY(1,1) PRIMARY KEY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(50) NOT NULL,
StreetAddress VARCHAR(75),
City VARCHAR(255),
State VARCHAR(25),
ZipCode VARCHAR(5),
EmployeeType VARCHAR (20),
HourlyWage DECIMAL(18,2)
)
CREATE TABLE WagesPayable
(
FOREIGN KEY (EmpID) REFERENCES Employee (EmpID),
WorkedHours DECIMAL(18,2),
PayRate DECIMAL(18,2),
TotalPayable AS (WorkedHours * PayRate),
DateLastPaid DATETIME
)
Whenever I try and create the WagesPayable table I get the following error:
Msg 1769, Level 16, State 1, Line 26
Foreign key 'EmpID' references invalid column 'EmpID' in referencing table 'WagesPayable'.
What am I missing here? Your help is much appreciated.
You need to declare the column before the reference. You can do this all in one step:
CREATE TABLE WagesPayable
(
EmpID int REFERENCES Employee (EmpID),
WorkedHours DECIMAL(18,2),
PayRate DECIMAL(18,2),
TotalPayable AS (WorkedHours * PayRate),
DateLastPaid DATETIME
)
The SQL Fiddle is here.
You aren't creating a column EmpID for table WagesPayable.
So first you need to create the column, and then reference it as a foreign key like this :
CREATE TABLE WagesPayable
(
EmpID int,
WorkedHours DECIMAL(18,2),
PayRate DECIMAL(18,2),
TotalPayable AS (WorkedHours * PayRate),
DateLastPaid DATETIME,
FOREIGN KEY (EmpID) REFERENCES Employee (EmpID)
)