I cannot figure out what the problem is with my table..
CREATE TABLE PLAYER
(FirstName VARCHAR(15) NOT NULL,
LastName VARCHAR(15) NOT NULL,
BirthDate DATE,
Address VARCHAR(30),
EmailAddress VARCHAR(30) NOT NULL,
Sex CHAR,
TeamName VARCHAR(30) NOT NULL,
ClubName VARCHAR(30) NOT NULL,
AgeGroup VARCHAR(5) NOT NULL,
PRIMARY KEY (LastName),
FOREIGN KEY (TeamName)
**);**
CREATE TABLE TEAMS
(TeamName VARCHAR(30) NOT NULL,
AgeGroup VARCHAR(5) NOT NULL,
Sex CHAR,
CoachFName VARCHAR(20) NOT NULL,
CoachLName VARCHAR(20),
ClubName VARCHAR(30) NOT NULL,
PRIMARY KEY (TeamName),
FOREIGN KEY (AgeGroup)
**FOREIGN KEY (ClubName)**
);
Below is the message I keep receiving.. I honestly have no idea how to get this table to execute! I highlighted in bold where the message said the error was near.
*Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ')'.
Msg 156, Level 15, State 1, Line 24
Incorrect syntax near the keyword 'FOREIGN'.*
You need to specify what table and field the foreign keys reference. For example, for the first error, you need to specify foreign key (teamname) references teams(teamname), if that is what you mean.
Related
I'm trying to create a database in Ubuntu using MySQL in the command line, I need to create a table with the following data:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
Condition VARCHAR(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
It just returns an error that says "ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Condition varchar(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle' at line 6"
What is wrong with my code? The "Price" and "Engine_Size" columns need to only be a float/decimal values so they can't be varchar because I want to only be able to insert numbers.
This works:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size int NOT NULL,
Vehicle_Condition VARCHAR(255) NOT NULL,
Price numeric(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Notes:
For Price you seem to want numeric, not float, because you are specifying the precision and scale.
For Engine_Size, I have no idea what float(1, 1) is supposed to mean. I am guessing that int is an appropriate type.
Condition is a reserved word in MySQL, so I changed the name of the column.
The only apparent problem in your code is the use of 'condition' as a column name as condition is a reserved word in MySQL.
You can fix it in 2 ways:
Don't use 'condition' as a column name:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
V_Condition VARCHAR(255) NOT NULL, //Just an example feel free to use any another name
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Put 'condition' inside backticks (``)
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
`Condition` VARCHAR(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Hope this helps
I was following W3Schools' guide on how to create a Foreign Key but every time I try to run my code it gives me an error.
My code
CREATE TABLE students(
`Student Number` INT NOT NULL PRIMARY KEY,
`First Name` VARCHAR(255) NOT NULL,
`Last Name` VARCHAR(255) NOT NULL,
Address VARCHAR(255) NOT NULL,
City VARCHAR(255) NOT NULL,
State VARCHAR(255) NOT NULL,
Zip INT NOT NULL,
`Date of Birth` DATE NOT NULL,
`Major ID` INT,
Advisor VARCHAR(255),
`Enroll Date` DATE,
`Total Credits Complete` INT,
FOREIGN KEY `Major ID` REFERENCES Major(`Major ID`)
);
I'm trying to give 'Major ID' a Foreign Key but instead I get this error code
Error SQL query:
CREATE TABLE students(
Student Number INT NOT NULL PRIMARY KEY,
First Name VARCHAR(255) NOT NULL,
Last Name VARCHAR(255) NOT NULL,
Address VARCHAR(255) NOT NULL,
City VARCHAR(255) NOT NULL,
State VARCHAR(255) NOT NULL,
Zip INT NOT NULL,
Date of Birth DATE NOT NULL,
Major ID INT,
Advisor VARCHAR(255),
Enroll Date DATE,
Total Credits Complete INT,
FOREIGN KEY Major ID REFERENCES Major(Major ID) ) MySQL said:
1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'REFERENCES Major(Major ID) )' at line 14
I would really appreciate it if someone could gear me in the right direction.
Thank you for taking your time to help me.
This is what I have now. I have done a check for DECIMAL() using BETWEEN AND. Was wondering if it is possible to do the same for VARCHAR()
CREATE TABLE STUDENT(
student_number CHAR(4) NOT NULL,
first_name VARCHAR(50) NULL,
last_name VARCHAR(50) NULL,
date_of_birth DATE NULL,
student_type VARCHAR(20) NULL,
CONSTRAINT student_pkey PRIMARY KEY (student_number),
CONSTRAINT student_check1 CHECK (student_type POSTGRADUDATE_STUDENT OR UNDERGRADUATE_STUDENT));
I am trying to create a table in a MySQL database, I am not sure where I am going wrong with my syntax. In my query it is returning a 1064 error in the line where I use ENUM. Here is part of my query:
CREATE TABLE General
(
pId varchar(30) NOT NULL UNIQUE,
Hometown varchar(30) NOT NULL,
Year char(2) NOT NULL ENUM("FR","SO","JR","SR")
Position varchar(2) NOT NULL ENUM("PG","SG","SF","PF","C"),
Season char(4) NOT NULL DEFAULT 2016,
Date_Of_Birth DATE NOT NULL,
CONSTRAINT PRIMARY KEY (pId));
missing comma , enum don't need type, season is a string '2016' and remove constraint for primary key
CREATE TABLE General
(
pId varchar(30) NOT NULL UNIQUE,
Hometown varchar(30) NOT NULL,
Year ENUM("FR","SO","JR","SR"), <---- here missing commma
Position ENUM("PG","SG","SF","PF","C"),
Season char(4) NOT NULL DEFAULT '2016',
Date_Of_Birth DATE NOT NULL,
PRIMARY KEY (pId)
);
this is the relational table where am getting my foreign keys from
CREATE TABLE EMPLOYEE(
ENUM DECIMAL(12) NOT NULL,
FNAME VARCHAR(50) NOT NULL,
INITIALS VARCHAR(5) NULL,
LNAME VARCHAR(50) NOT NULL,
DOB DATE NULL,
BLDG DECIMAL(3) NOT NULL,
STREET VARCHAR(50) NOT NULL,
SUBURB VARCHAR(50) NOT NULL,
STATE VARCHAR(5) NOT NULL,
ZIPCODE DECIMAL(4) NOT NULL,
CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY(ENUM) );
and i want to create a new table called EMPNAME but have error when adding my foreign key constraint
CREATE TABLE EMPNAME(
ENUM DECIMAL(12) NOT NULL,
FNAME VARCHAR(50) NOT NULL,
INITIALS VARCHAR(5) NOT NULL,
LNAME VARCHAR(50) NOT NULL,
CONSTRAINT EMPNAME_PKEY PRIMARY KEY(ENUM),
CONSTRAINT EMPNAME_FKEY FOREIGN KEY(ENUM, FNAME, INITIALS, LNAME) REFERENCES EMPLOYEE(ENUM, FNAME, INITIALS, LNAME)
);
i dont get errors if i only set the primary key enum as foreign key but when i make all of the attributes foreign keys, i keep getting error.
thanks
Your reference columns must have indexes in order to be used as foreign keys, so add index(es) on these columns in your employee table:
CREATE TABLE EMPLOYEE (
ENUM DECIMAL (12) NOT NULL,
FNAME VARCHAR (50) NOT NULL,
INITIALS VARCHAR (5) NULL,
LNAME VARCHAR (50) NOT NULL,
DOB DATE NULL,
BLDG DECIMAL (3) NOT NULL,
STREET VARCHAR (50) NOT NULL,
SUBURB VARCHAR (50) NOT NULL,
STATE VARCHAR (5) NOT NULL,
ZIPCODE DECIMAL (4) NOT NULL,
CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY (ENUM),
INDEX `FKEYS` (ENUM, FNAME, INITIALS, LNAME)
);
Then you will be able to add your constraint into second table.