feature | daily | high gear | we::blog | contact      
 
Search
Feature / 16 Oct 2000 / Simple MySQL Commands
Web Search
Anyone can submit a news item, but only members can comment on them! New users, sign up here.
 
  danchan Login  
  Nickname  
   
  Password  
   
  Remember  
   

New openlog - the unbearable lightness of blog - plus free weblogging with comments, easy syndication and no ads!

Get [danchan] for your PDA!

Every daily news item!

10 latest comments

  Archive  
  2001 December
2001 November
2001 October
2001 September
2001 August
2001 July
2001 June
2001 May
2001 April
2001 March
2001 February
2001 January
2000 December
2000 November
2000 October
2000 September
2000 August
2000 July
2000 June
 

Simple MySQL Commands

Let's run through some simple SQL commands. You already know the create table command.

create table users
(
    id              int auto_increment not null,
    nickname        varchar(16) not null,
    password        varchar(16) not null,
    socks           int,
    favorite_number int,
    primary key     (user_id),
    unique          (nickname)
);

What if we just want user nicknames and their favorite number?

select nickname, favorite_number from users;

This gives us:

+----------+-----------------+
| nickname | favorite_number |
+----------+-----------------+
| GdayMate |              42 |
+----------+-----------------+
|   Javier |             945 |
+----------+-----------------+
|     Rolo |               8 |
+----------+-----------------+

And if we want all nicknames of users with less than 10 pairs of socks and whose favorite number is greater than 100?

select nickname from users where socks < 10 and 
    favorite_number > 100;
+----------+
| nickname |
+----------+
|   Javier |
+----------+

So how do you insert a row of data into the table? Simple.

insert into users (nickname, socks)
    values ('Cowlick', 0);

Uh oh, the row that's created is missing the password column! Remember the line used to create the password column in the users table?

create table users
(
    ...
    password varchar(16) not null,
    ...
);

The not null means you must have some value for the column. So MySQL gives an error in this case. We should use instead:

insert into users (nickname, password, socks) 
    values ('Cowlick', 'udder', 0);

This results in a row like this:

+----+----------+----------+-------+-----------------+
| id | nickname | password | socks | favorite_number |
+----+----------+----------+-------+-----------------+ 
|  4 |  Cowlick |    udder |     0 |            NULL |
+----+----------+----------+-------+-----------------+

But wait! Why didn't we have to specify an id? That's not null also. The line from the create table users command:

create table users
(
    id int auto_increment not null, 
    ...
);

In the case of the id column, we have specified auto_increment and MySQL creates the value for us by adding 1 to the greatest value it finds in that column (Rolo has an id of 3).

We've forgotten to include Cowlick's favorite number. Which is -1, by the way. For this we use the update command.

update users set favorite_number = -1 
  where id = 4;

We could have also used:

update users set favorite_number = -1 
  where nickname = 'Cowlick';

But what if there were more than one user with the nickname Cowlick? In our example, there can't be, because in the create table command we've specified

create tables users
(
    ...
    unique (nickname)
);

If we try and insert another user with the nickname Cowlick, we'd get an error from MySQL.

Let's say you've built up a large community of sock aficionados/numerologists and you've been having a lot of problems with Javier. He keeps talking off subject about high jumping and hunting for truffles. The other members of the community are up in arms because they see Javier as a trouble maker and his posts as noise.

delete from users where nickname = 'Javier';

Of course, this doesn't stop Javier from re-registering and continuing his annoying banter.

Let's say you're sick of all the talk about gold toe vs. tube and Western-Lucky-7 vs. Chinese-Good-Fortune-8. You realize there is a demand for a site about high jumping and hunting for truffles. You want to revamp the site.

drop table users;

The table definition and all the data are now gone. Be very careful with this command.

next 3. MySQL types and primary key
page 1, 2, 3, 4
 
0 comments
 
  DAYPOP  
Searching the Living Web
Daypop indexes news sites and weblogs every day to give you the latest relevant information.

Weblogging

Super-customizable weblogging with comments
Add comments to your web site or create a weblog of your own!

Web Caching
Accelerate your website!
Improve your website's responsiveness by preloading your site content into a user's browser cache.

Contents
1
Introduction to MySQL (using Perl DBI)
2
Simple MySQL Commands
3
MySQL types and primary key
4
Using Perl DBI to interface to MySQL
Questions? Comments? Send all mail to: dc@danchan.com