MySQL statement that has identity attribute - mysql

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.

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.

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

SQL Query error (create table)

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

SqlServer create table with MySql like auto_increment primary key

I want to make a table in SqlServer that will add, on insert, a auto incremented primary key. This should be an autoincremented id similar to MySql auto_increment functionality. (Below)
create table foo
(
user_id int not null auto_increment,
name varchar(50)
)
Is there a way of doing this with out creating an insert trigger?
Like this
create table foo
(
user_id int not null identity,
name varchar(50)
)
OP requested an auto incremented primary key. The IDENTITY keyword does not, by itself, make a column be the primary key.
CREATE TABLE user
(
TheKey int IDENTITY(1,1) PRIMARY KEY,
Name varchar(50)
)
They have answered your question but I want to add one bit of advice for someone new to using identity columns. There are times when you have to return the value of the identity just inserted so that you can insert into a related table. Many sources will tell you to use ##identity to get this value. Under no circumstances should you ever use ##identity if you want to mantain data integrity. It will give the identity created in a trigger if one of them is added to insert to another table. Since you cannot guarantee the value of ##identity will always be correct, it is best to never use ##identity. Use scope_identity() to get this value instead. I know this is slightly off topic, but it is important to your understanding of how to use identity with SQL Server. And trust me, you did not want to be fixing a problem of the related records having the wrong identity value fed to them. This is something that can quietly go wrong for months before it is dicovered and is almost impossible to fix the data afterward.
As others have mentioned: add the IDENTITY attribute to the column, and make it a primary key.
There are, however, differences between MSSQL's IDENTITY and MySQL's AUTO_INCREMENT:
MySQL requires that a unique
constraint (often in the form of a
primary key) be defined for the
AUTO_INCREMENT column.MSSQL doesn't have such a requirement.
MySQL lets you manually insert values into an AUTO_INCREMENT column.
MSSQL prevents you from manually inserting a value into an IDENTITY
column; if needed, you can override
this by issuing a "SET
IDENTITY_INSERT tablename ON"
command before the insert.
MySQL allows you to update values in an AUTO_INCREMENT column.MSSQL refuses to update values in an
IDENTITY column.
Just set the field as an identity field.
declare the field to be identity
As advised above, use an IDENTITY field.
CREATE TABLE foo
(
user_id int IDENTITY(1,1) NOT NULL,
name varchar(50)
)
As others have said, just set the Identity option.