Create a table with a space in the name - mysql

I want to create a table in MySQL with the name "123 Product", with numbers at the beginning and a space in between. I can do this using phpMyAdmin but I want to make this table using PHP. But, unfortunately, I am unable to do this.

You can simply try this: enclose table name with backticks (`)
CREATE TABLE `123 Product`
(
`product_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`product_name` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB;

Related

MySQL Incorrect syntax near 'MODIFY'

someone know what is bad?
ALTER TABLE "stats"
MODIFY "id" int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
Incorrect syntax near 'MODIFY'.
idk what is wrong, someone can help?
Instead of using double quotes use backticks, also an Auto_increment must be PRIMARY KEY
ALTER TABLE `stats`
MODIFY `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, AUTO_INCREMENT=2;
COMMIT;
short answer: instead of " (double quotes ) use ` (backticks)
Long answer :
Backticks are used in MySQL to select columns and tables from your MySQL source. In the example below, we are calling to the table titled Album and the column Title. Using backticks we are signifying that those are the column and table names.
ALTER TABLE `stats`
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
or, The backticks for column names may not be necessary though.
ALTER TABLE stats
MODIFY id int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

The default table name of my table in SQL is table 2 and I cannot rename it [duplicate]

I want to create a table in MySQL with the name "123 Product", with numbers at the beginning and a space in between. I can do this using phpMyAdmin but I want to make this table using PHP. But, unfortunately, I am unable to do this.
You can simply try this: enclose table name with backticks (`)
CREATE TABLE `123 Product`
(
`product_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`product_name` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB;

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.

Issue Converting varchar to nvarchar mysql

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

What is the best way to have a varchar primary key column with an auto-increment?

I would like to create some tables in MySQL. One table would be for users, one for topics, one for comments, and so on.
I need each table to have its own ID column in the following format:
USERS table: ID column
Values:
USR00001
USR00002
USR00003
..
..
USR99999
where as topics table would have IDs like:
TPC00001
TPC00002
TPC00003
similarly, the comments table would have the following IDs:
CMT00001
CMT00002
I tried to use UNIQUE key but did not work: (inspired by this answer)
CREATE TABLE `users` (
`ID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(100) NOT NULL ,
`lastname` VARCHAR(100) NOT NULL ,
`email` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`ID`)
UNIQUE KEY ( 'USR' + `ID`)
);
Can it be done using triggers (Before Insert) maybe?
Please note that I don't want to handle the insertion of the primary keys on the application level. I would prefer the database engine to handle all the work for that.