0% found this document useful (0 votes)
9 views

mysql queries 1 kalyani

Uploaded by

khushbu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

mysql queries 1 kalyani

Uploaded by

khushbu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

mysql> SHOW DATABASES;

+----------+

| Database |

+----------+

| mysql |

| test |

| tmp |

The list of databases displayed by the statement may be different on your


machine;

If the test database exists, try to access it:

mysql> USE test

Database changed

Creating and Selecting a Database

mysql> CREATE DATABASE student;

must always refer toyour database as student, not as Student, STUDENT, or


some other variant.

Note

If you get an error such as ERROR 1044 (42000): Access denied for user

'micah'@'localhost' to database 'menagerie' when attempting to

create a database, this means that your user account does not have the
necessaryprivileges to do so.

mysql> USE student;

Creating a Table
Creating the database is the easy part, but at this point it is empty, as SHOW
TABLES tells you:

mysql> SHOW TABLES;

Empty set (0.00 sec)

The harder part is deciding what the structure of your database should be: what
tables you need and what

columns should be in each of them.

You want a table that contains a record for each of your pets. This can be called
the pet table, and

it should contain, as a bare minimum, each animal's name. Because the name by
itself is not veryinteresting, the table should contain other information. For
example, if more than one person in yourfamily keeps pets, you might want to
list each animal's owner. You might also want to record some basicdescriptive
information such as species and sex.

How about age? That might be of interest, but it is not a good thing to store in a
database. Age changesas time passes, which means you'd have to update your
records often. Instead, it is better to store a fixedvalue such as date of birth.
Then, whenever you need age, you can calculate it as the difference between

the current date and the birth date. MySQL provides functions for doing date
arithmetic, so this is notdifficult. Storing birth date rather than age has other
advantages, too:

• You can use the database for tasks such as generating reminders for upcoming
pet birthdays. (If youthink this type of query is somewhat silly, note that it is the
same question you might ask in the contextof a business database to identify
clients to whom you need to send out birthday greetings in the current

week or month, for that computer-assisted personal touch.)

• You can calculate age in relation to dates other than the current date. For
example, if you store deathdate in the database, you can easily calculate how
old a pet was when it died.
You can probably think of other types of information that would be useful in the
pet table, but the onesidentified so far are sufficient: name, owner, species, sex,
birth, and death.

mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),

species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

VARCHAR is a good choice for the name, owner, and species columns because
the column values varyin length. The lengths in those column definitions need
not all be the same, and need not be 20. You can normally pick any length from
1 to 65535, whatever seems most reasonable to you. If you make a poor choice
and it turns out later that you need a longer field, MySQL provides an ALTER
TABLE statement.

mysql> SHOW TABLES;

+---------------------+

| Tables in student |

+---------------------+

| pet |

+---------------------+

To verify that your table was created the way you expected, use a DESCRIBE
statement:

mysql> DESCRIBE pet;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| name | varchar(20) | YES | | NULL | |

| owner | varchar(20) | YES | | NULL | |

| species | varchar(20) | YES | | NULL | |


| sex | char(1) | YES | | NULL | |

| birth | date | YES | | NULL | |

| death | date | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

You can use DESCRIBE any time, for example, if you forget the names of the
columns in your table or whattypes they have.

Loading Data into a Table

After creating your table, you need to populate it. The LOAD DATA and INSERT
statements are useful forthis.

mysql> INSERT INTO pet

VALUES ('pitbull','gullyboy','hamster','m','1999-03-30',NULL);

String and date values are specified as quoted strings here. Also, with INSERT,
you can insert NULLdirectly to represent a missing value.

mysql> INSERT INTO pet

VALUES ('jeniffer','lopez',newyork','f','1999-03-3',NULL);

Retrieving Information from a Table

SELECT what_to_select

FROM which_table

WHERE conditions_to_satisfy;

what_to_select indicates what you want to see. This can be a list of columns, or *
to indicate “allcolumns.” which_table indicates the table from which you want to
retrieve data. The WHERE clauseis optional. If it is present, conditions to satisfy
specifies one or more conditions that rows mustsatisfy to qualify for retrieval.
Selecting All Data

The simplest form of SELECT retrieves everything from a table:

mysql> SELECT * FROM pet;

+----------+--------+---------+------+------------+------------+

| name | owner | species | sex | birth | death |

+----------+--------+---------+------+------------+------------+

| pitbull | gullyboy | hamster| m | 1999-03-30 | NULL |

mysql> SELECT * FROM pet WHERE name = 'pitbull';

+--------+-------+---------+------+------------+------------+

| name | owner | species | sex | birth | death |

+--------+-------+---------+------+------------+------------+

| pitbull | gullyboy | hamster| f | 1999-03-30 | NULL |

+--------+-------+---------+------+------------+------------+

You might also like