I am trying to create a table from my command line (Debian), but it keeps saying I have an error in my syntax. To me it looks fine and I have got it checked by 2 different people who also cannot find the issue.
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT,
uuid VARCHAR(32) NOT NULL,
key VARCHAR(50) NOT NULL
);
One guy said remove NOT NULL but I still had the same issue.
KEY is a reserved word try change with my_key
CREATE TABLE users (id INT( 6) UNSIGNED AUTO_INCREMENT,
uuid VARCHAR(32) NOT NULL,
my_key VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`));
Sorry,
for an AUTO_INCREMENT Field you MUST have a key on this COLUMN.
So this works:
CREATE TABLE `user` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`uuid` varchar(32) DEFAULT NULL,
`key` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MySQL has lots of reserved keywords that cannot be used as column names. Here you are using key as a column name, and since it is a reserved keyword in MySQL, you need to change the name of the column to something that is not a reserved keyword.
You can find a full list of reserved keywords that cannot be used as a column name here.
The column name "key" you used for the third column is a reserved word, all you have to do is change the name.
Well, one probably can't know all the existing keywords in a programming language but one can help himself/herself by using colour-code enabled text editor or Integrated Development Environment (IDE) when writing codes. It helps a lot.
Related
My question may sound vague but am going to try as much as possible to make it clear enough. Prior to this, I have made some research on the internet and other SO pages but to no avail.
Is it possible to use CHAR_LENGTH() function in a CREATE TABLE clause like we usually do in SELECT Clause :
SELECT CHAR_LENGTH("SQL Tutorial") AS LengthOfString;
What I want to do is similar to this:
CREATE TABLE `tbl_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(32) NOT NULL,
`no_of_chars` int(4) NULL DEFAULT CHAR_LENGTH(`content`) ,
PRIMARY KEY (`id`)
)
But the above CREATE statement gives an error near CHAR_LENGTH.
Precisely, During insertion of a record into such a table, I want the server to be able to read the length of the content field and store in no_of_chars field as default value.
Is this POSSIBLE?
Yes, you could use generated column:
CREATE TABLE `tbl_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(32) NOT NULL,
`no_of_chars` int(4) AS ( CHAR_LENGTH(`content`)) ,
PRIMARY KEY (`id`)
);
DBFiddle Demo
please give me recommendation my query does not working
SQL query:
CREATE TABLE `amenities` (
`amenities_id` int(11) NOT NULL auto_increment,
`pic` varchar(100) NOT NULL,
`des` text NOT NULL,
PRIMARY KEY (`amenities_id`)
) TYPE=MariaDB AUTO_INCREMENT=13
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'TYPE=MariaDB AUTO_INCREMENT=13' at line 6
There is no type table option, you possibly want to define the table engine
and there is no mariadb engine try
CREATE TABLE amenities ( amenities_id int(11) NOT NULL auto_increment,
pic varchar(100) NOT NULL, des text NOT NULL, PRIMARY KEY (amenities_id) )
AUTO_INCREMENT=13,
engine=innodb
Or leave out the engine option if you want to default the table to the database engine/
Hope this works.
CREATE TABLE amenities (
amenities_id int(11) NOT NULL auto_increment,
pic varchar(100) NOT NULL,
des text NOT NULL,
PRIMARY KEY (amenities_id)
) AUTO_INCREMENT=13
The TYPE keyword was replaced by ENGINE long ago.
The ENGINEs are InnoDB, MyISAM, MEMORY, ARIA and possibly others. Not MySQL, nor MariaDB.
The error message ... near 'TYPE ... points exactly at or after the offending syntax: TYPE in this case. (Not AUTO_INCREMENT, which was later)
AUTO_INCREMENT=13 is produced by SHOW CREATE TABLE for possible reloading. However, it is rarely useful otherwise. It is also mostly harmless.
DROP TABLE IF EXISTS `amenities`;
CREATE TABLE `amenities` (
`amenities_id` int(11) NOT NULL AUTO_INCREMENT,
`pic` varchar(100) NOT NULL,
`des` text,
PRIMARY KEY (`amenities_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create new Table amenities where amenities_id is a PRIMARY KEY that will be auto increment. another table field pic is a varchar data type and des is a text data type that is used for rich text.
Hi I just followed a YouTube video and created this but it won't let me create the tables how do I fix it?
2 errors were found during analysis .
Unexpected beginning of statement. (near "member_id" at position 24)
Unrecognized statement type. (near "SMALLINT" at position 34)
CREATE TABLES members(
member_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(60) NOT NULL,
phone CHAR(10) NOT NALL DEFAULT'000000000',
membership_status ENUM('gold','silver,''bronze','nam') NOT NULL DEFAULT'nam',
PRIMARY KEY (member_id)
)
You had multiple typos (missing single quote, you wrote NAL instead of NULL). This query will work:
CREATE TABLE `members` (
`member_id` SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(60) NOT NULL,
`phone` CHAR(10) NOT NULL DEFAULT '000000000',
`membership_status` ENUM('gold', 'silver', 'bronze', 'nam') NOT NULL DEFAULT 'nam',
PRIMARY KEY (`member_id`));
Furthermore you may want to look into different tutorials. In my oppinion no one should use ENUMs anymore, because of several disadvantages. Maybe someone can write a good tutorial tip here. Maybe this is something for you: https://www.codecademy.com/learn/learn-sql
CREATE TABLES members(
member_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(60) NOT NULL,
phone CHAR(10) NOT NALL DEFAULT'000000000',
membership_status ENUM('gold','silver,''bronze','nam') NOT NULL DEFAULT'nam',
PRIMARY KEY (member_id)
);
Try by doing this. As it seems, there is no ; mark in the closing.
Try changing create tables to create table.
Sorry if this is an easy question, I am coming to MySQL from SQL Server.
When I execute my create statement it contains nvarchar but commits to the database as varchar. Even in my alter statement afterwards the column does not change at all. Does the collation or DB engine make a difference?
During execution I am not encountering any issues in results, other than the fact the column changes datatype. I attached a screencast of my activity http://screencast.com/t/wc94oei2
I have not been able to find anyone with similar issues through my Google searches
Did you mean, this..
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name nvarchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
----turns to---
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name varchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
I am working with mysql .
I have checked the CREATE table statement , and I saw there a KEY word
| pickupspc | CREATE TABLE `pickupspc` (
`McId` int(11) NOT NULL,
`Slot` int(11) NOT NULL,
`FromTime` datetime NOT NULL,
`ToTime` datetime NOT NULL,
`Head` int(11) NOT NULL,
`Nozzle` int(11) DEFAULT NULL,
`FeederID` int(11) DEFAULT NULL,
`CompName` varchar(64) DEFAULT NULL,
`CompID` varchar(32) DEFAULT NULL,
`PickUps` int(11) DEFAULT NULL,
`Errors` int(11) DEFAULT NULL,
`ErrorCode` varchar(32) DEFAULT NULL,
KEY `ndx_PickupSPC` (`McId`,`Slot`,`FromTime`,`ToTime`,`Head`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
But what is the meaning of it ?
It's not like a PRIMARY KEY right ?
Thanks .
It is simply a synonym for INDEX. It creates an index with the name ndx_PickupSPC on the columns specified in parenthesis.
See the CREATE TABLE syntax for more information.
It's just a non-unique index. From the manual
KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can
also be specified as just KEY when given in a column definition. This
was implemented for compatibility with other database systems.
Key and index are the same. The word Key in the table creation is used to create an index, which enables faster performance.
In the above code, Key ndx_PickupSPC means that it is creating an index by the name ndx_PickupSPC on the columns mentioned in parenthesis.
It's an INDEX on the table. Indexes enable fast lookups for specific queries which check the values of the columns the index is built on. The example uses a compound key.
They are a bit similar to the indexes you find at the end of the books. You can quickly find an entry with the index without searching through the whole book. Databases typically use B-Trees for indexes.