Incorrect syntax near the keyword 'CONSTRAINT' - mysql

I am getting this error message ...
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'CONSTRAINT'.
I've done my research and I'm still stuck on completing this script.
Here`s what I entered.
DROP TABLE SEMESTER;
DROP TABLE CLASS;
DROP TABLE STUDENT;
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null);
CREATE TABLE CLASS (
title CHAR(40) not null PRIMARY KEY,
instructor CHAR(40) not null );
CREATE TABLE SEMESTER (
year int not null );
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),
CONSTRAINT pk_class PRIMARY KEY (title, instructor),
CONSTRAINT pk_semester PRIMARY KEY (semid, year));

I think those CONSTRAINT statements need to be inside the CREATE TABLE statements. For example, the STUDENT table should look like this:
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null,
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),
);
Otherwise, how could the RDBMS know to which table each constraint actually belongs?

DROP TABLE SEMESTER;
DROP TABLE CLASS;
DROP TABLE STUDENT;
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),);
CREATE TABLE CLASS (
title CHAR(40) not null ,
instructor CHAR(40) not null ,
CONSTRAINT pk_class PRIMARY KEY (title, instructor));
CREATE TABLE SEMESTER (
year int not null ,
semid int not null
CONSTRAINT pk_semester PRIMARY KEY (semid, year));;
AS per my understanding
Here constraint must be create inside the table for student
and
second table u are creating 2 times primary key on class(title)
and
coming to third table semid itself not present in semester table

Related

Why can't I add foreign key constraints

this is my code:
CREATE DATABASE exams;
SHOW DATABASES;
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(40),
last_name VARCHAR(40)NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE(email));
SHOW table status
INSERT INTO exams_3121(student_id, first_name, middle_name, last_name, email, password, reg_date)
CREATE TABLE entries
(
entrie_id int NOT NULL,
student_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (subject_id)REFERENCES subject(subject_id)
)
CREATE DATABASE subjects;
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(20) NOT NULL,
level_entery VARCHAR(40)NOT NULL,
exam_board VARCHAR(60) NOT NULL,
PRIMARY KEY (subject_id));
CREATE TABLE entries
(
entrie_id int NOT NULL,
entrie_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (subject_id)REFERENCES subject(subject_id)
)
When I use this code it says cannot add foreign key constraint
and I don't know what to do. Please and thanks in advance.
There are two problems.
First, you got the names of the table you're referencing wrong. The name of the tables are students and subjects, but you wrote student and subject.
Second, the entries table has two entrie_id columns. One of them should be student_id.
CREATE TABLE entries
(
entrie_id int NOT NULL,
student_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (subject_id) REFERENCES subjects(subject_id)
)
Also, if you're creating multiple databases and putting your tables in them, you'll need to refer to tables with their database prefixes if they're different from the one you selected as default with the USE command. As you've written it, you're not actually using the databases you created with CREATE DATABASE.

Error creating mysql table with Foreign Key

I have researched thoroughly before asking this question including on this site.
I have a students table:
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE (email));
I also have a subjects table:
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(40) NOT NULL,
level_of_entry VARCHAR(20) NOT NULL,
exam_board VARCHAR(20) NOT NULL,
PRIMARY KEY (subject_id));
I am now creating a table to link the above tables:
CREATE TABLE IF NOT EXISTS entries(
exam_date DATETIME NOT NULL,
FOREIGN KEY (student_id) REFERENCES students (student_id),
FOREIGN KEY (subject_id) REFERENCES subjects (subject_id)
);
My problem is that when I try to declare the foreign keys in the third table called entries, I get an error stating that the subject_id foreign key is not in the table referenced.
ERROR 1072 (42000) : Key column 'student_id' doesn't exist in table, even though it is clearly contained inside the the students table and the same applies to 'subject_id' and the subject table.
I am certain that my syntax for declaring the foreign keys is correct so I am unsure how to fix the problem.
All help is appreciated. Thank you.
You forgot to create these two columns before applying your foreign key constraints :
CREATE TABLE IF NOT EXISTS entries(
exam_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
student_id INT UNSIGNED NOT NULL,
subject_id INT UNSIGNED NOT NULL,
exam_date DATETIME NOT NULL,
PRIMARY KEY (exam_id),
FOREIGN KEY (student_id) REFERENCES students (student_id),
FOREIGN KEY (subject_id) REFERENCES subjects (subject_id)
);
EDIT :
I advise you to add in every table a unique ID column (here : exam_id).

Error with creating table

This is my statement, I'm getting an error on Customer_T. The error states:
"01:22:05 DROP TABLE customer_t Error Code: 1051. Unknown table 'energyefficient.customer_t' 0.016 sec"
CREATE TABLE Customer_t
(CustomerID INT NOT NULL,
Name VARCHAR(45) NOT NULL,
Address VARCHAR(256) ,
Email VARCHAR(100) ,
Phone VARCHAR(16) ,
CONSTRAINT PK_CustomerID PRIMARY KEY (CustomerID));
CREATE TABLE Order_t
(OrderID INT NOT NULL,
OrderDate DATE NULL,
CustomerID INT NOT NULL,
CONSTRAINT PK_OrderID PRIMARY KEY (OrderID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES customer_t(CustomerID));
CREATE TABLE Equipment
(EquipmentID INT NOT NULL,
EquipmentType VARBINARY(12) ,
YearOfManufacture INT ,
Cost DECIMAL(9,2) ,
Maker VARCHAR(45) ,
Model VARCHAR(45) ,
CustomerID INT ,
CONSTRAINT EquipmentID_PK PRIMARY KEY (EquipmentID),
CONSTRAINT CustomerID_FK FOREIGN KEY (CustomerID) REFERENCES Customer_t(CustomerID));
CREATE TABLE Order_Line_t
(OrderLineID INT NOT NULL,
OrderID INT NOT NULL,
EquipmentID INT NOT NULL,
OrderLineCost DECIMAL(9,2) ,
CONSTRAINT OrderLineID_PK PRIMARY KEY (OrderLineID),
CONSTRAINT OrderID_FK1 FOREIGN KEY (OrderID) REFERENCES Order_t(OrderID),
CONSTRAINT EquipmentID_FK2 FOREIGN KEY (EquipmentID) REFERENCES Equipment_t(EquipmentID));
CREATE TABLE MaintenanceSchedule_t
(MaintenanceID INT NOT NULL,
MaintenanceType VARCHAR(45) NOT NULL,
Schedule_Date DATE NOT NULL,
EquipmentID INT NOT NULL,
ServiceID INT ,
CONSTRAINT MaintenanceID_PK PRIMARY KEY (MaintenanceID),
CONSTRAINT EquipmentID_FK3 FOREIGN KEY (EquipmentID) REFERENCES Equipment(EquipmentID));
CREATE TABLE Service
(ServiceID INT NOT NULL,
EstimatedCost DECIMAL(9,2) NOT NULL,
Status VARCHAR(16) NOT NULL,
ServiceDate DATE ,
EquipmentID INT NOT NULL,
EmployeeID INT NOT NULL,
ActualCost DECIMAL(9,2) ,
ServiceType VARCHAR(45) NOT NULL,
Notes VARCHAR(2000) ,
CONSTRAINT ServiceID_PK PRIMARY KEY (ServiceID),
CONSTRAINT EquipmentID_FK FOREIGN KEY (EquipmentID) REFERENCES Equipment(EquipmentID));
CREATE TABLE Employee_t
(EmployeeID INT NOT NULL,
AnnualSalary DECIMAL(9,2) ,
Name VARCHAR(45) NOT NULL,
DOB DATE ,
POSITION VARCHAR(45) ,
CONSTRAINT EmployeeID_PK PRIMARY KEY (EmployeeID));
try using this:
set foreign_key_checks=0;
drop table energyefficient.customer_t;
set foreign_key_checks=1;
Some time it needs to set foreign key checks to 0;
Thanks
Possibility 1:
Your table creation script does not have any error.
As per the error message shown:
"01:22:05 DROP TABLE customer_t
Error Code: 1051. Unknown table 'energyefficient.customer_t' 0.016 sec"
You are trying to drop the table customer_t in a database named energyefficient.
Possible reason is that you have created the table in some other database and trying to drop from somewhere else.
Possibility 2:
You can modify the drop table to include if exists clause, so that the error is suppressed and ignored silently but with a warning, if such table does not exist.
Example:
DROP TABLE IF EXISTS customer_t;
In case if you are dropping the parent table first, change the global variable foreign_key_checks to false and run the drops.
set foreign_key_checks = 0;
DROP TABLE IF EXISTS customer_t;
-- other drop statements here.
set foreign_key_checks = 1; -- reset to default

wish to use empID from `users` table to `leave` table

i am relatively new in phpmysql.i need some help...
i have two table namely users and leave.In users table i have following column
empID(int)
username(varchar)
password(varchar)
mac(varchar)
ip(varchar)
and in leave table i have following column
lid(int)
empname(varchar)
username(varchar)
nod(int)
sdate(date)
edate(date)
reason(varchar)
action(varchar)
Now i want to use empID in leave table.how can i use it.i have to say that i already apply a code but its not working.Its giving the following error..
1072 - Key column 'empID' doesn't exist in table
and the code
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
so whats wrong...please help me..
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
should be
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID int, // here you need one like this
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
you are missing a empID column.
Your SQL to create the leave table includes the correct bit to create the foreign key relationship:
FOREIGN KEY(empID) REFERENCES users(empID)
but that only creates the relationship between the tables, based on the empID columns. It doesn't create the empID column for you! You also need a line above, to define the empID column in the leave table, something like this:
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID INT NOT NULL,
empname ...
... more lines ,
FOREIGN KEY(empID) REFERENCES users(empID)

sql Error 1064 (42000) Syntax Error

CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id')
FOREIGN KEY ('userid') REFERENCES users('id'));
I was just wondering if someone could help me in identifying a syntax error as I can not create a table.
Try to put , after the primary key declaration.
Update: I guess it should be
CREATE TABLE IF NOT EXISTS message (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
I'm assuming this is for MS SQL Server? If you get MS SQL Server Studio, you can script stuff which gives you an idea:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[message]') AND type in (N'U'))
CREATE TABLE message(
id INT IDENTITY NOT NULL,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (userid) REFERENCES users(id))
GO
You query should be as below
CREATE TABLE IF NOT EXISTS message (
id INT auto_increment PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
Provided you have id as a primary key in users table.
CREATE TABLE users (id INT PRIMARY KEY)
Your query should look like this:
CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id'),
FOREIGN KEY ('userid') REFERENCES users('id')
) Engine=InnoDB;
Note the , after PRIMARY KEY ('id').
Small trick
You don't have to specify foreign keys in table definitions. It's practical when you do it like this (because dump may export tables in order that foreign keys will fail on creating/inserting):
CREATE TABLE 1; -- With references to table 2
CREATE TABLE 2;
INSERT INTO 1;
INSERT INTO 2;
ALTER TABLE 1 ADD FOREIGN KEY (user_id) REFERENCES 2 2(id);
Try to change the name of your table, May be message is an in-built keyword in MySQL.
Update to this: I guess it should be
CREATE TABLE IF NOT EXISTS myMessage (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));