I met an error in an SQL command when trying to create a table in SQL. Below is my command:
CREATE TABLE Registration
(
registrationID varchar2(5) NOT NULL CONSTRAINT registrationID PRIMARY KEY,
competitionID varchar2(5) NOT NULL CONSTRAINT competitionID REFERENCES Competition(competitionID),
competitorID varchar2(5) NOT NULL CONSTRAINT competitorID REFERENCES Competitor(competitorID),
categoryType varchar2(6) NOT NULL,
entryFeeStatus char(1) NOT NULL,
creditCardNumber number(16),
datePaid date
);
My competitionID is primary key of Competition table.
My competitorID is primary key of Competitor table.
The error shown is:
Error report -
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
*Cause: The specified constraint name has to be unique.
*Action: Specify a unique constraint name for the constraint.
May I know what I should change in my statement? Thank you.
Below are the Competition and Competitor tables that I created:
CREATE TABLE Competition
(
competitionID varchar2(5) NOT NULL CONSTRAINT competitionID PRIMARY KEY,
timePlanned date NOT NULL,
country varchar2(50) NOT NULL,
city varchar2(50),
address varchar2(50),
entryFee number(4) NOT NULL
);
CREATE TABLE Competitor
(
competitorID varchar2(5) NOT NULL CONSTRAINT competitorID PRIMARY KEY,
firstName varchar2(9) NOT NULL,
lastName varchar2(9) NOT NULL,
dateOfBirth date NOT NULL,
nationality varchar2(12),
gender varchar2(1) NOT NULL,
lifetimeRanking number(6),
totalPrizeMoney number(6)
);
You mixed up the two different ways to specify foreign keys. And had an extra comma.
CREATE TABLE Registration
(
registrationID varchar2(5) NOT NULL CONSTRAINT registrationID PRIMARY KEY,
competitionID varchar2(5) NOT NULL CONSTRAINT competitionIDfk REFERENCES Competition(competitionID),
competitorID varchar2(5) NOT NULL CONSTRAINT competitorIDfk REFERENCES Competitor(competitorID)
)
Foreign keys can either be specified per column:
columnname datatype CONSTRAINT constraintname REFERENCES tablename [ (column) ]
Or per table:
CONSTRAINT constraintname FOREIGN KEY (column-list) REFERENCES tablename [ (column-list) ]
BOOLEAN is not valid SQL type. Use NUMBER(1) with 0 and 1, CHAR(1) or VARCHAR2(1) with 'Y' and 'N' values or other surrogate representation.
Related
The ArtistID column in Piece table refers to the ArtistID in the Artist table. Likewise, the LocationID column in Piece refers to LocationID in the GeographicLocation table. However, both foreign key references throw a "not the same data type as referencing column" error. What am I doing wrong?
CREATE TABLE dbo.Artist
(
ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Sex CHAR(1) NOT NULL CHECK(Sex = 'F' OR Sex = 'M')
)
CREATE TABLE dbo.Piece
(
PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Artist(ArtistID),
LocationID SMALLINT NOT NULL
FOREIGN KEY REFERENCES GeographicLocation(LocationID),
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Appraiser(AppraiserID)
)
CREATE TABLE dbo.GeographicLocation
(
LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL
)
I think you missed a table here, which is AppraiserID column in Piece table refers to the AppraiserID in the Appraisertable
So, when I execute your SQL I am getting below error:
Foreign key 'FK__Piece__Appraiser__322C6448' references invalid table 'Appraiser'.
First Create a table called Appraiser, then create Foreign key references for those column.
Below SQL I am able to create all tables and Foreign key references:
CREATE TABLE dbo.Artist
(ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Gender CHAR(1) NOT NULL CHECK(Gender = 'F' OR Gender = 'M'))
CREATE TABLE dbo.GeographicLocation
(LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL)
CREATE TABLE dbo.Appraiser
(AppraiserID SMALLINT PRIMARY KEY IDENTITY(1, 5),
AppraisedValue MONEY NOT NULL,
AppraisedName VARCHAR(100) NULL)
CREATE TABLE dbo.Piece
(PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL,
LocationID SMALLINT NOT NULL,
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL)
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_ArtistID FOREIGN KEY(ArtistID)
REFERENCES dbo.Artist (ArtistID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_ArtistID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_AppraiserID FOREIGN KEY(AppraiserID)
REFERENCES dbo.Appraiser (AppraiserID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_AppraiserID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_LocationID FOREIGN KEY(LocationID)
REFERENCES dbo.GeographicLocation (LocationID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_LocationID
GO
added Default Constraint on a Foreign key column for default value so if the user does not supply value default value should be inserted. but instead of firing constraint, it shows Column name or number of supplied values does not match table definition.
--Table--
create table tblperson(
Id int not null,
Name nvarchar(50) not null,
Email nvarhcar(50) not null,
GenderId int
);
--tblGender--
Create Table tblGender(
ID int Not Null Primary Key,
Gender nvarchar(50)
);
--Add foreign Key--
Alter table tblPerson
add constraint tblPerson_GenderId_FK FOREIGN KEY (GenderId) references tblGender(ID)
--Default Constraint--
ALTER TABLE tblPerson
ADD CONSTRAINT DF_tblPerson_GenderId
DEFAULT 1 FOR GenderId
--Insert Statement--
insert into tblperson values(5,'Jake','j#j.com')
The answer to your question is:
insert into tblperson(Id, Name, Email) values(1,'Jake','j#j.com');
Most of people forget to write the columns names, you should always be carful of the columns names and the number of supplied values.
BTW, fix the typo in this line:
Email nvarhcar(50) not null,
it should be:
Email nvarchar(50) not null,
I need to get the percentage increase revenue since last year, per country.
create table Customer
(
customer_mail_address varchar(255) not null,
lastname varchar(50) not null,
firstname varchar(50) not null,
payment_method varchar(10) not null,
payment_card_number varchar(30) not null,
contract_type varchar(20) not null,
subscription_start date not null,
subscription_end date ,
user_name varchar(30) not null,
password varchar(50) not null,
country_name varchar(50) not null,
gender char(1) ,
birth_date date ,
constraint pk_Customer primary key(customer_mail_address),
constraint fk_Customer1 foreign key(country_name) references Country(country_name) ON UPDATE CASCADE,
constraint fk_Customer2 foreign key(contract_type) references Contract(contract_type) ON UPDATE CASCADE,
constraint fk_Customer3 foreign key(payment_method) references Payment(payment_method) ON UPDATE CASCADE,
constraint chk_Customer1 check(subscription_start < subscription_end),
constraint uc_Customer unique(user_name),
constraint chk_Customer2 check(len([password]) >= 8 AND [password] like '%[0-9]%'),
constraint chk_Customer3 check(birth_date < subscription_start),
constraint chk_Customer4 check(user_name NOT LIKE '%[^A-Z0-9]%')
)
create table Watchhistory
(
movie_id integer not null,
customer_mail_address varchar(255)not null,
watch_date date not null,
price numeric(5,2)not null,
invoiced bit not null,
constraint pk_Watchhistory primary key(movie_id, customer_mail_address, watch_date),
constraint fk_Watchhistory1 foreign key(movie_id) references Movie(movie_id),
constraint fk_Watchhistory2 foreign key(customer_mail_address) references Customer(customer_mail_address) ON UPDATE CASCADE ON DELETE CASCADE
)
These two tables are what I use to calculate this. I tried a lot of stuff but nothing really did the trick. This is what I have now to calculate the revenue for each year per country but not the percentage increase per country.
CREATE VIEW OmzetStijgingDalingPerLand AS
SELECT DATEPART(yy,watch_date) as [jaar], country_name, SUM(price) AS [omzet]
FROM Watchhistory w
INNER JOIN Customer c ON w.customer_mail_address = c.customer_mail_address
GROUP BY DATEPART(yy,watch_date), country_name
As it is described in article mentioned before, LAG function is the key
SELECT jaar,
country_name,
omzet,
omzet / LAG(omzet, 1) OVER(PARTITION BY country_name ORDER BY jaar) AS increase_percent
FROM OmzetStijgingDalingPerLand
I'm trying to declare a FOREIGN KEY using SQL but i'm always getting that error :
FOREIGN KEY (Dno) REFERENCES DEPARTMENT (Dnumber))
ERROR at line 14: ORA-00942: table or view does not exist
FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn))
*
ERROR at line 8: ORA-00942: table or view does not exist
This is the code i'm using :
CREATE TABLE employee (fname VARCHAR (15) NOT NULL,
minit CHAR,
lname VARCHAR (15) NOT NULL,
ssn CHAR (9) NOT NULL,
bdate DATE,
address VARCHAR (30),
sex CHAR,
salary DECIMAL (10, 2),
super_ssn CHAR (9),
dno INT NOT NULL,
PRIMARY KEY (ssn),
FOREIGN KEY (super_ssn) REFERENCES employee (ssn),
FOREIGN KEY (dno) REFERENCES department (dnumber));
CREATE TABLE department (dname VARCHAR (15) NOT NULL,
dnumber INT NOT NULL,
mgr_ssn CHAR (9) NOT NULL,
mgr_start_date DATE,
PRIMARY KEY (dnumber),
UNIQUE (dname),
FOREIGN KEY (mgr_ssn) REFERENCES employee (ssn));
Since your employee table create statement has references to the department table, you'll need to make sure the department table is created first. However, you have a reference to the employee table as part of the create department table script. Circular references ahoy!
Fortunately, you can create constraints separately from the create table script, which is what you'll have to do in this case, e.g.:
CREATE TABLE employee (fname VARCHAR (15) NOT NULL,
minit CHAR,
lname VARCHAR (15) NOT NULL,
ssn CHAR (9) NOT NULL,
bdate DATE,
address VARCHAR (30),
sex CHAR,
salary DECIMAL (10, 2),
super_ssn CHAR (9),
dno INT NOT NULL,
PRIMARY KEY (ssn),
FOREIGN KEY (super_ssn) REFERENCES employee (ssn));
CREATE TABLE department (dname VARCHAR (15) NOT NULL,
dnumber INT NOT NULL,
mgr_ssn CHAR (9) NOT NULL,
mgr_start_date DATE,
PRIMARY KEY (dnumber),
UNIQUE (dname),
FOREIGN KEY (mgr_ssn) REFERENCES employee (ssn));
alter table employee add constraint emp_dept_fk FOREIGN KEY (dno) REFERENCES department (dnumber);
I have 4 tables, 3 of which have their own primary keys. In the 4th table, the primary keys of all the other 3 tables are referenced as foreign keys and these are made into a primary key (combination of all 3 foreign keys). However, upon entering data into the columns, I get constraint violation errors.
The code snippets are as below:
--Table # 1
create table PartSupplier(
partSupplierID int primary key,
fName varchar(20) NOT NULL,
lName varchar(20),
houseNO varchar(20),
streetName varchar(20),
city varchar(20),
dob date,
contactNo varchar(50)
)
--Table # 2
create table PartSpecialist(
partSpecialistID int primary key,
fName varchar(20) NOT NULL,
lName varchar(20),
houseNO varchar(20),
streetName varchar(20),
city varchar(20),
dob date,
contactNo varchar(15)
)
--Table # 3
create table CarPart(
partNumber int primary key,
description varchar (250),
vehicleID int NOT NULL,
imagePath varchar(50)
)
--Table # 4 (with multiple foreign keys made into one primary key)
create table SpecialistSuppliedPart(
partSpecialistID int NOT NULL,
partSupplierID int NOT NULL,
partNumber int NOT NULL,
condition varchar(10),
dateAcquired date,
costPrice int NOT NULL,
constraint specialist_supplier_part_PK primary key (partSpecialistID, partSupplierID, partNumber),
foreign key (partSpecialistID) references PartSpecialist(partSpecialistID),
foreign key (partSupplierID) references PartSupplier(partSupplierID),
foreign key (partNumber) references CarPart(partNumber)
)
I tried inserting the following values in the table:
3 3 5 USED 5000 2011-03-01
3 13 18 NEW 6000 2011-06-26
upon which I got the following error:
http://i.stack.imgur.com/HhFk2.jpg
where FK_SpecialistSuppliedPart_PartSupplier is the Foreign Key existing between Table # 1 and Table # 4.
I have all of these entries existing in the respective tables. After an exhaustive search on the internet (including stackoverflow.com), I could not find a solution.
What am I doing wrong here?
P.S. I tried adding the Foreign and Primary keys using MSSQLs Design Tool but it still results in the same error.