I am stuck with Error Code 1604 trying to create a table. The code goes like this:
USE Rudinas;
CREATE TABLE clan (
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
birth_date DATE NOT NULL
sex ENUM("M", "F") NOT NULL,
clan_belong ENUM('EU', 'ASIAPAC', 'USA', 'SLO') NOT NULL,
date_entered TIMESTAMP,
relative_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
How can I get this table to run?
Use comma(,) after birth_date DATE NOT NULL
CREATE TABLE clan (
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
birth_date DATE NOT NULL,
sex ENUM("M", "F") NOT NULL,
clan_belong ENUM('EU', 'ASIAPAC', 'USA', 'SLO') NOT NULL,
date_entered TIMESTAMP,
relative_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
you just missed the , before sex column.
CREATE TABLE clan(
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
birth_date DATE NOT NULL ,
sex ENUM("M", "F") NOT NULL,
clan_belong ENUM('EU', 'ASIAPAC', 'USA', 'SLO') NOT NULL,
date_entered TIMESTAMP,
relative_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
);
There will be something wrong with your statement somewhere. Unfortunately with the smallest of errors the human brain will sometimes autocorrect and it takes some time/energy/luck to discover the problem.
One thing I would suggest is to start paring your statement down until it works, then build it back up again. For example, start by removing half the columns, then add the others back until it either breaks (the problem is here) or it works (you didn't make the same mistake this time). This is in fact a good approach for debugging most code, especially if you cannot step through.
Stepping away for a few minutes and coming back can also help, as can utilizing a second pair of eyes.
In this case you're simply missing a comma:
birth_date DATE NOT NULL
-- ^^^
Related
I tried it with SQL Server and it was working by changing auto_increment into identity, but when it comes to MySQL, I keep getting an error:
create database Students
create table midTBSurname (
id int not null primary key AUTO_INCREMENT,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null)
insert into midTBSurname
(**name**,Sex,birthdate,address,contactno,Course,Yearlevel) VALUES
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1')
Select * from midTBSurname;
1 errors were found during analysis.
Unrecognized statement type. (near "name" at position 305)
Here is the version in SQL Server
create database Students
create table midTBSurname (
id int not null primary key IDENTITY,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null)
Insert into midTBSurname
(name,Sex,birthdate,address,contactno,Course,Yearlevel) values
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1')
Select * from midTBSurname;
This the table in SQL Server:
- id name Sex birthdate address contactno Course Yearlevel
- 1 Ruskin M 2002-08-10 Mandaluyong 422-5267 BSMATH 4
- 2 Tristan M 2001-07-23 Quezon 913-6791 BSN 1
- 3 Therese F 1998-06-19 Quezon 422-5267 BSIT 2
In mysql EVERY statement must be terminated with a semi-colon
drop table if exists midTBSurname;
create table midTBSurname(
id int not null primary key AUTO_INCREMENT,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null);
insert into midTBSurname
(name,Sex,birthdate,address,contactno,Course,Yearlevel) VALUES
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1');
Select * from midTBSurname;
For example
I want to make a table in phpmyadmin and I am using SQL command for that
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
I am getting this error:
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 '
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT ' at line 2
It should be like this
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP);
You are missing KEY after PRIMARY:
CREATE TABLE userdetail (
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
Note that int(255) really doesn't make sense. Are you familiar with the integer data types and what the value in parentheses means? You can review the documentation here.
I am following a tutorial by Derek Banas on YouTube for learning MySQL in One Video. In his tutorial he is using a terminal whereas I am using Oracle MySql Developer, as it's what I use in university and am doing what I can to get ahead in classes, hence why I'm learning from Derek Banas' video! So my problem will be outlined below along with the error and any help/input at all would be greatly appreciated!
In line 8 below, "MEDIUMINT UNSIGNED", Unsigned shows up underlined in red indicating an error. Even when removing this error, it still show's the same error in the output window. I left it in there as it's exactly how Derek Banas does it in his video. (Reference: Derek Banas - MySQL Tutorial)
CREATE TABLE Student(
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(60) NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(40) NOT NULL,
state CHAR(2) DEFAULT "PA" NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
phone VARCHAR(20) NOT NULL,
birth_date DATE NOT NULL,
sex VARCHAR(1) CHECK (sex IN ('M','F')),
date_entered TIMESTAMP,
lunch_cost FLOAT NULL,
student_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
The output from this is as follows;
Error starting at line : 1 in command -
CREATE TABLE Student(
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(60) NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(40) NOT NULL,
state CHAR(2) DEFAULT "PA" NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
phone VARCHAR(20) NOT NULL,
birth_date DATE NOT NULL,
sex VARCHAR(1) CHECK (sex IN ('M','F')),
date_entered TIMESTAMP,
lunch_cost FLOAT NULL,
student_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)
Error report -
SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
No idea what to do from here, spent a while searching around google but found no answer. My guess is it's obviously something to do with Oracle SQL Developer compared to him using the terminal which probably has different formatting rules or something. So yeah as mentioned before, any help appreciated!
This create table statement comes from mysql and you're executing this on oracle DB.
In oracle this would be:
CREATE TABLE Student(
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
email VARCHAR2(60),
street VARCHAR2(50) NOT NULL,
city VARCHAR2(40) NOT NULL,
state CHAR(2) NOT NULL, --DEFAULT "PA"
zip number NOT NULL,
phone VARCHAR2(20) NOT NULL,
birth_date DATE NOT NULL,
sex VARCHAR2(1), --CHECK (sex IN ('M','F')),
date_entered TIMESTAMP,
lunch_cost number,
student_id number NOT NULL,
CONSTRAINT "STUDENT_PK" PRIMARY KEY ("STUDENT_ID"),
CONSTRAINT "STUDENT_CHK1" CHECK (sex in ('M','F')) ENABLE);
Auto increment in Oracle is done by:
create sequence studen_seq start with 1 increment by 1;
create or replace trigger student_trg
before insert on student for each row
declare
begin
:new.student_id := studen_seq.nextval;
end;
The syntax that you are using for creating the table is of MySQL DB.
The MySQL Developer that you are using must be using Oracle DB which do not recognize the MySQL syntax.
I am creating tables where I have foreign keys. This is part of the statements. For some reason I cannot get it to work.
What am i doing wrong?
CREATE TABLE Doctor (
NPI NUMBER PRIMARY KEY NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN NUMBER PRIMARY KEY NOT NULL,
INSURANCE_POLICY_ID NUMBER NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) REFERENCES INSURANCE (INSURANCE_POLICY_ID));
You have tagged your question with SQL Server and MySql both, in both of these RDBMS there is no data type called Number but there is a data type to store numbers it is called INT or INTEGER.
Therefore your table definitions should be as follow:
CREATE TABLE Doctor (
NPI INT NOT NULL PRIMARY KEY ,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE INT NOT NULL, --<-- should be a varchar since most phone numbers have a leading zero
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN INT NOT NULL PRIMARY KEY,
INSURANCE_POLICY_ID INT NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE INT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID)
REFERENCES INSURANCE (INSURANCE_POLICY_ID));
Following is my query:
CREATE TABLE report_invoice(
'ID' INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoice_Type' INT(10) NOT NULL,
'Operator' VARCHAR(50) NOT NULL,
'Customer' VARCHAR(50) NOT NULL,
'Invoice_Date' DATE NOT NULL,
'Total' DECIMAL('10,2'),
PRIMARY KEY ('ID'));
I keep getting this error:
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 ''ID'
INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoic' at line 2
You're using single quotes around your field names, use backticks instead:
CREATE TABLE report_invoice(
`ID` INT(10) NOT NULL AUTO_INCREMENT,
`Invoice_No` VARCHAR(30) NOT NULL,
`Invoice_Type` INT(10) NOT NULL,
`Operator` VARCHAR(50) NOT NULL,
`Customer` VARCHAR(50) NOT NULL,
`Invoice_Date` DATE NOT NULL,
`Total` DECIMAL(10,2),
PRIMARY KEY (`ID`));
Don't use simple quotes
You may replace them with backticks, or suppress them.
They would be usefull if you want to use reserved keywords as column names (bad idea anyway), or completely numeric column names, or special characters in column names.
So not in your case.
Don't put quotes around decimal scale and precision, too.
So this would do the job.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
Remove the ticks or replace them with back ticks. Same for numbers.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
CREATE TABLE report_invoice(
Id INT IDENTITY PRIMARY KEY,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL );