SQL Query error (create table) - mysql

I want to make a new table but it says my query is wrong, and I have no idea what's wrong with the query because I exported the database from SQLite Database browser.
Here's the query,
CREATE TABLE sqlite_sequence(name,seq);
and it says
#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 'seq)' at line 1
and if i can create that table i wanna insert into that table with this,
INSERT INTO sqlite_sequence VALUES('objek',55);
I hope it wont do another error.
Please tell me your opinion.

The only missing on your DDL is that it lacks datatypes on the columns,
CREATE TABLE sqlite_sequence
(
name VARCHAR(20),
seq INT
);
SQLFiddle Demo
UPDATE 1
MySQL Data Types

The sqlite_sequence table gets generated automatically when you assign a data field of a table to autoincrement (e.g. _id integer primary key autoincrement)
It is impossible to create table called sqlite_sequence manually because "Table Name cannot begin with sqlite_ as the object name reserved for internal use: SQLITE_SEQUENCE"

You need to specify the data types for name and seq.
Try:
create table SQLITE_SEQUENCE ( name varchar(255), seq int );

define data type for fields(columns)
CREATE TABLE sqlite_sequence(name varchar(200),seq int(8));

Here is a normal create table command for reference:
CREATE TABLE demo (id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE DEFAULT 0,name text);
And yeah there's no problem with the INSERT command.

You have to define data type after each table field Like this:
CREATE TABLE sqlite_sequence (
name VARCHAR(45),
seq INT
);

Related

My MYSQL code won't run through a syntax checker

Online SQL syntax checkers have been giving me a lot of guff about my simple SQL code, and I don't understand why...
CREATE DATABASE IF NOT EXISTS DB ;
USE DB ;
CREATE TABLE IF NOT EXISTS teachers(
ID INT NOT NULL AUTO INCREMENT,
LAST_NAME TEXT,
FIRST_NAME TEXT,
COMPUTER_ID TEXT);
I pretty much copied this code from some SQL examples. I'm using mySQL and testing on SQLfiddle.com which is set to use MYSQL 5.6. It says Access denied for user 'user_9_79940c'#'%' to database 'db' if I have the first two lines there and if I remove them it says about the create table that I have a syntax error near AUTO INCREMENT
First of all, you are not allowed to create or switch databases on SQLFiddle. On your own MySQL instance, you might be able to, depending on what permissions you have. This is why the first two lines do not work in the SQLFiddle; the database is pre-created and pre-selected for you, and you are sandboxed in it.
The CREATE TABLE command will throw a syntax error complaining about AUTO INCREMENT - that is not a keyword, it should be written AUTO_INCREMENT.
There is still an error there: "there can be only one auto column and it must be defined as a key". There is only one auto column, but it is not a key yet. So, we'll make it one, by appending PRIMARY KEY. This works:
CREATE TABLE IF NOT EXISTS teachers(
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
LAST_NAME TEXT,
FIRST_NAME TEXT,
COMPUTER_ID TEXT);
Note that PRIMARY KEY will imply NOT NULL, so NOT NULL is unnecessary, and can be deleted without consequence.

Existing table add auto increment primary key - sql

I have an existing product table which is already populated with 120 records and I have tried below SQL query:
ALTER TABLE product ADD product_id
INT PRIMARY KEY AUTO_INCREMENT
But it's given me the error:
Error: #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
Here is work around
create a simple column with autoincrement
browse the table and again make this newly added column as primary key
You are done !
:) Thanks to Guy who is not in Stackoverflow
Friend
If you try to do this in MSSQL then your script will be this.
suppose your table is below
create table product
(
name varchar(20),
product_type varchar(20)
)
and suppose you entered some record in this table after that you want to add a column that name product_id with auto increment and primary key. Then you use this script that is below
alter table product add product_ID int primary key identity(1,1)
suppose this will be help full for you.
There can only be one auto increment field per table. But, you could have a calculated field based on the auto increment field. Or, you could have an int field where you manage the sequence by front end code or by a trigger. And also you could use a sequence in SQL Server.
CREATE SEQUENCE MySequence START WITH 100;
CREATE TABLE MyTable
(
RealIdentity INT IDENTITY(1,1),
RandomCol NVARCHAR(100),
FakeIdentity INT DEFAULT NEXT VALUE FOR MySequence
);
UPDATE - MySQL Way
I just noticed that your question is taged for MySQL. Above answer is for MS SQL. Here's how you'd do the same in MySQL.
How do I create a sequence in MySQL?
MySQL equivalent of Oracle's SEQUENCE.NEXTVAL

How to make a empty MySQL table?

What is wrong with the following syntax? This is the code that MySQL work bench made and I can't tell what is wrong with it.
CREATE TABLE `data1`.`table1` ();
You are trying to create a table without columns. That is not possible in MySQL. You need to specify at least one column in order to create table.
You are missing columns, a table need columns
Propper syntax would be:
CREATE TABLE table1(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
);
Read more about this at w3school
A table is a collection of columns and rows. Without columns, it is not a table. 'An empty variable'- would be the best name.
Check the CREATE TABLE syntax here.

error in applying auto increment by 1 on specific column

I have a table name article and a column name articleid. I want that every time i enter the data in the table then value in column of articleid should be incremented to 1. I am trying to do like this.
ALTER TABLE article MODIFY COLUMN articleid INT auto_increment
But it generates this error statement:
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 '=1' at line 1
Your error message is
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 '=1' at line 1
But the query you've shown us is
ALTER TABLE article MODIFY COLUMN articleid INT auto_increment
Note =1 is not in the query you've shown us, so you are running some code that you are not intentionally trying to run. The syntax for your ALTER TABLE query looks correct.
Update: To ensure your MySQL increment by 1, set the MySQL configuration setting of auto_increment_increment = 1. Note in MySQL, this is a global setting and not a table or column setting.
http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment
A working SQL Statement to accomplish this is:
ALTER TABLE article CHANGE articleid articleid INT NOT NULL AUTO_INCREMENT
Please make sure that you've assigned the Primary key to the column 'articled'
If not, run this statement:
ALTER TABLE article ADD PRIMARY KEY(articled)
Because auto_increment can only be applied to the column that is the primary key of your table.
If it's not working at all, you could just start over by dropping your table (all data is lost!)
DROP TABLE article
And re-create it with all properties and keys assigned properly.
CREATE TABLE article (
articleid int NOT NULL AUTO_INCREMENT,
articletitle varchar(100) NOT NULL,
PRIMARY KEY (articleid)
)

MySQL statement that has identity attribute

I am a MySQL beginner, I followed a simple tutorial on the web, it has such a simple create table statement
create table users(
userID int primary key identity(1,1)
)
I now sort of have an idea of what identity means, however, whenever I typed this statement in MySQL console, it always complains that I have a syntax error.
Could anybody help out? Thanks.
Identity(1,1) is not mysql syntax; use auto_increment instead
You can try with this syntax
CREATE TABLE users(
userID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(userID )
);
Your syntax if proper to sql server
Identity is used to auto-increment the field value for a new row.
Identity(increment, seed)
.
Having said that I don't think mysql supports that at all. I have seen that in sql-server. In mysql auto_increment is used.