Receiving a syntax error for this SQL code - mysql

Syntax error:-
CREATE TABLE CUSTOMER (
CustomerID Int NOT NULL IDENTITY(1,1) PRIMARY KEY,
CustomerName Char(25) NOT NULL,
CustomerDeliveryAddress Char(25) NOT NULL,
CustomerPhone Char (10) NOT NULL,
CustomerBillingAddress Char(25) NOT NULL,
CustomerCreditCard Int NOT NULL,
CONSTRAINT CustomerPK PRIMARY KEY(CustomerID)
);

In mysql there is nothing named identity, instead there is AUTO_INCREMENT
CREATE TABLE CUSTOMER (
CustomerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustomerName Char(25) NOT NULL,
CustomerDeliveryAddress Char(25) NOT NULL,
CustomerPhone Char (10) NOT NULL,
CustomerBillingAddress Char(25) NOT NULL,
CustomerCreditCard Int NOT NULL
);

u have to declare primary key only once
CREATE TABLE CUSTOMER (
CustomerID INT NOT NULL AUTO_INCREMENT,
CustomerName Char(25) NOT NULL,
CustomerDeliveryAddress Char(25) NOT NULL,
CustomerPhone Char (10) NOT NULL,
CustomerBillingAddress Char(25) NOT NULL,
CustomerCreditCard Int NOT NULL,
PRIMARY KEY(CustomerID));

Related

produce an error Error Code: 1215. Cannot add foreign key constraint in mysql

i made 2 tables like this,
CREATE TABLE personal (
id INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50),
created_at DATETIME);
CREATE TABLE personal_details (
id INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
personal_id INT(255),
tittle VARCHAR(10) NOT NULL,
family_name VARCHAR(50) NOT NULL,
given_name VARCHAR(50) NOT NULL,
place_of_birth VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
gender VARCHAR(25) NOT NULL,
country_citizen VARCHAR(50) NOT NULL,
national_identity_number INT(50) NOT NULL,
passport_no INT(50) NOT NULL,
issue_date DATE NOT NULL,
expiry_date DATE NOT NULL,
FOREIGN KEY (personal_id) REFERENCES personal(id)
);
but it returns an error like this Error Code: 1215. Cannot add foreign key constraint
even tough i already follow this solution from another question
https://stackoverflow.com/a/16969116/17067132
I am a bit confused aboud the use scholarship_uiii in between the two CREATE-statements.
But the reason for the error is that you are missing the UNSIGNED for your personal_id field.
So your second CREATE-statement should read:
CREATE TABLE personal_details (
id INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
personal_id INT(255) UNSIGNED,
tittle VARCHAR(10) NOT NULL,
family_name VARCHAR(50) NOT NULL,
given_name VARCHAR(50) NOT NULL,
place_of_birth VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
gender VARCHAR(25) NOT NULL,
country_citizen VARCHAR(50) NOT NULL,
national_identity_number INT(50) NOT NULL,
passport_no INT(50) NOT NULL,
issue_date DATE NOT NULL,
expiry_date DATE NOT NULL,
FOREIGN KEY (personal_id) REFERENCES personal(id)
);

")" is not valid at this position, expecting an identifier CREATE TABLE

I'm keep getting this error ")" is not valid at this position, expecting an identifier
although I had created table similar way but getting this error for any new table I'm trying to create.
CREATE TABLE IF NOT EXISTS company.employee(
fname VARCHAR(15) NOT NULL,
mname CHAR NULL,
laname VARCHAR(15) NOT NULL,
ssn CHAR(9) NOT NULL,
Bdate DATE,
Adress VARCHAR(35) NOT NULL,
Sex CHAR NULL,
Salary DECIMAL (10,2) NULL,
Supper_ssn CHAR(9) NULL,
Dno INT NOT NULL,
PRIMARY KEY(ssn)
);
CREATE TABLE IF NOT EXISTS company.Department(
Dname VARCHAR(15) NOT NULL,
Dnumber INT NOT NULL,
Mgr_ssn CHAR(9) NOT NULL,
Mgr_start_date DATE NULL,
PRIMARY KEY (Dnumber)
);
USE compnay;
CREATE TABLE IF NOT EXISTS projects(
Pname VARCHAR(15) NOT NULL,
Pnumber INT NOT NULL,
Plocation VARCHAR(15),
Dnum INT NOT NULL,
PRIMARY KEY(Pnumber),
UNIQUE(Pname)
);
CREATE TABLE IF NOT EXISTS Dept_location(
Dnumber INT NOT NULL,
Dlocation VARCHAR(15) NOT NULL,
PRIMARY KEY(Dnumber,Dlocation)
);
CREATE TABLE IF NOT EXISTS Works_on(
Essn CHAR(9) NOT NULL,[![enter image description here][1]][1]
);

I cannot create the database GradeBook with the foreign key. When I try to add it I get error1046: choose database

CREATE SCHEMA GRADEBOOK;
CREATE TABLE GRADEBOOK.PERSON
(PERSON_ID INT NOT NULL,
Fname varchar(15) NOT NULL,
Minit varchar (1),
Lname varchar(15) NOT NULL,
B_date date NOT NULL,
SEX varchar(1) NOT NULL,
ADDRESS varchar(50) NOT NULL,
TELEPHONE VARCHAR(12),
STATUS varchar(10) NOT NULL,
PRIMARY KEY (PERSON_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.STUDENT
(STUDENT_ID INT NOT NULL,
SUBJECT_ID VARCHAR (30) NOT NULL,
PRIMARY KEY (STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.ALUMNUS
(STUDENT_ID INT NOT NULL,
GRAD_YEAR date NOT NULL,
PRIMARY KEY(STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.SUBJECT
(SUBJECT_ID VARCHAR (30) NOT NULL,
SUBJECT_NAME VARCHAR (20) NOT NULL,
CLASS_ID VARCHAR (20) NOT NULL,
PRIMARY KEY (SUBJECT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOUSE
(HOUSE_ID INT NOT NULL,
HOUSE_NAME VARCHAR (20) NOT NULL,
HOUSE_COLOR VARCHAR (20) NOT NULL,
PRIMARY KEY (HOUSE_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLUB
(CLUB_ID INT auto_increment NOT NULL,
CLUB_name varchar(20) NOT NULL,
PRIMARY KEY (CLUB_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOMEROOM
(HOMEROOM_ID VARCHAR(20) NOT NULL,
CLASS_ID VARCHAR(20) NOT NULL,
PRIMARY KEY( HOMEROOM_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLASS
(CLASS_ID VARCHAR(20) NOT NULL,
CLASS_name VARCHAR(20) NOT NULL,
HOMEROOM_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (CLASS_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.TEACHER
(TEACHER_ID INT NOT NULL,
HOOMROOM VARCHAR(20) NOT NULL,
SUBJECT_ID VARCHAR(20) NOT NULL,
YEAR INT NOT NULL,
TERM VARCHAR(20) NOT NULL,
PRIMARY KEY(TEACHER_ID))
ENGINE=INNODB;

Getting unknown errors in MySQL

create table Member_ID(
Member_ID not null,
Title varchar(4) not null,
Forename varchar(30) not null,
Surname varchar(30) not null,
DOB date not null,
Address 1 varchar (30), not null,
Address 2 varchar (30), not null,
Postcode varchar(8), not null,
MobileNo char (11), not null,
Email varchar (30), not null,
Gender char (1), not null,
Medical varchar (30), not null,
Joining_Date date not null,
Paid varchar (4), not null,
Membership_Type char(1), not null,
Staff_Initials char (2), not null,
Primary key (Member_ID) );
create table class(
Class_Name varchar (30) not null,
ClassDayofWeek date not null,
Class_Time select get date not null,
Class_Duration char(4) not null,
Studio_ID char (4) not null,
Instructor_ID int not null,
create table class_list(
Class_ID varchar (30) not null,
Member_ID not null,
Date_Booked date not null,
create table Instructor(
Instructor_ID not null,
InsFirstName varchar (30) not null,
InsSurname varchar (30) not null,
InsContactNo char (11) not null,
create table Equipment(
Equip_ID not null,
Supplier_ID not null,
Studio_ID not null,
Equip_Name varchar (30),
create table supplier(
Supplier_ID not null,
Supplier_Name varchar (30) not null,
SupplierContactNo char (11) not null,
Supplier_Email varchar (30) not null,
create table Equipment_Maintanence(
Maintenence_ID not null,
EquipID not null,
Main_date date not null
Maint_ID not null,
Eng_Name varchar (30)
Fault_Desc varchar (200)
Maint_Type varchar (7)
Hi,
I am attempting to create a table in MySQL and am a beginner so please be patient. I am getting syntax error on lines 2, 7, 26, 32. Everything else seems to be error free and unsure. I've spent quite some time figuring out. Any advice thanks.
There are plenty of errors in this sql, for example let's get first table "create table Member_ID(" and its
2nd string:
Member_ID not null - you should add type, for example
int
Member_ID int not null - normal definition for field
7th string
Address 1 varchar (30), not null, - you should use another column name, f.e Address 1 and remove extra comma after varchar (30) and before not null
Address_1 varchar(30) not null - normal definition for field
So I have fixed whole first table and get this code:
create table Member_ID(
Member_ID int not null,
Title varchar(4) not null,
Forename varchar (30) not null,
Surname varchar (30) not null,
DOB date not null,
Address_1 varchar(30) not null,
Address_2 varchar(30) not null,
Postcode varchar(8) not null,
MobileNo char(11) not null,
Email varchar(30) not null,
Gender char(1) not null,
Medical varchar(30) not null,
Joining_Date date not null,
Paid varchar(4) not null,
Membership_Type char(1) not null,
Staff_Initials char(2) not null,
Primary key (Member_ID)
);
you forget to add the datatype for alot of columns like Member_ID not null it should be Member_ID int not null
also if the column name have space you should be like that
`Address 1` varchar(30) not null
not
Address 1 varchar(30) not null
also Postcode varchar(8), not null, here there are extra , it should be Postcode varchar(8) not null,
and there few , and ); you forget to write
this seem work to me
create table Member_ID(
Member_ID int not null,
Title varchar(4) not null,
Forename varchar(30) not null,
Surname varchar(30) not null,
DOB date not null,
`Address 1` varchar(30) not null,
`Address 2` varchar(30) not null,
Postcode varchar(8) not null,
MobileNo char (11) not null,
Email varchar (30) not null,
Gender char (1) not null,
Medical varchar (30) not null,
Joining_Date date not null,
Paid varchar(4) not null,
Membership_Type char(1) not null,
Staff_Initials char (2) not null,
Primary key (Member_ID)
);
create table class(
Class_Name varchar (30) not null,
ClassDayofWeek date not null,
`Class_Time select get` date not null,
Class_Duration char(4) not null,
Studio_ID char (4) not null,
Instructor_ID int not null
);
create table class_list(
Class_ID varchar(30) not null,
Member_ID int not null,
Date_Booked date not null
);
create table Instructor(
Instructor_ID int not null,
InsFirstName varchar (30) not null,
InsSurname varchar (30) not null,
InsContactNo char (11) not null
);
create table Equipment(
Equip_ID int not null,
Supplier_ID int not null,
Studio_ID int not null,
Equip_Name varchar (30)
);
create table supplier(
Supplier_ID int not null,
Supplier_Name varchar (30) not null,
SupplierContactNo char (11) not null,
Supplier_Email varchar (30) not null
);
create table Equipment_Maintanence(
Maintenence_ID int not null,
EquipID int not null,
Main_date date not null,
Maint_ID int not null,
Eng_Name varchar(30),
Fault_Desc varchar (200),
Maint_Type varchar (7)
);

Database functional dependencies

Im trying to find some functional dependencies in my database but have no idea how to do it. Would anyone be able to explain how to do it or what the functional dependencies are . Heres my code for creating my tables
CREATE TABLE Customer(
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber varchar(128) NOT NULL,
UserEmail varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS OrderMaster(
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE OrderItem(
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS Product(
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL,
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS SupplySchedule(
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS Supplier(
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;