【发布时间】: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。是在真实代码中还是复制错误?