Unexpected beginning of statement - mysql

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.

Related

How to use CHAR_LENGTH() in CREATE TABLE query - MYSQL

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

Error while creating a MySql table

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.

What am I missing? MySQL syntax error

I am having this 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 ''tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'd' at line 1
From this statement:
CREATE TABLE 'tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
); ENGINE=MyISAM;
Why?
You need the backtick instead of the single quote ('). The backtick is this character:
`
Better yet - don't bother with either:
CREATE TABLE tablename (
id MEDIUMINT ...
Important: also see the comments below from tadman; they round out this answer nicely by explaining the backticks and pointing out another syntax issue.
You are using incorrect notation.
In your create table statement, you are using the single quote ( ').
You can't use that here for table names and column names.
Alternatives would be the tick mark (`). Or just completely remove all notation altogether.
Here is your code, fully functional:
CREATE TABLE tablename (
`id` MEDIUMINT NOT NULL,
`content`TEXT NOT NULL,
`date_added` DATETIME NOT NULL,
`user` VARCHAR (16) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`id`)
);
You are missing a space between the columnname and the type of the first two columns. Also, you have a ; to many at the end
CREATE TABLE 'tablename'(
'id' MEDIUMINT NOT NULL AUTO_INCREMENT,
'content' TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
) ENGINE=MyISAM;

Mysql Truncated incorrect DOUBLE value in INSERT query

I know there are lots of bugs like this around here but this query seems to be different, as it's an insert query. Here is the schema for the table card_info:
CREATE TABLE card_info (
card_id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
card_name_orig varchar(150) NOT NULL,
card_name_html varchar(150) NOT NULL,
card_name_search varchar(150) NOT NULL,
card_name_page varchar(150) NOT NULL,
card_cost varchar(50) DEFAULT NULL,
card_cost_converted tinyint(2) NOT NULL DEFAULT '0',
card_subtype varchar(75) DEFAULT NULL,
card_oracle_text_orig text,
card_oracle_text_html text,
card_power varchar(10) DEFAULT NULL,
card_toughness varchar(10) DEFAULT NULL,
card_loyalty tinyint(1) DEFAULT NULL,
PRIMARY KEY (card_id),
KEY card_name_nd (card_name_search),
KEY card_name_page (card_name_page),
KEY card_cost_converted (card_cost_converted),
KEY card_power (card_power),
KEY card_toughness (card_toughness),
KEY card_loyalty (card_loyalty),
FULLTEXT KEY card_oracle_text_orig (card_oracle_text_orig),
FULLTEXT KEY card_name_search (card_name_search)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
And here is my query:
INSERT INTO card_info (
card_name_orig, card_name_html, card_name_search, card_name_page, card_cost,
card_cost_converted, card_subtype, card_oracle_text_orig, card_oracle_text_html,
card_power, card_toughness, card_loyalty
)
SELECT DISTINCT
d.name_orig, d.name_html, d.name_search, d.name_page, d.cost,
COALESCE(d.cost_converted, 0), d.type_sub, d.oracle_text_orig,
d.oracle_text_html, d.`power`, d.toughness, d.loyalty
FROM card_info_de d
LEFT OUTER JOIN card_info i ON d.name_search = i.card_name_search
WHERE i.card_id IS NULL
AND d.edition_id = 'isd'
ORDER BY (d.collector_number + 0), d.collector_number;
If I perform this query, I'm getting this error:
1292 - Truncated incorrect DOUBLE value: '181a'
Please note that the value 181a is from the column card_info_de.collector_number and it is a VARCHAR(5) field, and that field isn't being inserted to the card_info table anyway, it's just being used in the order clause of the select query.
If I do the query starting from the SELECT only, I can see the correct results are being selected, but when I do the insert, it gives me the error above. Do note that If I remove the ORDER BY clause from the SELECT query, it inserts fine. I don't have a clue what I'm doing wrong.
Thanks in advance.
Just hit the same error myself and it is a bit misleading. I found the answer in another thread:
Error Code 1292 - Truncated incorrect DOUBLE value - Mysql
This message means you're trying to compare a number and a string in a WHERE or ON clause. Either make sure they have similar declarations, or use an explicit CAST to convert the number to a string.
If you turn off strict mode, the error should turn into a warning.
Barmar
So basically check for any of these columns being treated as the wrong data type:
d.name_search
i.card_name_search
d.edition_id
d.collector_number
Try
ORDER BY (d.collector_number + '0'), d.collector_number;
Terrible if true, but quite possible.

MySQL Hash Value as Column Name

I'm getting a syntax error when creating a table with the result of PHP's md5(microtime()) as the column name.
In particular, the error is getting thrown at the part with ** surrounding it:
CREATE TABLE form_data_38 (
id INT SIGNED auto_increment NOT NULL,
rltd_pri_key INT SIGNED NULL,
0accc77c084cc74a51dee479f8d095e3 TEXT(65535) NOT NULL,
**092e60b78f7804e86ea9a6e83701a929 TEXT(65535) NOT NULL**,
6734131796201537410e4d43635bf1b3 TEXT(65535) NULL,
PRIMARY KEY (id)
) TYPE=InnoDB;
What is confusing me is why it's being thrown at that spot and not the other 2 hash values prior to it. I appended 'a' to the column names and it created the table no problem. I've searched MySQL naming rules and so far I haven't come up with anything. It just says all alphanumeric characters plus '_' and '$' are okay to use, which should be fine in this instance.
What am I missing?
Put your table and field names in backquotes: `09e4_fieldName`, at least for those that can create such problems:
CREATE TABLE form_data_38
( id INT SIGNED auto_increment NOT NULL
, rltd_pri_key INT SIGNED NULL
, `0accc77c084cc74a51dee479f8d095e3` TEXT(65535) NOT NULL
, `092e60b78f7804e86ea9a6e83701a929` TEXT(65535) NOT NULL
, `6734131796201537410e4d43635bf1b3` TEXT(65535) NULL
, PRIMARY KEY (id)
)
ENGINE = InnoDB ; --- ENGINE, not TYPE