Error 1064(42000) Creating a Table in msql5 - sql

CREATE TABLE Message(
MessageID int unsigned not null auto_increment primary key,
naiveUserID int unsigned(7) NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
);
I keep trying to run this simple create table blurb on a Mysql5 database and I keep getting the 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 '(7) NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
)' at line 3
I've googled the proper syntax from several different sites and can't make anything run! The maddening part is that when I use my teacher's code, which is very similar to this apart from some data types, it runs perfectly:
CREATE TABLE EMPL (
Eid int unsigned not null auto_increment primary key,
Name varchar(20),
title varchar(30),
salary int,
emailsuffix varchar(60)
);

It's because your syntax is indeed wrong: there's no int unsigned(7)type; try this:
CREATE TABLE Message(
MessageID int unsigned not null auto_increment primary key,
naiveUserID int(7) unsigned NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
);
or:
CREATE TABLE Message(
MessageID int unsigned not null auto_increment primary key,
naiveUserID int unsigned NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
);

Related

I am creating a table in pgweb Heroku, and get this error "ERROR: pq: syntax error at or near "(""

Here is my exact query
CREATE Table Publisher
(Publisher_Id Int primary key not null,
Name varchar(20) not null,
Address varchar(50) not null,
Phone Int(10),
Isbn varchar(13) references books (Isbn) not null
)
Any help would be greatly appreciated.
datatype int does not take a length. So:
create table publisher (
publisher_id int primary key,
name varchar(20) not null,
address varchar(50) not null,
phone int,
isbn varchar(13) references books (isbn) not null
);
Notes:
not null is redondant on a primary key column
int does not seem like a good pick for a phone number; you would typically need to store leading 0s, or allow special characters such as + or () - int cannot do that. A string datatype would probably be a better pick

Error 1064 sql phpmyadmin

This is my sql query:
CREATE TABLE estados (
id int IDENTITY(1,1) PRIMARY KEY,
nombre VARCHAR(20) NOT NULL,
paridad int NOT NULL
);
and it keeps telling me:
#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 '(1,1) PRIMARY KEY,
nombre VARCHAR(20) NOT NULL,
paridad int NOT NULL
I don't know why is this, I am using 10.1.10-MariaDB. I don't know why I have syntax error, and if this has to do with the versions.
IDENTITY is for SQL Server. You should use AUTO_INCREMENT instead:
CREATE TABLE estados (
id int AUTO_INCREMENT PRIMARY KEY,
nombre VARCHAR(20) NOT NULL,
paridad int NOT NULL
);

What is breaking my sql program? Incorrect syntax near (

This code section
CREATE TABLE AIRPORT (
Airport_Code int NOT NULL,
City varchar(20) NOT NULL,
State varchar(20) NOT NULL,
Name varchar(25) NOT NULL,
CONSTRAINT PK_AIRPORT PRIMARY KEY (Airport_Code)
)
Is almost the same as this one but this one is giving me an error and I can't figure out how to fix it
CREATE TABLE AIRPLANE_TYPE (
Company varchar(20) NOT NULL,
Typename varchar(20) NOT NULL,
Max_seats int NOT NULL,
CONSTRAINT PK_AIRPLANE_TYPE (Typename) //error is here (Typename)
)
I am getting the error Incorrect syntax near '('.
Adding the primary key inline with the column will give you a crap auto-generated name for your primary key. I also don't understand how "encouraging primary keys to be on one column" is an added benefit.
CREATE TABLE AIRPLANE_TYPE (
Company varchar(20) NOT NULL,
Typename varchar(20) NOT NULL,
Max_seats int NOT NULL,
CONSTRAINT PK_AIRPLANE_TYPE PRIMARY KEY (Typename)
)
You are missing the PRIMARY KEY. I have a preference for putting this directly in the column definition:
CREATE TABLE AIRPLANE_TYPE (
Company varchar(20) NOT NULL,
Typename varchar(20) NOT NULL PRIMARY KEY,
Max_seats int NOT NULL
);
In addition to a shorter definition, this encourages all primary keys to be only one column -- an added benefit.

What's wrong with my SQL code? Getting an error on line 9

I'm trying to import the code below into a MySQL database. However it says that I have an error on line 9 involving the ')' token. Any idea how to fix it? Error message is:
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 ')' at line 9
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 ')' at line 6
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 ')' at line 10
CREATE TABLE user_types (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(64),
description VARCHAR(255),
rank INT UNSIGNED NOT NULL DEFAULT 1
);
CREATE TABLE users (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(64),
email VARCHAR(64) NOT NULL,
password VARCHAR(255) NOT NULL,
ip_address VARCHAR(255),
user_agent VARCHAR(255),
type INT UNSIGNED NOT NULL DEFAULT 1,
first_name VARCHAR(64),
last_name VARCHAR(64),
biography TEXT,
dob_month INT UNSIGNED,
dob_year INT UNSIGNED,
dob_day INT UNSIGNED,
interests VARCHAR(255),
gender VARCHAR(64),
created INT UNSIGNED NOT NULL
);
CREATE TABLE sessions (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
ip_address VARCHAR(255),
user_agent VARCHAR(255),
created INT UNSIGNED NOT NULL,
last_activity INT UNSIGNED NOT NULL DEFAULT 0
);
Several CREATE TABLE statements have a trailing comma after the last column, which should be removed.
For example:
CREATE TABLE announcements (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
title VARCHAR(255),
content TEXT,
labels VARCHAR(255),
created INT UNSIGNED NOT NULL,
archived INT(1) NOT NULL DEFAULT 0,
);
Should be:
CREATE TABLE announcements (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
title VARCHAR(255),
content TEXT,
labels VARCHAR(255),
created INT UNSIGNED NOT NULL,
archived INT(1) NOT NULL DEFAULT 0
);
With these changes the schema is building successfully on SQL Fiddle.

Error with auto_increment while conneted to Postgres via psql and puTTY

I'm getting this error in puTTY. Not sure why, looks right to me ...
psql:pierre.sql:10: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
^
psql:pierre.sql:18: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
--DROP TABLE customer, reservation;
CREATE TABLE customer(
c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_ref VARCHAR(30) NOT NULL,
f_name VARCHAR(30) NOT NULL,
l_name VARCHAR(30) NOT NULL,
address VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(11) NOT NULL
);
CREATE TABLE reservation(
r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_id VARCHAR(30) NOT NULL REFERENCES customer(c_id),
book_date DATE NOT NULL CHECK (book_date <= now()),
s_time DOUBLE NOT NULL,
e_time DOUBLE NOT NULL,
amount INTEGER NOT NULL
);
Any ideas why?
auto_increment looks like something you'd use with MySQL.
But, here, it seems you are using PostgreSQL.
According to the datatype serial section of the manual, postgresql's equivalent of auto_increment is serial or bigserial.
Quoting that page :
The data types serial and bigserial are not true types, but merely
a notational convenience for setting up unique identifier columns
(similar to the AUTO_INCREMENT property supported by some other databases).
In Postgres 10 or later consider an IDENTITY column:
CREATE TABLE customer(
c_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
...
(PRIMARY KEY also makes it NOT NULL automatically.)
Details:
Auto increment table column
In Postgres 9.6 or older consider a serial like Pascal already suggested.
Works in pg 10 or later, too, but IDENTITY is generally superior.