MySQL database schema syntax behaving strangely - mysql
I'm trying to write a MySQL database (called security_db.sql) in which I outline some tables about a security guard job. The code itself is below. Sorry about the weird formatting.
drop database if exists security;
create database security;
use security;
drop table if exists caller;
drop table if exists number;
drop table if exists officer;
drop table if exists suspect;
drop table if exists report;
drop table if exists car;
drop table if exists writeup;
drop table if exists activitylog;
drop table if exists ticket;
create table caller
(
firstname varchar(64),
lastname varchar(64),
cellnumber int unsigned not null primary key,
location varchar(64),
);
insert into caller (firstname, lastname, cellnumber, location)
values (`Braydon`, `Rekart`, `2174088935`, `Stoddard`);
create table report
(
incidentreport varchar(64) not null primary key,
);
insert into report (incidentreport) values (`Testing this database.`);
reate table activitylog
(
report text not null,
date int not null primary key,
location varchar(64) not null,
);
insert into activitylog (report, date, location) values (`Testing this
database`, `112233`, `OlinMahan`);
create table number
(
securityphone int not null primary key,
);
insert into number (securityphone) values (`1217666666`);
create table suspect
(
suspectfirstname varchar(64) not null,
suspectlastname varchar(64) not null,
weight int,
height int,
suspectid int not null primary key,
);
insert into suspect (suspectfirstname, suspectlastname, suspectid)
values (`Braydon`, `Rekart`, `3`);
create table writeup
(
recipientfirstname varchar(64) not null,
recipientlastname varchar(64) not null,
whogaveit varchar(64) not null,
reason text not null,
writeupid int not null primary key,
reason text not null,
writeupid int not null primary key,
);
insert into writeup (recipientfirstname, recipientlastname,
whogaveit, reason, writeupid) values (`Braydon`, `Rekart`, `Karson`, `Late`,
`45`);
create table officer
(
officerid int not null primary key,
position varchar(64) not null,
firstname varchar(64) not null,
lastname varchar(64) not null,
);
insert into officer (officerid, position, firstname, lastname)
values (`1`, `Crewhead`, `Braydon`, `Rekart`);
create table car
(
make varchar(64),
color varchar(64),
model varchar(64),
licenseplatenumber varchar not null primary key,
);
insert into car (licenseplatenumber) values (`N33D4SP33D`);
create table ticket
(
ticketnumber int primary key,
writerfirstname varchar(64) not null,
writerlastname varchar(64) not null,
recipientfirstname varchar(64) not null,
recipientlastname varchar(64) not null,
recipientfirstname varchar(64) not null,
recipientlastname varchar(64) not null,
writerid int,
);
insert into ticket (ticketnumber, writerfirstname, writerlastname,
recipientfirstname, recipientlastname)
values (`3`, `Braydon`, `Rekart`, `Karson`, `Gragert`);
The errors it gives me are as follows.
mysql> use security
Database changed
mysql> source security.db
Query OK, 0 rows affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
Database changed
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
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 ')' at line 7
ERROR 1146 (42S02): Table 'security.caller' doesn't exist
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 ')' at line 4
ERROR 1146 (42S02): Table 'security.report' doesn't exist
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 ')' at line 6
ERROR 1146 (42S02): Table 'security.activitylog' doesn't exist
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 ')' at line 4
ERROR 1146 (42S02): Table 'security.number' doesn't exist
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 ')' at line 8
ERROR 1146 (42S02): Table 'security.suspect' doesn't exist
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 ')' at line 8
ERROR 1146 (42S02): Table 'security.writeup' doesn't exist
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 ')' at line 7
ERROR 1146 (42S02): Table 'security.officer' doesn't exist
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 'not null primary key, )' at line 6
ERROR 1146 (42S02): Table 'security.car' doesn't exist 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 ')' at line 9
ERROR 1146 (42S02): Table 'security.ticket' doesn't exist
No matter how I fiddle around with the syntax I can't seem to figure it out. I feel like it's a total rookie mistake but I need help. Thank you.
You have an extra comma after the last field definition in every single create table statement, therefore none of the tables are created and none of the insert can succeed.
Example:
...
location varchar(64) not null, <- this comma
);
You also use backticks around the values to be inserted incorrectly. Backticks are used to enclose object identifiers, such as table or field names. String values should be enclosed by single or double quotes, numeric values should not be enclosed.
Example:
insert into caller (firstname, lastname, cellnumber, location)
values ('Braydon', 'Rekart', 2174088935, 'Stoddard');
Related
Why am I getting an insert blob error when using MySql v8.0 instead of v5.1?
I created the following table: CREATE TABLE `player__main` ( `row` varbinary(256) NOT NULL, `schema_id` int NOT NULL, `version` int NOT NULL, `format` tinyint NOT NULL, `avro` mediumblob NOT NULL, PRIMARY KEY (`row`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 Then, I run my script below on MySql v5.17: insert into player__main (row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69'); Query OK, 1 row affected (0.05 sec) However, when I execute the code below in MySql v8.0: insert into player__main (row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69'); I get 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 'row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69' at line 1 It's the same query but it returns different results depending of the version of MySql I am using. How can I fix this?
I think the real problem here is row is a reserved keyword in 8: ROW (R); became reserved in 8.0.2 You'll need to rename that column, or escape it going forward: INSERT INTO player_main (`row`, ...)
cannot decipher SQL error when creating trigger
This is for a trigger I'm creating that responds to inserts on the table groceries CREATE TABLE grocery_changes ( ID INT NOT NULL AUTO_INCREMENT, changed_table VARCHAR(255), date_time DATETIME, operation CHAR(10) NOT NULL, PRIMARY KEY(ID) ); CREATE TRIGGER trg_grocery_audit AFTER INSERT ON groceries for each ROW BEGIN INSERT INTO grocery_changes (modified_table, action_date_time, operation) VALUES ('groceries', NOW(), 'INS'); I keep getting this error But I cant figure it out and could really appreciate a second pair of eyes at this point. Thank you ERROR 1064 (42000): 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 '' at line 3
Problems with mysql CREATE TABLE syntax
I try to create table in mysql with the following command: CREATE TABLE keys (id INT(10), key VARCHAR(100)); and it always gives me an error like this: 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 'keys (id INT(10), key VARCHAR(100))' at line 1`
So both the table name keys and the field key are reserved in the Mysql namespace. If you choose a different table name (e.g. keys_tbl) and rename the second field something like key_id, your code will work.
You should be very carefully with table and columns naming, you might face a lot of problems in the future. I suggest find names that are not MySQL reserved words If you still want to keep the names you should put in in backticks. mysql> CREATE TABLE keys (id INT(10), key VARCHAR(100)); 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 'keys (id INT(10), key VARCHAR(100))' at line 1 mysql> CREATE TABLE `keys` (id INT(10), `key` VARCHAR(100)); Query OK, 0 rows affected, 1 warning (0.97 sec) mysql> show create table keys; 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 'keys' at line 1 mysql> mysql> mysql> show create table `keys`; +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+ | keys | CREATE TABLE `keys` ( `id` int DEFAULT NULL, `key` varchar(100) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) Note that you have to use backticks when using the table
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?
I want to make a table and this error comes up. What do I have to fix? mysql> create database mytest; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mytest | | performance_schema | | phpmyadmin | +--------------------+ 5 rows in set (0.00 sec) mysql> use mytest; Database changed mysql> create table price(NAME varchar2(60), PRICE number(10)); 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 'varchar2(60), PRICE number(10))' at line 1
there are more errors in your statement: 1) you must use backticks if you use keywords as field names like name 2) there is no datatype VARCHAR2 use VARCHAR instead 3) there is no datatype NUMBER use INT instead: CREATE TABLE price ( `NAME` VARCHAR(60), `PRICE` INT(10) ); sample mysql> CREATE TABLE price ( -> `NAME` VARCHAR(60), -> `PRICE` INT(10) -> ); Query OK, 0 rows affected (0,03 sec) mysql>
Replace number(10) by INTEGER(10). There is no data type named number in mysql.
Unable to create table with column name "schemas" [duplicate]
This question already has answers here: Is "key" a reserved word in MySqli? I'm getting an error (2 answers) Closed 6 years ago. When I tried to execute following statement. create table user_schemas (user_id varchar(255) not null, schemas varchar(255)); I am getting following 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 'schemas varchar(255))' at line 1 When I change the column name "schemas" to something else, it is working fine. mysql> create table user_schemas (user_id varchar(255) not null, schemas varchar(255)); 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 'schemas varchar(255))' at line 1 mysql> mysql> create table user_schemas (user_id varchar(255) not null, rules varchar(255)); Query OK, 0 rows affected (0.01 sec) Any idea on how to solve this problem??
If you want to use reserved words in the SQL syntax you must use Backticks (`) like: create table user_schemas ( user_id varchar(255) not null, `schemas` varchar(255) );