Object already named in the database ERROR - sql

I'm trying to do a simple a database SQL script where I simply need tocreate a database as well as 4 tables within. So far I'm getting a very annoying error that states: There is already an object named 'DEVICE_TYPE' in the database. Why, I don't know? Any help regarding this matter will be highly appreciated.
Please see the code below:
DROP Database EliteShop
GO
CREATE Database EliteShop
GO
USE EliteShop
GO
CREATE TABLE DEVICE_TYPE
(
Device_TypeID INT IDENTITY(1,1),
Device_TypeID_Name VARCHAR(50),
CONSTRAINT pk_devicetype PRIMARY KEY(Device_TypeID)
)
CREATE TABLE MANUFACTURER
(
Manufacturer_Code VARCHAR(50),
Manufacturer_Description VARCHAR(100),
CONSTRAINT pk_manufacturercode PRIMARY KEY(Manufacturer_Code)
)
CREATE TABLE [PLATFORM]
(
PlatformID INT IDENTITY(1,1),
Platform_Description VARCHAR(50),
CONSTRAINT pk_platID PRIMARY KEY(PlatformID)
)
CREATE TABLE DEVICE
(
DeviceID INT IDENTITY(1,1),
Device_TypeID INT,
PlatformID INT,
Manufacturer_Code VARCHAR(50),
Model VARCHAR(50),
InternalMemory VARCHAR(10),
Price MONEY,
CONSTRAINT pk_deviceID PRIMARY KEY (DeviceID),
CONSTRAINT fk_deviceType FOREIGN KEY (Device_TypeID) REFERENCES DEVICE_TYPE(Device_TypeID),
CONSTRAINT fk_manuFCode FOREIGN KEY (Manufacturer_Code) REFERENCES MANUFACTURER(Manufacturer_Code),
CONSTRAINT fk_myPlatID FOREIGN KEY (PlatformID) REFERENCES [PLATFORM](PlatformID)
)

Related

Why can I not create a foreign key for this table?

Table 1:
CREATE TABLE News
(
News_Id int PRIMARY KEY IDENTITY(1,1),
Title nvarchar(200) NOT NULL,
Author nvarchar(50),
Calender DATETIME NOT NULL,
Contents nvarchar(20) NOT NULL, --store link to text file
[Type_Id] int ,
FOREIGN KEY ([Type_Id]) REFERENCES [Types]([Type_Id])
)
Table 2:
CREATE TABLE [Types]
(
[Type_Id] int,
Name nvarchar(50),
PRIMARY KEY ([Type_Id])
)
I get this error:
FK_News references invalid table Types
Can you help me?
You have to create Type table first and then News tables, as you are referencing foreign key from Type table to News table.
Table1:
CREATE TABLE [Types](
[Type_Id] int,
Name nvarchar(50),
PRIMARY KEY ([Type_Id])
)
Table2:
CREATE TABLE News(
News_Id int PRIMARY KEY IDENTITY(1,1),
Title nvarchar(200) NOT NULL,
Author nvarchar(50),
Calender DATETIME NOT NULL,
Contents nvarchar(20) NOT NULL, --store link to text file
[Type_Id] int ,
FOREIGN KEY ([Type_Id]) REFERENCES [Types]([Type_Id])
)
Since you have created News table first, drop it and recreate it. Or create both the tables without constraints and then alter both the tables with primary key ad foreign key receptively
Since the Types table seems to be the primary one, you should create it first:
CREATE TABLE [Types](
[Type_Id] int,
Name nvarchar(50),
PRIMARY KEY ([Type_Id])
);
Then, create the News table, which has foreign keys referring to Types above:
CREATE TABLE News(
News_Id int PRIMARY KEY IDENTITY(1,1),
Title nvarchar(200) NOT NULL,
Author nvarchar(50),
Calender DATETIME NOT NULL,
Contents nvarchar(20) NOT NULL, --store link to text file
[Type_Id] int ,
FOREIGN KEY ([Type_Id]) REFERENCES [Types]([Type_Id])
);
It isn't possible to point a foreign key to a table which does not yet exist.

ERROR :ORA-02264: name already used by an existing constraint

create table ward
(
wnum int primary key,
wname varchar(30),
phoneno int,
wloc varchar(50),
chnursename varchar(20) constraint ward_fk references charge_nurse
);
create table charge_nurse
(
chnurse varchar(20) constraint charge_nurse_pk primary key,
stnum int constraint charge_nurse_fk references staff
);
create table staff
(
stname varchar(20),
stnum int constraint staff_pk primary key,
addr varchar(20),
phoneno int,
stposition varchar(30),
specality varchar(30) unique,
shift varchar(10),
noofhoursperweek int
);
create table generalsupplies
(
itnum int constraint generalsupplies_pk primary key,
itname varchar(20) unique,
quantityinstock int,
reorder varchar(10),
despt varchar(10),
costperunit int
);
create table pharmasupplies
(
dnum int constraint pharmasupplies_pk primary key,
dname varchar(30) unique,
despt varchar(20),
dosage_Mg int,
quantityinstock int,
reorder varchar(10),
costperunit int
);
While creating the below table I am facing problem:
ORA-02264: name already used by an existing constraint
create table centralsupplies
(
wardnum int constraint centralsupplies_fk references ward,
itemnum int constraint centralsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_fk references staff,
regnum int unique,
dateord date,
daterec date
);
How do I solve this problem?
You use 3 times the same constraint name centralsupplies_fk in your centralsupplies table.
3 constraints = 3 constraint names
Your create table statement has four foreign keys all called centralsupplies_fk. That is not allowed: constraint names must be unique within a schema. You must give each one a different name.
It is usual practice to include the referenced table in the key name. So
create table centralsupplies
(
wardnum int constraint centralsupplies_ward_fk references ward,
itemnum int constraint centralsupplies_generalsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_pharmasupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_staff_fk references staff,
regnum int unique,
dateord date,
daterec date
)
Also you have another foreign key constraint on STAFFNAME which you have not named. You do not need to name constraints, the system will generate a unique one for you, but it's generally a good idea to name them, not least because it is easier to diagnose relational integrity error messages with meaningfully named constraints.
However, in this case the correct solution is to drop the STAFFNAME column. You already have a foreign on the STAFF table, and you should join to that table whenever you need to display a value for STAFFNAME. Besides you do not have a unique constraint on staff.stname column, so the foreign key statement will fail: foreign keys can only reference primary or unique keys.

Problem in creating new table ORA-00906: missing left parenthesis

Here is my instruction :
it works on my friend computer !
CREATE TABLE typeRdv1(
id_type int PRIMARY KEY,
des VARCHAR(20)
);
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR NOT NULL,
type_rdv VARCHAR(20),
state VARCHAR(20),
CONSTRAINT `FK_cin_rdv` FOREIGN KEY (cin_pat)
REFERENCES patients(cin_pat) ON DELETE NO ACTION,
CONSTRAINT `FK_id_type_rdv` FOREIGN KEY (type_rdv)
REFERENCES typeRdv1(id_type) ON DELETE NO ACTION
);
A ORA-00906: missing left parenthesis
Any help !
Please make sure all the "varchar" fields has a length defined. In your second table, it says: date_rdv VARCHAR NOT NULL, which I believe causing the issue, so try to change it into date_rdv VARCHAR(50) NOT NULL and try again.
...
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR(50) NOT NULL, --length added to varchar
type_rdv VARCHAR(20),
state VARCHAR(20),
...
EDIT:
The reason you get "invalid character" error is that you cannot use "on delete no action" while creating a table with a constraint. Instead you should alter your table after to create your constraints with "on delete cascade" option. Probably like below ( you may need to drop your constraints first):
CREATE TABLE patients(
cin_pat int PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE typeRdv1(
id_type int PRIMARY KEY,
des VARCHAR(20)
);
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR(50) NOT NULL,
type_rdv VARCHAR(20),
state VARCHAR(20)
);
alter table rdv1 add (
CONSTRAINT FK_cin_rdv FOREIGN KEY (cin_pat)
REFERENCES patients (cin_pat) ON DELETE cascade,
CONSTRAINT FK_id_type_rdv FOREIGN KEY (type_rdv)
REFERENCES typeRdv1 (id_type) ON DELETE cascade
);
Check this for more details: https://www.haidongji.com/2006/07/24/defining-no-action-foreign-key-constraints-in-oracle/comment-page-1/

Can't figure out how to make a subquery

CREATE TABLE TBLTeams
(
teamnm varchar(30) NOT NULL,
teamid varchar(10),
rondenm varchar(10)
primary key(teamnm)
)
CREATE TABLE TBLWedstrijd
(
thuisteamnm varchar(30) NOT NULL,
uitteamnm varchar(30) NOT NULL,
wedstrijdid varchar(10)
primary key(wedstrijdid)
)
alter table TBLWedstrijd add foreign key (thuisteamnm) references TBLTeams(teamnm)
alter table TBLWedstrijd add foreign key (uitteamnm) references TBLTeams(teamnm)
CREATE TABLE TBLUitslag
(
thuisteamnm varchar(30) NOT NULL,
uitteamnm varchar(30) NOT NULL,
wedstrijdid varchar(10) NOT NULL,
uitteampunt int,
thuisteampunt int
primary key(thuisteamnm)
)
alter table TBLUitslag add foreign key (wedstrijdid) references TBLWedstrijd(wedstrijdid)
If I update teamnm in TBLTeams how can I update reference in TBLWedstrijd uitteamnm. I know its a subquery but I really dont know how.
You're going about it a bit wrong. If you're going to change the teamname, you shouldn't use it as a key (Since that will break all references).
You'll want to reference their IDs from the TBLWedstrijd instead of using their name.
So your statements will become:
CREATE TABLE TBLWedstrijd
(
wedstrijdid varchar(10),
thuisteamid varchar(10) NOT NULL,
uitteamid varchar(10) NOT NULL,
primary key(wedstrijdid)
)
alter table TBLWedstrijd add foreign key (thuisteamid) references TBLTeams(teamid)
alter table TBLWedstrijd add foreign key (uitteamid) references TBLTeams(teamid)
The same is valid for your table TBLUitslag, you should only reference TBLWedstrijd, since that one already holds the info about which teams are playing. Not to mention, the score is pretty much part of a game anyway so it doesn't make a lot of sense to seperate those into 2 tables. So that becomes:
CREATE TABLE TBLWedstrijd
(
wedstrijdid varchar(10),
thuisteamid varchar(10) NOT NULL,
uitteamid varchar(10) NOT NULL,
uitteampunt int,
thuisteampunt int
primary key(wedstrijdid)
)
This is called "table normalisation" if you wanted to google some stuff for more info about it.

Microsoft SQL Server Management Studio - Trying to add FK constraint, getting error: "Invalid column..."

I'm trying to add a foreign key constraint to link my tables, however I keep getting this error:
Msg 1769, Level 16, State 1, Line 1
Foreign key 'fk_customers_titles' references invalid column 'title_id' in referencing table 'customers'.
I've created my tables, along with it's columns and also stated my primary keys using the ALTER TABLE statement. But whenever I try to add a foreign key constraint for any table, I keep getting this error.
This is how I created my tables:
CREATE TABLE customers
(
customer_id char_idtype,
name varchar(50) NOT NULL,
contact_name varchar(30),
address varchar(50),
city varchar(20),
region varchar(15),
country_code varchar(10),
country varchar(15),
phone varchar(20),
fax varchar(20)
);
GO
CREATE TABLE titles
(
title_id char(3) NOT NULL,
description varchar(35) NOT NULL
);
GO
And this is how I added my primary keys:
ALTER TABLE customers
ADD PRIMARY KEY (customer_id);
GO
ALTER TABLE titles
ADD PRIMARY KEY (title_id);
GO
This is how I am adding my foreign key constraint:
ALTER TABLE customers
ADD CONSTRAINT fk_customers_titles
FOREIGN KEY (title_id)
REFERENCES titles(title_id);
I am using Microsoft SQL Server Management Studio 2012.
Your help is appreciated,
Thanks
I think the error is pretty clear: customers does not have a title_id. You need to add it . . .
ALTER TABLE customers ADD title_id char(3);
Or put it in the CREATE TABLE statement.
Your script should be like below mentioned.
CREATE TABLE titles
(
title_id char(3) Primary key NOT NULL,
description varchar(35) NOT NULL
);
GO
CREATE TABLE customers
(
customer_id char(3) primary key,
title_id char(3) foreign key references titles,
name varchar(50) NOT NULL,
contact_name varchar(30),
address varchar(50),
city varchar(20),
region varchar(15),
country_code varchar(10),
country varchar(15),
phone varchar(20),
fax varchar(20)
);
GO