Can we have the table name as "option" in MySQL? - sql

I am very, very new to MYSQL.I tried to create a table named "option".
My SQL Query is :
create table option(
id int not null primary key auto_increment,
choice varchar(30)
)
While executing this query it shows the following error
Error Code : 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 'option(
id int not null primary key auto_increment,
choice varchar(30)
)' at line 1
(0 ms taken)
If I try with the table name as "choice" it is working.
can we have the table name as "option" in mysql?
thanks

If you want to have a table name Option, you should be able to, just remember that whenever you use the table in a query, you will have to encase it in ` symbols. Like this.
`option`
The ` key on the top left of your keyboard, with the tilde.

Pick a different name (one that isn't a reserved word in your RDBMS) and save yourself and whoever else might work on it many headaches.

option is a reserved word in Mysql.we can use a reserved word by using the word inside a single quotes.

Better you select the other tablename.Ohterwise maintaining our code will be difficult.

You can use SQL keywords as table names in MySQL if you escape them with back-quotes.
CREATE TABLE `option` (
...
)
It's not normally a good idea to do so, though.

option is a reserved word in MySQL. Save yourself a world of pain and use choice for your table name.

See the MySQL documentation on this. You can do it as follows:
create table `option` (
...
)

Yes you can definitely create a table named option but in every query you will have to use
`option`
instead of plain option. Better improvise a little and create a table named options to save from trouble. Restrain from using mysql reserved words as table name or column name or procedure names.

Related

Using a bit column in a where clause

I am writing a very simple query where I need to retrieve the records based on a column that has two values: 0 and 1. I didn't realize this column has a bit type, therefore when I write the query SQL Server is giving me the message
"Incorrect syntax near the keyword 'Primary'."
My query is simple:
select * from [table name]
where Primary = '1'
I've tried searching the site but couldn't find a good answer. BTW, I only have access to retrieve the data from the table. I can't declare variables or create a stored procedure or any of that stuff. Surely this can't be that complicated. Please assist!
PRIMARY is reserved word and it needs to be quoted:
select * from [table name] where [Primary] = 1;
The problem has nothing to do with the datatype being a bit, the error, in fact, is telling you exactly what the problem is:
Incorrect syntax near the keyword 'Primary'.
Emphasis added.
PRIMARY is a reserved keyword. Ideally don't use keywords for object names, but if you "must" then you must delimit identify the object. In fact, really you should avoid any names for objects that require delimit identifing:
SELECT {Your Columns} --Define the columns, don't use *
FROM dbo.[Table Name] --I hope you don't have white space in your object names too
WHERE [Primary] = 1; --Don't wrap bit/numerical values in quotes.
That's because primary is a reserved word in SQL Server. You would need to quote it:
select * from [table name] where [Primary] = 1
I would warmly recommend changing the column name so it does not conflict with a langauge keyword. There are less than 200 reserved works in SQL Server, which leaves a lot of room for alternatives.
So:
sp_rename '[table name].[primary]', 'prim', 'COLUMN';

How to implement searching concept using keywords in SQL

Can anyone guide how to implement the keyword search concept in SQL.Is there only “Like” operator available or any other options there?
Example: - Table:
Keyword -- Questions -- Answers
Where,pen -- Where is my pen? -- Pen is on the table
Where ,pen, kept -- where you have kept the pen -- Pen is under the
table
Input - Where is my pen?
Output should be - “Pen is on the table”
In this part I have to split the keyword from input and check the keyword with db to send the output. I should get only one output from above .Because in second row value also have “where,pen” keyword
Could anyone suggest how this logic will be implemented .
I would like to know If any keyword creating option can done in SQL part itself.
You do not want to store the keywords in a comma separated list. Instead, you want a separate table, say QuestionKeywords:
create table QuestionKeywords (
QuestionKeywordsId int identity(1, 1) primary key,
QuestionId int not null references Questions(QuestionId),
Keyword varchar(255),
constraint unq_QuestionKeywords_QuestionId_Keyword unique (QuestionId, Keyword)
);
You can then search for keywords just using equality or in.

Error message when trying to import db.sql into my database on phpMyAdmin?

I'm using phpMyAdmin to hold my databases.
I made a database and I pressed the "import" button.
After that, I chose my file db.sql
And then I clicked the go button.
Instead of importing my file, this error message comes up:
Error
SQL query:
TABLE structure FOR TABLE `category` --
CREATE TABLE `category` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`category` VARCHAR( 30 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =9;
MySQL said:
#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 'Table structure for table category
CREATE TABLE category (
id int(1' at line 1
What does all of this mean?
And how do I get my file to import to the database?
How do I fix this?
I'm trying to open up an ebook that I need to read, and I can't find it anywhere else.
Thanks, I really appreciate your help.
I'd expect the first line to be a comment (start with --). The lack of the comment delimiter combined with what isn't really an SQL statement are causing your error message since MySQL doesn't know what to do with the line TABLE structure FOR TABLE `category`.
Where did this SQL statement come from? Either there's a bad copy/paste or the original source is missing the comment part.
Either remove the first line or add the two dashes to make MySQL ignore it as a comment:
-- TABLE structure FOR TABLE `category` --
Also, the trailing -- is pretty much ignored and probably the result also of a bad copy/paste.

How to use sql def variable

I'm trying to declare a variable in a simple sql script to create some tables in a schema, but it's not working.
Here's a snippet of my sql script:
DEF SCHEMA_NAME = MY_SCHEMA;
CREATE TABLE &SCHEMA_NAME.BOOK
(
BOOK_ID INTEGER
);
That's creating a table with the table name MY_SCHEMABOOK instead of a table named BOOK in the schema MY_SCHEMA.
The script output in Oracle Sql Developer says:
old:CREATE TABLE &SCHEMA_NAME.BOOK
(
BOOK_ID INTEGER
)
new:CREATE TABLE MY_SCHEMABOOK
(
BOOK_ID INTEGER
)
table MY_SCHEMABOOK created.
If that helps. Also, it seems that changing it to &SCHEMA_NAME..BOOK does make it work like I want it to, but having to put two periods doesn't make sense to me. Can anyone tell me what I'm doing wrong?
SET CON[CAT] {. | c | ON | OFF}
Sets the character used to terminate a substitution variable reference
when SQL*Plus would otherwise interpret the next character as a part
of the variable name.
SQL*Plus resets the value of CONCAT to a period when you switch CONCAT on.
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm#sthref2722
SQL Developer works the same way - your first period is interpreted as a substitution variable terminator. So, it is perfectly valid to use two consequent periods in this case.

Select query to retrieve the value of primary key for a specific row in a table

I am struggling to retrieve the value of primary key for a table. We are using MS SQL Server 2005. The database was designed years back by somebody else (he didn't follow the normalization rules at all). He used Key (which is a keyword in sql server) as the column name for primary key of a table. So I cannot use query like this: select key from table_name where column2 = ?
Could anyone help to write a query to get the value of the primary key for a specific row something like this: select primary_key from tbale_name where column2 = ?
Yes you can, simply wrap column names in backticks:
select `key` from `table_name` where `column2` = ?
Alternatively, depending on your DB, you might use square brackets:
select table_name.[key] from table_name where table_name.[column2] = ?
Edit: I see you said "MS SQL". I think that one works with the square brackets. MySQL accepts the backtick syntax.