【问题标题】:MySQL database schema syntax behaving strangelyMySQL 数据库模式语法行为异常
【发布时间】:2017-03-10 20:31:39
【问题描述】:

我正在尝试编写一个 MySQL 数据库(称为 security_db.sql),其中列出了一些关于保安工作的表格。代码本身如下。对奇怪的格式感到抱歉。

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`);

它给我的错误如下。

mysql>使用安全性

数据库已更改

mysql> 源security.db

查询正常,0 行受影响(0.01 秒)

查询正常,1 行受影响(0.00 秒)

数据库已更改

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

查询正常,0 行受影响,1 个警告(0.00 秒)

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 7 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.caller' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 4 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.report' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 6 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.activitylog' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 4 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.number' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 8 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.suspect' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 8 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.writeup' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 7 行的 ')' 附近使用的语法

错误 1146 (42S02):表 'security.officer' 不存在

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 6 行的 'not null primary key, )' 附近使用的语法

ERROR 1146 (42S02): 表 'security.car' 不存在 ERROR 1064 (42000):您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 在第 9 行的 ')' 附近

错误 1146 (42S02):表 'security.ticket' 不存在

无论我如何摆弄语法,我似乎都无法弄清楚。我觉得这完全是菜鸟的错误,但我需要帮助。谢谢。

【问题讨论】:

  • 在每一个 create table 语句的最后一个字段定义之后都有一个额外的逗号,因此没有一个表被创建并且没有一个插入可以成功。您还可以在要错误插入的值周围使用反引号。反引号用于包含对象标识符,例如表或字段名称。字符串值应该用单引号或双引号括起来,数值不应该被括起来。
  • 我觉得你可以这样做很奇怪 --> location varchar(64), );表定义中最后一个字段后的最后一个逗号。
  • 您在reate table activitylog 的开头缺少c。是在真实代码中还是复制错误?

标签: mysql sql


【解决方案1】:

在每个单独的 create table 语句中的最后一个字段定义之后都有一个额外的逗号,因此没有一个表被创建并且没有一个插入可以成功。

例子:

...
location varchar(64) not null, <- this comma

);

您还会在要错误插入的值周围使用反引号。反引号用于包含对象标识符,例如表或字段名称。字符串值应该用单引号或双引号括起来,数值不应该被括起来。

例子:

insert into caller (firstname, lastname, cellnumber, location)
values ('Braydon', 'Rekart', 2174088935, 'Stoddard');

【讨论】:

  • 哦。谢谢!如您所见,我现在只是在学习语法和东西是如何工作的。
  • @BraydonRekart 有关引号和反引号,请参阅 stackoverflow.com/questions/11321491/…
  • 如果上面的回复回答了你的问题,那么请将回复标记为回答,以表明问题已经解决!
  • 一个小问题。我检查并删除了代码中所有最后的逗号,并将反引号更改为单引号。我仍然得到这些错误。 ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 6 行的“not null primary key)”附近使用正确的语法错误 1146 (42S02):表 'security.car' 不存在返回代码时有问题,它似乎很好。
  • 好吧,在not 之前查看导致此错误的语句,您很可能会找到此错误的原因。提示:查看您在其他情况下如何声明 varchar 字段。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 2015-12-12
  • 1970-01-01
相关资源
最近更新 更多