I cannot get this .sql file to run successfully in MySQL? - sql

MySQL CMD Client will run this file but get half way through and complain my syntax is wrong i have looked over and changed bits in it all day but just cannot find the issue.
I know again its probably a very easily answered question but after looking at it all day its time for a fresh pair of eyes to have a look for me. Please help!
DROP DATABASE FIFAWC2010;
CREATE DATABASE FIFAWC2010;
USE FIFAWC2010;
CREATE TABLE VENUE(
VENUE_ID CHAR(3),
LOC_COORDS VARCHAR(50),
CITY VARCHAR(20),
VENUE_NAME VARCHAR(20),
MAX_CAPACITY NUMERIC(9),
PRIMARY KEY(VENUE_ID)
);
CREATE TABLE REFEREE(
REF_ID CHAR(10),
REF_FNAME VARCHAR(20),
REF_SNAME VARCHAR(20),
NATIONALITY VARCHAR(20),
PRIMARY KEY(REF_ID)
);
CREATE TABLE MATCH(
MATCH_ID CHAR(11),
VENUE_ID CHAR(3),
MATCH_DATE DATE,
KICK_OFF TIME,
FINAL_SCORE VARCHAR(7),
REF_ID CHAR(10),
VENUE_ATTEN NUMERIC(9),
PRIMARY KEY(MATCH_ID,VENUE_ID) REFERENCES VENUE(VENUE_ID),
);
CREAT TABLE GROUP(
GROUP_ID CHAR(1),
MATCH_ID VARCHAR(28),
PRIMARY KEY(GROUP_ID),
);
CREATE TABLE COUNTRY(
COUNTRY_ID VARCHAR(20),
GROUP_ID CHAR(1),
PRIMARY KEY(COUNTRY_ID),
);
CREATE TABLE PLAYER(
PLAYER_ID CHAR(10),
PLAYER_FNAME VARCHAR(20),
PLAYER_SNAME VARCHAR(20),
POSITION VARCHAR(10),
MATCH_ID CHAR(11),
COUNRTY_ID VARCHAR(20),
PRIMARY KEY(PLAYER_ID),
);
COMMIT;
Thank you! :) Ash.
UPDATE:
Sorry i pasted the wrong code, this is the code i am using and having issues with:
DROP DATABASE FIFAWC2010;
CREATE DATABASE FIFAWC2010;
USE FIFAWC2010;
CREATE TABLE VENUE(
VENUE_ID CHAR(3),
LOC_COORDS VARCHAR(50),
CITY VARCHAR(20),
VENUE_NAME VARCHAR(20),
MAX_CAPACITY NUMERIC(9),
PRIMARY KEY(VENUE_ID)
);
CREATE TABLE REFEREE(
REF_ID CHAR(10),
REF_FNAME VARCHAR(20),
REF_SNAME VARCHAR(20),
NATIONALITY VARCHAR(20),
PRIMARY KEY(REF_ID)
);
CREATE TABLE MATCH(
MATCH_ID CHAR(11),
VENUE_ID CHAR(3),
MATCH_DATE DATE,
KICK_OFF TIME,
FINAL_SCORE VARCHAR(7),
REF_ID CHAR(10),
VENUE_ATTEN NUMERIC(9),
PRIMARY KEY(MATCH_ID,VENUE_ID) REFERENCES VENUE(VENUE_ID),
FOREIGN KEY(REF_ID) REFERENCES REFEREE(REF_ID)
);
CREAT TABLE GROUP(
GROUP_ID CHAR(1),
MATCH_ID VARCHAR(28),
PRIMARY KEY(GROUP_ID),
FOREIGN KEY(MATCH_ID) REFERENCES MATCH(MATCH_ID)
);
CREATE TABLE COUNTRY(
COUNTRY_ID VARCHAR(20),
GROUP_ID CHAR(1),
PRIMARY KEY(COUNTRY_ID),
FOREIGN KEY(GROUP_ID) REFERENCES GROUP(GROUP_ID)
);
CREATE TABLE PLAYER(
PLAYER_ID CHAR(10),
PLAYER_FNAME VARCHAR(20),
PLAYER_SNAME VARCHAR(20),
POSITION VARCHAR(10),
MATCH_ID CHAR(11),
COUNRTY_ID VARCHAR(20),
PRIMARY KEY(PLAYER_ID),
FOREIGN KEY(COUNTRY_ID) REFERENCES COUNTRY(COUNTRY_ID),
FOREIGN KEY(MATCH_ID) REFERENCES MATCH(MATCH_ID)
);
COMMIT;
I get Error 1064 (42000) 4 times, and its on MATCH, GROUP, (MATCH_ID), (GROUP_ID).
Is this a syntax error with the Primary keys?

My guess : having a , on the last line of a list... like this one:
PRIMARY KEY(MATCH_ID,VENUE_ID) REFERENCES VENUE(VENUE_ID),
change to
PRIMARY KEY(MATCH_ID,VENUE_ID) REFERENCES VENUE(VENUE_ID)
you have 4 of them.

I have managed to fix the error my self, it would seem that MATCH and GROUP are actually keywords that MySQL / SQL uses and cannot be used as the names of my tables, I installed MySQL WorkBench and it will underline errors for you.
Also to create a composite key with a Foreign Key, you first create the variable for the data to sit in, use the foreign key syntax to get the data into the variable, then use the usual syntax for a composite key with the Foreign Key variable in place.
Thank you everyone for your help!
Ash.

Related

Oracle Live SQL ORA-00907: missing right parenthesis, Running on other compiler [duplicate]

This question already has an answer here:
Create Table Error - Oracle SQL
(1 answer)
Closed 2 years ago.
CREATE TABLE COUNTY(
NAME VARCHAR(255) NOT NULL PRIMARY KEY,
POPULATION int,
INFECTIONRATE int,
TOTALNUMBEROFINFECTIONS int,
TOTALNUMBEROFNEWINFECTIONS int,
COUNTRYNAME VARCHAR(255) FOREIGN KEY REFERENCES COUNTRY(NAME)
)
When trying to run my code on Oracle Live I run into this exception: ORA-00906: missing left parenthesis
Proper syntax:
CREATE TABLE COUNTY (NAME VARCHAR(255) NOT NULL PRIMARY KEY,
POPULATION int,
INFECTIONRATE int,
TOTALNUMBEROFINFECTIONS int,
TOTALNUMBEROFNEWINFECTIONS int,
COUNTRYNAME VARCHAR(255) REFERENCES COUNTRY(NAME) -- without FOREIGN KEY
)
You can either phrase the statement like so, using the inline foreign key declaration syntax:
CREATE TABLE COUNTY(
NAME VARCHAR2(255) NOT NULL PRIMARY KEY,
POPULATION int,
INFECTIONRATE int,
TOTALNUMBEROFINFECTIONS int,
TOTALNUMBEROFNEWINFECTIONS int,
COUNTRYNAME VARCHAR2(255) REFERENCES COUNTRY(NAME)
);
Or you can use the following syntax, which lets you choose the name of the constraint you create:
CREATE TABLE COUNTY(
NAME VARCHAR2(255) NOT NULL PRIMARY KEY,
POPULATION int,
INFECTIONRATE int,
TOTALNUMBEROFINFECTIONS int,
TOTALNUMBEROFNEWINFECTIONS int,
COUNTRYNAME VARCHAR2(255),
CONSTRAINT FK_COUNTRY FOREIGN KEY (COUNTRYNAME) REFERENCES COUNTRY(NAME)
);

Number of referencing columns must match referenced columns

I am relatively new to SQL and I keep getting the following error "The number of columns in the foreign-key referencing list is not equal to the number of columns in the referenced list."
create table client
(name varchar(30),
phone int,
city varchar(20),
state char(2) CHECK(state='MN' OR state='ND' OR state='SD' OR state='WI' or state='IA'),
primary key(name,phone));
create table owns_vehicle
(name varchar(30),
phone int,
vin varchar(10),
primary key(name, phone, vin),
foreign key(name,phone) references client);
create table service_appointment
(mydate date,
vin varchar(10),
mechanic varchar(15),
description varchar(30) NOT NULL,
cost int CHECK (cost>=0),
primary key(mydate,vin),
foreign key(vin) references owns_vehicle);
This is the line that is causing the issue:
foreign key(vin) references owns_vehicle);
Does anyone know why I am getting this error?
You try to reference owns_vehicle, thus you need to reference all of the tables PRIMARY KEY columns, which would be name, phone, vin. But your foreign key has only vin, thus there are two columns (name, phone) missing.
The primary key has three parts. You need to reference all of them. If you are going to use this model, then you need the other two components:
create table service_appointment (
mydate date,
name varchar(30),
phone int,
vin varchar(10),
mechanic varchar(15),
description varchar(30) NOT NULL,
cost int CHECK (cost>=0),
primary key(mydate,vin),
foreign key(name, phone, vin) references owns_vehicle (name, phone, vin)
);
However, I think you data model should change. VINs can change owners. People can change phone numbers. These don't seem like good components of a primary key.
If you assume that a vehicle can only have a single owner at a given time, you can make your current data model work by changing owns_vehicle to have a uniqueness constraint on VIN:
CREATE TABLE OWNS_VEHICLE
(NAME VARCHAR2(30),
PHONE INT,
VIN VARCHAR2(10),
CONSTRAINT PK_OWNS_VEHICLE
PRIMARY KEY(NAME, PHONE, VIN)
USING INDEX,
CONSTRAINT OWNS_VEHICLE_UQ1
UNIQUE(VIN)
USING INDEX,
CONSTRAINT OWNS_VEHICLE_FK1
FOREIGN KEY(NAME,PHONE) REFERENCES CLIENT(NAME, PHONE));

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

SQL - is developing queries based on nonexistent data possible?

Is it possible to develop a query from nonexistent data? for example, I have this schema :
-- ZIP code database
BEGIN TRANSACTION;
CREATE TABLE city (
city_id INT,
city_name VARCHAR(80),
city_county VARCHAR(80),
city_state VARCHAR(5),
city_country VARCHAR(5),
city_region VARCHAR(5),
longitude INT,
latitude INT,
timezone varchar(80),
PRIMARY KEY (city_id)
);
CREATE TABLE zipcode (
zipcode CHAR(5),
ziptype VARCHAR(36),
active BOOLEAN,
primary_city_id INT NOT NULL,
notes TEXT,
PRIMARY KEY (zipcode),
FOREIGN KEY (primary_city_id) REFERENCES city(city_id)
);
CREATE TABLE acceptable (
zipcode CHAR(5),
city_id INT,
acceptable BOOLEAN,
PRIMARY KEY (zipcode, city_id),
FOREIGN KEY (zipcode) REFERENCES zipcode(zipcode),
FOREIGN KEY (city_id) REFERENCES city(city_id)
);
CREATE TABLE zip_areacode (
zipcode CHAR(5),
areacode VARCHAR(5),
PRIMARY KEY (zipcode, areacode),
FOREIGN KEY (zipcode) REFERENCES zipcode(zipcode),
FOREIGN KEY (areacode) REFERENCES areacode(prefix)
);
COMMIT;
//from here, would I be able to filter out results by the zip code with the highest population? This isn't possible, right? I don't see any data regarding population
No, you cant. Because you have no information about population. Thus your queries will be on non existent relations. That's not possible.
You can create temporary tables (relations) if you want. And fill it with random data. But I don't think you'll find out something useful.

Object already named in the database ERROR

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)
)