Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2012, 09:00 PM   PM User | #1
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
My first look at PHPMyAdmin / MySQL

I'm messing around with databases and tables and I made a table names 'users' with columns like userid (primary & auto increment), username, etc, and I have one called verified which i set as a boolean (tinyint) and default value defined to 0. The problem is when I made this column it automatically tried to make it a primary key and require unique. I obviously don't want either of the 2 to be the case but it won't let me switch it! If I go in and edit the column and change it to text, however, it doesn't assume this PK value or the other things. I'm really confused why this keeps happening. Any ideas?

Thanks!
KULP is offline   Reply With Quote
Old 11-29-2012, 10:18 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Sounds to me like PHPMyAdmin is getting in the way of sanity.

If you simply execute a command to create a table, such as
Code:
create table users ( 
    userid int auto_increment primary key, 
    verified boolean, 
    name varchar(30) 
);
it works perfectly okay.

So... Forget creating the table using the table creation tools of PHPMyAdmin and just issue a query, as above.

Or you could create the table without the verified column and then use the MySQL query/command
Code:
    ALTER TABLE users ADD verified BOOLEAN;
to add the boolean column with, again, PHPMyAdmin not sticking its ugly nose into things, one hopes.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 04:42 PM   PM User | #3
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
It still creates the column with unique and primary key required... I don't understand, especially because I already have a PK set. GAH

I even tried to create a new table with your first code and just changed the table name to users2 and this still happens:



EDIT: Wait, does gold key mean not primary? I'm getting really confused.

Last edited by KULP; 11-30-2012 at 05:42 PM..
KULP is offline   Reply With Quote
Old 11-30-2012, 07:32 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by KULP View Post
EDIT: Wait, does gold key mean not primary? I'm getting really confused.
I'd guess so. Since using an autoincrement requires that the PK be in use, and that PK is not in gold, that would suggest that phpmyadmin uses gold to mark what is not a pk.
I haven't used phpmyadmin in a long long time. You could simply issue a SHOW CREATE TABLE yourtablename query and it will give you the text required to create it. That would indicate all your keys as well.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 08:17 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
When I use SHOW CREATE TABLE USERS on my created table, I see this:
Code:
mysql> show create table users;
+-------+-------------------------------------------
| Table | Create Table
+-------+-------------------------------------------
| users | CREATE TABLE `users` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `verified` tinyint(1) DEFAULT NULL,
  `name` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------
Man, your experience sure make me want to learn PHPMyAdmin....NOT!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 08:23 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Fou-Lu View Post
Since using an autoincrement requires that the PK be in use...
Not true! AUTO_INCREMENT requires that the AUTO_INCREMENT column have a a UNIQUE INDEX, but it does *NOT* have to be the primary key.

Example:
Code:
mysql> CREATE TABLE FouLu ( id INT AUTO_INCREMENT,
    -> email VARCHAR(50) PRIMARY KEY,
    -> UNIQUE INDEX id_index (id)
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> show create table FouLu;
+-------+----------------------------------------------------
| Table | Create Table
+-------+----------------------------------------------------
| FouLu | CREATE TABLE `foulu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(50) NOT NULL,
  PRIMARY KEY (`email`),
  UNIQUE KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------
I grant you that this is not normal usage, but it's perfectly legal.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 09:12 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by Old Pedant View Post
Not true! AUTO_INCREMENT requires that the AUTO_INCREMENT column have a a UNIQUE INDEX, but it does *NOT* have to be the primary key.

Example:
Code:
mysql> CREATE TABLE FouLu ( id INT AUTO_INCREMENT,
    -> email VARCHAR(50) PRIMARY KEY,
    -> UNIQUE INDEX id_index (id)
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> show create table FouLu;
+-------+----------------------------------------------------
| Table | Create Table
+-------+----------------------------------------------------
| FouLu | CREATE TABLE `foulu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(50) NOT NULL,
  PRIMARY KEY (`email`),
  UNIQUE KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------
I grant you that this is not normal usage, but it's perfectly legal.
I'll be damned, this whole time I thought that you required PK for ai in mysql!
Would there be any benefit of doing the above instead versus swapping the PK and UK on that table? I typically don't use AI at all as I don't usually use a surrogate key, but this may become beneficial in the future for an alternate field I would want to increment.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 09:36 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
> Would there be any benefit of doing the above instead versus swapping the PK and UK on that table?

Probably not much on that table.

But sometimes you will want a primary key that uses multiple fields.

And, at least with INNODB (and, by the by, with SQL Server!) that has PHYSICAL implications:
http://dev.mysql.com/doc/refman/5.5/...mary-keys.html
Quote:
With the InnoDB storage engine, the table data is physically organized to do ultra-fast lookups and sorts based on the primary key column or columns.
In other words, the primary key is placed FIRST in the record layout *AND* forms the basis of the B-TREE organization of the entire table.

SQL Server takes great pains to emphasize that choosing your primary key can make a huge difference in performance. MySQL isn't quite so sensitive, but it can still make a noticeable difference.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 10:35 PM   PM User | #9
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
Still really confused, i just started over with a new table and I can't change primary keys or anything. Ugh!

I did show table and this is what I get:

Code:
CREATE TABLE `users` (
 `userid` int(11) NOT NULL AUTO_INCREMENT,
 `uname` text NOT NULL,
 `pword` text NOT NULL,
 `email` text NOT NULL,
 `nickname` text NOT NULL,
 `verified` tinyint(1) NOT NULL DEFAULT '0',
 PRIMARY KEY (`userid`),
 UNIQUE KEY `userid` (`userid`),
 KEY `userid_2` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I *think* the query looks correct (except what is "KEY `userid_2` (`userid`)")... But this is what it actually shows:


Is anyone else confused?

Last edited by KULP; 11-30-2012 at 10:41 PM..
KULP is offline   Reply With Quote
Old 11-30-2012, 11:05 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
That is *REALLY* what you got from SHOW CREATE TABLE users??

Then the display in PHPMyAdmin? Then I think you need to remove PHPMyAdmin from your system and re-install it.

Because it's clearly braindead.

HOWEVER...

You must have also done something wrong when you did the CREATE TABLE, in the first place.

You have THREE indexes all defined on the field userid which makes no sense at all.

On top of that, you are using the TEXT data type which is a VERY expensive data type for no reason at all! Not just once by FOUR times!

Try doing *JUST THIS*:
Code:
CREATE TABLE users (
    userid INT AUTO_INCREMENT PRIMARY KEY,
    uname VARCHAR(30) NOT NULL,
    pword VARCHAR(30) NOT NULL,
    email VARCHAR(200) NOT NULL,
    nickname VARCHAR(30) NOT NULL,
    verified BOOLEAN NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When I do that and then do show create table users I get:
Code:
+-------+--------------------------------------------
| Table | Create Table
+-------+--------------------------------------------
| users | CREATE TABLE `users` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `uname` varchar(30) NOT NULL,
  `pword` varchar(30) NOT NULL,
  `email` varchar(200) NOT NULL,
  `nickname` varchar(30) NOT NULL,
  `verified` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
KULP (12-02-2012)
Old 12-01-2012, 02:25 AM   PM User | #11
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
Wow that was quite the process. Not sure what all went down but I must say I find it strange that the PK is denoted by a silver key. Seems as though it should be the gold. Anyways... I was clearly pressing SOMETHING wrong.. although I'm not sure what, I think it was successful this time. Really was not looking forward to re-installing. Does this look right to you too (the show turned up identical)? Also, does setting a value at the PK automatically set it as UNIQUE? Because it doesn't show unique I don't think.

KULP is offline   Reply With Quote
Old 12-01-2012, 07:45 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
VOLKSWAGENS are CARS by definition. CARS are not necessarily VOLKSWAGENS. Yet we don't say "VOLKSWAGEN CARS" in normal usage.

PRIMARY keys are UNIQUE by definition. UNIQUE keys are not necessarily PRIMARY. We don't say PRIMARY UNIQUE in normal usage.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-02-2012, 08:21 AM   PM User | #13
KULP
Regular Coder

 
Join Date: Mar 2012
Posts: 166
Thanks: 5
Thanked 11 Times in 11 Posts
KULP is an unknown quantity at this point
Good explanation. Thanks for all the help!
KULP is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:23 AM.


Advertisement
Log in to turn off these ads.