我正在尝试构建数据库,但我的 SQL 文件无法执行

问题描述 投票:0回答:1

我尝试在 SQL Server Management Studio 中运行以下相当简单的查询,但出现很多不正确的语法错误。 例如:

消息 102,第 15 级,状态 1,第 1 行
“=”附近的语法不正确。
消息 102,第 15 级,状态 1,第 22 行
'`' 附近的语法不正确。

我该如何解决这个问题?

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `restaurant`
--

-- --------------------------------------------------------

--
-- Table structure for table `items`
--

DROP TABLE IF EXISTS `items`;
CREATE TABLE IF NOT EXISTS `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `category` varchar(100) NOT NULL,
  `price_per_quantity` double NOT NULL,
  `quantity` int(3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `items`
--

INSERT INTO `items` (`id`, `name`, `category`, `price_per_quantity`, `quantity`) VALUES
(1, 'Mars Bar', 'Dairy', 249, 45),
(2, 'Pepsi Can', 'Carbonated Drink', 135, 178);

-- --------------------------------------------------------

--
-- Table structure for table `payments`
--

DROP TABLE IF EXISTS `payments`;
CREATE TABLE IF NOT EXISTS `payments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `method` varchar(11) NOT NULL,
  `total_amount` double NOT NULL,
  `balance` float NOT NULL,
  `time` timestamp NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `payments`
--

INSERT INTO `payments` (`id`, `method`, `total_amount`, `balance`, `time`) VALUES
(1, 'cash', 234.5, 13, '2020-10-12 05:08:19'),
(2, 'cash', 500.5, 67, '2020-10-10 12:59:13'),
(3, 'Card', 234, 3, '2020-12-31 07:00:45');

-- --------------------------------------------------------

--
-- Table structure for table `sales`
--

DROP TABLE IF EXISTS `sales`;
CREATE TABLE IF NOT EXISTS `sales` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `year` varchar(50) NOT NULL,
  `items sold` int(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;



INSERT INTO `sales` (`id`, `year`, `items sold`) VALUES
(1, '2018', 34905),
(2, '2019', 43899);

-- --------------------------------------------------------


DROP TABLE IF EXISTS `type`;
CREATE TABLE IF NOT EXISTS `type` (
  `Name` varchar(10) NOT NULL,
  `Number` int(2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



INSERT INTO `type` (`Name`, `Number`) VALUES
('Admin', 0),
('Cashier', 1),
('Chef', 2),
('Waiter', 3),
('Rider', 4);

-- --------------------------------------------------------


DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(100) NOT NULL,
  `lastname` varchar(100) NOT NULL,
  `emailaddress` text NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO`users`(`id`,`firstname`,`lastname`, `emailaddress`, `username`, `password`) VALUES
(1, '', '', '', 'user1', 'pass'),
(4, 'a', 'a', 'a', 'a', 'a');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

我从 open git 中获取了这段代码,并尝试为餐厅创建数据库。

sql sql-server syntax-error
1个回答
-1
投票

我会在两个地方更改代码。首先是顶部的那个。我认为它不喜欢双引号

SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO';
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = '+00:00';

现在,对于下一个修复,找到每个 ` 并将其替换为 ' 。这应该可以解决你的问题。

如果您想解析查询并确保其正确,您可以使用 SQL Server Management Studio 中的解析实用程序并按 CTRL + F5,如此处所述 - https://www.sqlshack.com/sql-syntax-checker-tools/

这应该可以帮助您了解查询是否正确,因为它将突出显示查询中的问题。

© www.soinside.com 2019 - 2024. All rights reserved.