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.
Related
Disclaimer: I have only novice knowledge of and experience with databases.
I'm following a Laravel course on Laracasts, and in the database video, the instructor sets the ID column to a type of SERIAL. This is different to how I've seen this done in all other database tutorials, where they will usually check the A_I (auto-increment) checkbox, and this automatically makes the column primary, and leaves the type to be something like INT.
Hovering over the SERIAL type in PHPMyAdmin tells me that it's an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE, but is there ever a particular reason to prefer it over the settings that checking the A_I checkbox sets up? Does either way offer any advantages or disadvantages?
I did find this for PostgreSQL, indicating SERIAL is old and outdated, but I couldn't find an equivalent for MySQL and I'm unsure if the same applies to it.
I'm sure MySQL's SERIAL type was implemented to make it easy for folks who were accustomed to PostgreSQL to have one set of CREATE TABLE statements that would work on both brands of database, and do more or less the same thing.
In an old version of the MySQL manual, it was stated that SERIAL is a compatibility feature (without naming the brand it was intended to be compatible with). The language about compatibility was removed (see https://bugs.mysql.com/bug.php?id=7978).
Now that even PostgreSQL has changed its recommended practice and they use IDENTITY columns instead of SERIAL, the MySQL feature is really unnecessary.
There is no advantage to using SERIAL in MySQL. On the contrary, if you do use it in a CREATE TABLE statement, you will see that the syntax isn't saved. It is just an alias for the BIGINT UNSIGNED AUTO_INCREMENT UNIQUE, as documented.
I find that it's actually wasteful to do this, because I typically declare the auto-increment column as a PRIMARY KEY anyway, and this makes the UNIQUE redundant. So you end up with two unique indexes for no reason.
mysql> create table mytable (id serial primary key);
mysql> show create table mytable\G
*************************** 1. row ***************************
Table: mytable
Create Table: CREATE TABLE `mytable` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`) -- this one is superfluous
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
P.S. This question is almost but not quite a duplicate of What is the difference between SERIAL and AUTO_INCREMENT in 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
);
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.
I am trying to insert an element in a table where there are already 3 rows.
Its a table called usuarios=[id (primary, autoincrement), fid, first_name, last_name....]
So there are already 3 rows with id's: 0,1,2
And when I am trying to execute this query (note I am not setting value for id attribute)
INSERT INTO usuarios (fid,email,pass,first_name,last_name,avatar,bday,fecha,id_loc,id_loc_from)
VALUES (-1,'toni#ideadeia.com','72253f579e7dc003da754dad4bd403a6','','','',NOW(),NOW(),'','')
I get this mysql error:
Duplicate entry '0' for key 1
extra: I don't know how this 3 items where inserted (if via interface, by console query, ..)
So question is, how can I make sure that the Primary Keyis autoincrement, and if not; how to set it? (will that solve the problem?)
You should be able to set it as auto-increment using this statement if you have rights to alter the table:
ALTER TABLE usuarios modify id INT(11) NOT NULL AUTO_INCREMENT;
Although I've seen reports of bugs that recommend instead dropping the colulmn and recreating it, if the above doesn't work for some reason. the syntax recommended in those posts is:
ALTER TABLE usuarios
DROP COLUMN id;
ALTER TABLE usuarios
ADD COLUMN idINT(11) NOT NULL AUTO_INCREMENT FIRST;
Warning: Do this in a test database first. You'd want to be careful doing this if the table is using this as a foreign key. It could fail at best, or break all the relationships at worst.
There's tons of info at the MySql reference manual.
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.