Can I name a column name
in my mysql tables?
Just a word of caution here. As field name name
is fine.
However in stored procedures name as variable does something else.
Consider the following procedure. When I called this procedure it always would alter the first record in my config database. Once I renamed the variable named name to something else, things worked fine.
PROCEDURE `updateConfig`(IN name VARCHAR(255), IN val VARCHAR(255))
BEGIN
UPDATE LOW_PRIORITY `config`
SET `value`=val
WHERE `name` = name
LIMIT 1;
END
Regardless of whether or not you can, you might want to consider why you want to do it in the first place. Name of what? What kind of name? After a few years, if you look at the database schema, will you still remember these details?
I suggest it would be in your best interest to treat the column name the same as you would treat any variable names, and give it a descriptive name. Years down the road, you (or anyone who maintains your code) will be thanking you.
CREATE TABLE mytest (name INT);
SELECT *
FROM mytest;
name
--------
In dubious cases you may enclose names into grave accents:
CREATE TABLE `CREATE TABLE` (name INT);
SELECT *
FROM `CREATE TABLE`;
name
--------
You should be fine calling a column 'name'. Check out http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html for more details on how to deal with reserved words.
Wrapping column names in the (`) character ensures that even reserved words won't be misinterpreted.
Why not just try it?
mysql> select name from name;
+------+
| name |
+------+
| 1 |
+------+
Also you can quote any names with ``.
mysql> select `from` from `from`;
+------+
| from |
+------+
| 1 |
+------+
Yes even though if its a person name I would recommend FirstName or FullName to be the column name to be more specific MYSQL Reserved Words