MySQL - SQLite - Converting is spitting out SET error - mysql

I am trying to convert the following sql into sqlite via sqlite3.exe but it keeps giving me this error:
Error: near line 1: near "SET": syntax error
I'm not entirely sure what this means or why. Here is my sql script:
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO',
time_zone = '+00:00';
CREATE TABLE `px` (
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `px`
ADD PRIMARY KEY (`id`);
ALTER TABLE `px`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
I'm sure i'm overlooking a simple syntax error. Any help would be much appreciated!

You can 't just run a MySQL script in SQLite. These are two different databases, whose syntax differ.
In SQLite, a relatively close syntax to the MySQL script would be:
CREATE TABLE `px` (
`x` integer NOT NULL,
`y` integer NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT
);

Related

How to create mysql table with column timestamp default current_date?

I need to create mysql table with default value on column CURRENT_DATE()
I try
DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_DATE(),
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
but it is writting an error
Query: CREATE TABLE `visitors` ( `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `ip` VARCHAR(32) NOT NULL, `browser` VARCHAR(500) NO...
Error Code: 1064
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 'CURRENT_DATE() NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1' at line 7
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.063 sec
what is the problem?
I need to uniques only date
not with full time info...
can you help me?
Use CURRENT_TIMESTAMP function instead of CURRENT_DATE() function
Try this:
DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
you just have to replace CURRENT_DATE() by NOW() in your query.
I tried it and it's look ok.
This may be a little late but I think it might be helpful to someone else.
My approach has been to use getdate() like this:
[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_myTable_TimeStamp] DEFAULT (getdate()).
Where [TimeStamp] is the column in question.
The result is for example: 2017-11-02 11:58:34.203
To trim this, I use the following
declare #mydate datetime
set #mydate = '2017-11-02 11:58:34.203'
SELECT try_convert(nvarchar(20), #mydate, 120)
This final result is: 2017-11-02 11:58:34
You can actually set this in MSSQL Management Studio

Error when importing schema.sql

I'm new to sql, currently I am using phpMyAdmin via XAMP, I think that uses mysql so correct me if I'm wrong by saying that. Anywhos, I'm trying to import a schema.sql data file into my database I created called "test" but I got an error upon importing it:
It says
Import has been successfully finished, 1 queries executed.
(schema.sql)
But then it also gives me an error message:
CREATE TABLE `population` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`population` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
MySQL said: Documentation
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
and :
Notice in .\import.php#704 Undefined variable: import_text Backtrace
I'm not sure what the issue is. The database I created is completely and has nothing in it.
Column id is the auto-column in question; auto-columns need to be defined as a key, for example as a unique key or a primary key. In your case, a primary key is a good idea because - well, it's your id-column.
CREATE TABLE `population` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`population` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

How can I create a trigger for table logs using mysql?

Can you help me with my code? Because I am creating a table logs for my project. So I include a TRIGGER in my table but there's an error with my sql code. Here's my sql codes.
CREATE TABLE `sales_category` (
`salescatid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`salescatname` VARCHAR(128) NOT NULL,
`salescatdesc` VARCHAR(512) NOT NULL,
UNIQUE INDEX `salescatname` (`salescatname`),
UNIQUE INDEX `salescatid` (`salescatid`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
CREATE TABLE `category_log` (
`action` ENUM('CREATE','UPDATE','DELETE') NULL DEFAULT NULL,
`id` INT(10) UNSIGNED NOT NULL,
`salescatname` VARCHAR(255) NOT NULL,
`salescatdesc` VARCHAR(255) NOT NULL,
INDEX `id` (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
//Here's the error: SQL error 1054: Unknown column 'id' in 'NEW'
DELIMITER #
CREATE TRIGGER ai_category
AFTER INSERT ON sales_category
FOR EACH ROW
BEGIN
INSERT INTO category_log(action,id,salescatname,salescatedesc)
VALUES('CREATE',NEW.id,NEW.salescatname,NEW.salescatdesc);
END;#
Please help me with this thanks. I can't spot where did I go wrong with my code.
Ok i found my error. I just rename the fields in category_log exactly the sa me as my sales_category table.

ERROR 1064 (42000): You have an error in your SQL syntax;

I have a MySQL commands:
CREATE DATABASE IF NOT EXISTS courses;
USE courses
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VAR_CHAR(50) NOT NULL,
addr VAR_CHAR(255) NOT NULL,
phone INT NOT NULL,
);
When I run it, I get an 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 'VAR_CHAR(50) NOT NULL, addr VAR_CHAR(255) NOT
NULL, phone INT NOT NULL, )' at line 3
It is varchar and not var_char
CREATE DATABASE IF NOT EXISTS courses;
USE courses;
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
addr VARCHAR(255) NOT NULL,
phone INT NOT NULL
);
You should use a SQL tool to visualize possbile errors like MySQL Workbench.
Try this:
Use back-ticks for NAME
CREATE TABLE `teachers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`addr` varchar(255) NOT NULL,
`phone` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Use varchar instead of VAR_CHAR and omit the comma in the last line i.e.phone INT NOT NULL
);. The last line during creating table is kept "comma free".
Ex:- CREATE TABLE COMPUTER
(
Model varchar(50)
);
Here, since we have only one column ,that's why there is no comma used during entire code.

Mysql throws an error when one table name is not surrounded by single quotes

I'm building a simple app which lists teams and matches. The Team and Match databases were built with the following scripts (I'm using PhpMyadmin):
CREATE TABLE IF NOT EXISTS `Team` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(120) NOT NULL,
`screen_name` varchar(100) NOT NULL,
`sport_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `Match` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sport_id` int(11) NOT NULL,
`team_one_id` int(11) NOT NULL,
`team_two_id` int(11) NOT NULL,
`venue` varchar(80) NOT NULL,
`kick_off` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
If i do:
SELECT * FROM Team
The script runs and I get an empty result. But, incredibly, if I do
SELECT * FROM Match
I get the following error:
1064 - 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 'Match' at line 1
Instead, I have to do:
SELECT * FROM `Match`
And it works. I have other tables in the database but this is the only behaving like this. Any ideas why?
match is a reserved word in SQL
Read more here:
https://drupal.org/node/141051
Match is a Function in MySQL therefore you must put the quotes around it.
You need encapsulate it in quotes because Match is a keyword.
See key word list