Use the command line to administer your MySQL database
Table of Contents
- Introduction
- Acknowledgements
- Requirements
- How To Use This Document
- Log On And Off
- Log on from the guest machine
- Log on from the host machine or a remote computer
- Log on (for advanced users)
- Log off
- User Management
- Add a local user with full administrative privileges on all databases
- Add a local user with full administrative privileges on a specific database
- Add a local user with full administrative privileges on a specific table
- Add a remote user with full administrative privileges on all databases
- Add a remote user with full administrative privileges on a specific database
- Add a remote user with full administrative privileges on a specific table
- Add a local user with specific privileges on all databases
- Add a local user with specific privileges on a specific database
- Add a local user with specific privileges on a specific table
- Add a remote user with specific privileges on all databases
- Add a remote user with specific privileges on a specific database
- Add a remote user with specific privileges on a specific table
- Change your password
- Change a user’s local password
- Change a user’s remote password
- Display users
- Remove a user
- Database Management
- Back up and restore a database
- Cancel a MySQL command
- Change the name of a field
- Check MySQL history
- Check MySQL version
- Choose a database to work with
- Create a database
- Create a table
- Create a table with a required field or fields
- Create a table with a primary key field
- Create a table with a primary key field (automatically incremented)
- Create a table with a foreign key field (simple)
- Create a table with a foreign key field (advanced)
- Create a table with an index field
- Create a table with more than one index field
- Create a table with a unique field
- Create a table with a unique required field
- Delete a cell
- Delete a database
- Delete a field
- Delete a record or several records
- Delete a table
- Delete all data from a table
- Display all databases
- Display all data in a table (tabular output)
- Display all data in a table (block output)
- Display all tables
- Display current database
- Display indexes in a table
- Display table structure
- Edit a record or several records in a table (simple)
- Edit a record or several records in a table (advanced)
- Edit a table to index an existing field
- Edit a table to remove an index from a field
- Edit a table to make an existing field unique
- Export a database to an .sql file
- Export a field to a .csv file
- Export all fields to a .csv file
- Export specific fields to a .csv file
- Export specific records to a .csv file
- Import a .csv file into a table
- Import an .sql file into a database
- Import a text file into a table
- Insert a field at the beginning of a table
- Insert a field at the end of a table
- Insert a field between two existing fields in a table
- Insert a primary key field into a table
- Insert a record into a table (partial record with one field)
- Insert a record into a table (partial record with multiple fields)
- Insert multiple records into a table (partial records with one field)
- Insert multiple records into a table (partial records with multiple fields)
- Insert a record into a table (complete record)
- Insert multiple records into a table (complete records)
- Rename a database
- Rename a field
- Rename a table
- Select data from 1 table (all fields)
- Select data from 1 table (specific field)
- Select data from 1 table (specific fields)
- Select data from 1 table (specific records)
- Select data from 1 table (specific records that do not match the criterion)
- Select data from 2 tables (all fields)
- Select data from 2 tables (all fields with common field shown only once)
- Select data from 2 tables (specific fields)
- Select data from 2 tables (specific records)
- Select data from 2 tables (specific records that do not match the criterion)
- Select data from 3 tables (all fields)
- Select data from 3 tables (all fields with common field shown only once)
- Select data from 3 tables (specific fields)
- Select data from 3 tables (specific records)
- Select data from 3 tables (specific records that do not match the criterion)
- Sort selected data by field
- Appendix A
- Appendix B
- Appendix C
- Obligatory Happy Ending
Introduction
- This document is intended to serve as a cheat sheet or reference guide for the administration, structure, and use of a MySQL database. It’s mostly in alphabetical order, so you can quickly look up the steps to accomplish specific tasks.
- The examples were done on Ubuntu Server 8.04 running in VirtualBox 1.5.0_OSE with MySQL 5.0.51a-3ubuntu5.1 installed. You’re not required to have the same setup, but since this guide came about as a natural continuation of the two previous guides, How to install Ubuntu Server 8.04 in VirtualBox under Kubuntu and Yes, but what do I do now that Ubuntu Server 8.04 is installed in VirtualBox?, the instructions often include which machine (guest, host or remote) the commands should be executed on.
- Disclaimer: When showing example tables in this document, the line showing the number of rows in a set and how long it took the server to select them has usually been removed from the examples since the tables themselves are the focus.
- Disclaimer: It’s an accepted convention to use upper case when typing MySQL keywords. I do not follow this convention. If you feel strongly about it, feel free to change the keywords in any of the commands in this guide to upper case when using them.
- If you’ve never worked with MySQL before, the manual at https://dev.mysql.com/doc/ has an excellent tutorial for learning MySQL. I highly recommend that you go there first and complete the tutorial.
Acknowledgements
I would like to thank Frank Pirrone for starting me on this journey of exploration.
Requirements
- MySQL Server
- An understanding of the terms used in this document. Please take a look at the Terminology section.
How To Use This Document
- Make sure you meet the requirements in the Requirements section of this document.
- I highly recommend that you create a copy of the example database on your MySQL server so you can safely try out the commands and see for yourself how they work. The steps for creating it can be found here.
- Refer to the Further Information section for links to some pages on the internet that provide more detailed information than is available here.
- Refer to the Questions And Answers section for the answers to questions that commonly come up.
- Refer to the Troubleshooting section if you’ve followed the instructions on this page and you need to figure out why it’s not working.
- Refer to the Getting Help section if you need more interactive help.
Log On And Off
- This section contains instructions on how to log onto and off of your MySQL database from the guest machine, from the host machine, or from a remote computer.
- Instructions are included for advanced users as well.
| IMPORTANT |
|---|
| User names are case sensitive. |
Log on from the guest machine
- Type this command at a command prompt or in a terminal window on the guest machine:
- USERNAME with your username.
- Provide your MySQL password.
mysql -u USERNAME -p
Replace:
Log on from the host machine or a remote computer
- Type this command at a command prompt or in a terminal window on the host machine or a remote computer to log onto the guest machine:
ssh USERNAME@IP
Replace:
- USERNAME with your username.
- IP with the IP of the MySQL server.
- Provide the password you use on the server.
- Type this command to log onto the MySQL server:
mysql -u USERNAME -p
Replace:
- USERNAME with your MySQL username.
- Provide your MySQL password.
Log on (for advanced users)
Advanced users might like to open port 3306 (the default MySQL port) in the router. Once the port is open, you can use one of the following commands to log onto your database from a remote computer as long as the computer you’re on has the MySQL client installed on it.
- Use this command if the username you’re currently logged on as is the same as your MySQL username:
- IP with the IP of the MySQL server.
- Use this command if the username you’re currently logged on as is different from your MySQL username:
- USERNAME with your MySQL username.
- If you’d like to circumvent the additional password prompt, you can add your password after the -p password argument, but this is not recommended, for security reasons. Note that there is no space between the -p and the password you enter:
- USERNAME with your MySQL username.
- PASSWORD with your MySQL password.
- If you’d like to log on and immediately be using a specific database, you can use this command:
- USERNAME with your MySQL username.
- DATABASENEME with the name of the database you’d like to use.
mysql -h IP -p
Replace:
mysql -h IP -u USERNAME -p
Replace:
mysql -h IP -u USERNAME -pPASSWORD
Replace:
mysql -h IP -u USERNAME -p DATABASENAME
Replace:
Log off
- Regardless of which of the above methods you used to log onto your database, the method for logging off is always the same. Type this command:
-
exit
User Management
- There are two kinds of access for users – local and remote.
- A user can have just local access, just remote access, or local and remote access.
- Follow all the steps for each kind of access you’d like a user to have.
Add a local user with full administrative privileges on all databases
These steps will let you add a “super” user who has full administrative rights on all databases and tables, and the power to grant rights to others. This user can log on from the guest machine. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to all databases from the guest machine:
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
grant all privileges on *.* to 'USERNAME'@'localhost' identified by 'PASSWORD' with grant option;
Replace:
flush privileges;
Example:
Let’s say I want to add Mel as a local user and give him full administrative privileges so he can share the work of maintaining my MySQL server with me. I will be assigning spearmint as his password. I would type this command to give him full local administrative rights:
grant all privileges on *.* to 'Mel'@'localhost' identified by 'spearmint' with grant option;
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. |
Add a local user with full administrative privileges on a specific database
These steps will let you add a “super” user who has full administrative rights on all tables. This user can log on from the guest machine. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific database from the guest machine:
grant all privileges on DATABASENAME.* to 'USERNAME'@'localhost' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a local user and give him full administrative privileges so he can share the work of maintaining all of the tables in the People database on my MySQL server with me. I will be assigning spearmint as his password. I would type this command to give him full local administrative rights:
grant all privileges on People.* to 'Mel'@'localhost' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. |
Add a local user with full administrative privileges on a specific table
These steps will let you add a “super” user who has full administrative rights on a specific table. This user can log on from the guest machine. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific table in a specific database from the guest machine:
grant all privileges on DATABASENAME.TABLENAME to 'USERNAME'@'localhost' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- TABLENAME with the name of the table.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a local user and give him full administrative privileges so he can share the work of maintaining the Address table in the People database on my MySQL server with me. I will be assigning spearmint as his password. I would type this command to give him full local administrative rights:
grant all privileges on People.Address to 'Mel'@'localhost' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. |
Add a remote user with full administrative privileges on all databases
These steps will let you add a “super” user who has full administrative rights on all databases and tables, and the power to grant rights to others. This user can log on from the host machine or remote computers. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to all databases from the host machine or remote computers:
grant all privileges on *.* to 'USERNAME'@'%' identified by 'PASSWORD' with grant option;
Replace:
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a remote user and give him full administrative privileges so he can share the work of maintaining my MySQL server with me. I will be assigning spearmint as his password. I would type this command to give him full remote administrative rights:
grant all privileges on *.* to 'Mel'@'%' identified by 'spearmint' with grant option;
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. |
Add a remote user with full administrative privileges on a specific database
These steps will let you add a “super” user who has full administrative rights on a specific database. This user can log on from the host machine or remote computers. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific database from the host machine or remote computers:
grant all privileges on DATABASENAME.* to 'USERNAME'@'%' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a remote user and give him full administrative privileges so he can share the work of maintaining the People database on my MySQL server with me. I will be assigning spearmint as his password. I would type this command to give him full remote administrative rights:
grant all privileges on People.* to 'Mel'@'%' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. |
Add a remote user with full administrative privileges on a specific table
These steps will let you add a “super” user who has full administrative rights on a specific table. This user can log on from the host machine or remote computers. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to the specific table in a specific database from the host machine or remote computers:
grant all privileges on DATABASENAME.TABLENAME to 'USERNAME'@'%' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- TABLENAME with the name of the table.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a remote user and give him full administrative privileges so he can share the work of maintaining the Address table in the People database on my MySQL server with me. I will be assigning spearmint as his password. I would type this command to give him full remote administrative rights:
grant all privileges on People.Address to 'Mel'@'%' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. |
Add a local user with specific privileges on all databases
These steps will let you add a user whose only powers are to select, insert and update (edit) the information in all databases. This user can log on from the guest machine. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to the all databases from the guest machine:
grant select, insert, update on *.* to 'USERNAME'@'localhost' identified by 'PASSWORD';
Replace:
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a local user and give him access to all databases on my MySQL server, but only give him the power to work with the data. I will be assigning spearmint as his password. I would type this command to give him local database access:
grant select, insert, update on *.* to 'Mel'@'localhost' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. There many privileges you can choose from. |
Add a local user with specific privileges on a specific database
These steps will let you add a user whose only powers are to select, insert and update (edit) the information in a specific database. This user can log on from the guest machine. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific database from the guest machine:
grant select, insert, update on DATABASENAME.* to 'USERNAME'@'localhost' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a local user and give him access to the People database on my MySQL server, but only give him the power to work with the data. I will be assigning spearmint as his password. I would type this command to give him local database access:
grant select, insert, update on People.* to 'Mel'@'localhost' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. There many privileges you can choose from. |
Add a local user with specific privileges on a specific table
These steps will let you add a user whose only powers are to select, insert and update (edit) the information in a specific table. This user can log on from the guest machine. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific table in a specific database from the guest machine:
grant select, insert, update on DATABASENAME.TABLENAME to 'USERNAME'@'localhost' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- TABLENAME with the name of the table.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a local user and give him access to the Address table in the People database on my MySQL server, but only give him the power to work with the data. I will be assigning spearmint as his password. I would type this command to give him local database access:
grant select, insert, update on People.Address to 'Mel'@'localhost' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. There many privileges you can choose from. |
Add a remote user with specific privileges on all databases
These steps will let you add a user whose only powers are to select, insert and update (edit) the information in all databases. This user can log on from the host machine or a remote computer. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to all databases from the host machine or remote computers:
grant select, insert, update on *.* to 'USERNAME'@'%' identified by 'PASSWORD';
Replace:
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a remote user and give him access to all databases on my MySQL server, but only give him the power to work with the data. I will be assigning spearmint as his password. I would type this command to give him remote database access:
grant select, insert, update on *.* to 'Mel'@'%' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. There many privileges you can choose from. |
Add a remote user with specific privileges on a specific database
These steps will let you add a user whose only powers are to select, insert and update (edit) the information in a specific databases. This user can log on from the host machine or a remote computer. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific database from the host machine or remote computers:
grant select, insert, update on DATABASENAME.* to 'USERNAME'@'%' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a remote user and give him access to the People database on my MySQL server, but only give him the power to work with the data. I will be assigning spearmint as his password. I would type this command to give him remote database access:
grant select, insert, update on People.* to 'Mel'@'%' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. There many privileges you can choose from. |
Add a remote user with specific privileges on a specific table
These steps will let you add a user whose only powers are to select, insert and update (edit) the information in a specific table. This user can log on from the host machine or a remote computer. You must choose a password for your user. He or she can change that password later.
- Make sure you’re logged on.
- Type this command to give the user access to a specific table from the host machine or remote computers:
grant select, insert, update on DATABASENAME.TABLENAME to 'USERNAME'@'%' identified by 'PASSWORD';
Replace:
- DATABASENAME with the name of the database.
- TABLENAME with the name of the table.
- USERNAME with the name of the user.
- PASSWORD with the password of the user.
- Type this command to apply the changes without having to restart the server:
flush privileges;
Example:
Let’s say I want to add Mel as a remote user and give him access to the Address table in the People database on my MySQL server, but only give him the power to work with the data. I will be assigning spearmint as his password. I would type this command to give him remote database access:
grant select, insert, update on People.Address to 'Mel'@'%' identified by 'spearmint';
I would then apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| User names are case sensitive.
Passwords are case sensitive. There many privileges you can choose from. |
Change your password
- Make sure you’re logged on.
- Type this command to change your password:
set password = password('NEWPASSWORD');Replace:
- NEWPASSWORD with the new password you’d like to use.
Example:
Let’s say my current password is spearmint and I’d like to change it to peppermint. I would type this command:
set password=password('peppermint');
| IMPORTANT |
|---|
| Passwords are case sensitive. |
Change a user’s local password
This will change a user’s password for logging onto your MySQL server from the guest machine.
- Make sure you’re logged on.
- Type this command:
set password for 'USERNAME'@'localhost' = password('NEWPASSWORD');Replace:
- USERNAME with the name of the user.
- NEWPASSWORD with the user’s new password.
Example:
Let’s say I have a user named Mel who has local access to my MySQL server. His current password is spearmint and he’d like me to change it to peppermint. I would type this command:
set password for 'Mel'@'localhost' = password('peppermint');
| IMPORTANT |
|---|
| Passwords are case sensitive. |
Change a user’s remote password
This will change a user’s password for logging onto your MySQL server from the host machine or a remote computer.
- Make sure you’re logged on.
- Type this command:
set password for 'USERNAME'@'%' = password('NEWPASSWORD');Replace:
- USERNAME with the name of the user.
- NEWPASSWORD with the user’s new password.
Example:
Let’s say I have a user named Mel who has remote access to my MySQL server. His current password is spearmint and he’d like me to change it to peppermint. I would type this command:
set password for 'Mel'@'%' = password('peppermint');
| IMPORTANT |
|---|
| Passwords are case sensitive. |
Display users
- Make sure you’re logged on.
- Type this command to choose the default database that MySQL uses for all of its internal “housekeeping chores”:
- Display all users on your server by tying this command:
use mysql;
select user, host from mysql.user;
Remove a user
These steps will remove a user from your MySQL server regardless of what privileges they have.
- Make sure you’re logged on.
- Type this command to choose the default mysql database that MySQL uses for all of its internal “housekeeping chores”:
- Type this command to remove the user:
- USERNAME with the name of the user you’d like to remove.
- Type this command to apply the changes without having to restart the server:
use mysql;
delete from user where user='USERNAME';
Replace:
flush privileges;
Example:
Let’s say that for some reason I no longer wish Mel to be a user on my MySQL server. I would type this command to use the MySQL housekeeping database:
use mysql;
Then I’d type this command to remove Mel as a user from my server:
delete from user where user='Mel';
And finally, I’d apply the changes with this command:
flush privileges;
| IMPORTANT |
|---|
| If the user is logged into your MySQL server at the time, they will continue to have access until they log out. |
Database Management
This section is organized – for the most part – alphabetically, to make it easy to find what you’re looking for.
Back up and restore a database
For the purposes of this guide, backups can be done by exporting your databases to .sql or .csv files, and restores can be done by importing the files back into MySQL. What you do to further back up the exported files is an important decision only you can make after looking at the various ways Linux offers for doing backups and deciding which is the best fit for how you work.
Cancel a MySQL command
- If you’ve accidentally typed an incomplete command and pressed the Enter key, MySQL will wait at the MySQL prompt until you finish or cancel the command. To cancel an incomplete command, type this at the MySQL prompt:
-
\c
- Note that this does not always work, and you may occasionally find yourself in a position where you have to log out and back in again instead.
Change the name of a field
This will change the name of a field while preserving its type and attributes.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to make note of the current name, type, and attributes of the field you’re about to rename:
describe TABLENAME;
Replace:
- TABLENAME with the name of the table the field is in.
- Type this command to rename the field:
alter table TABLENAME change FIELDNAME NEWFIELDNAME TYPE(ATTRIBUTE);
Replace:
- TABLENAME with the name of the table the field is in.
- FIELDNAME with the current name of the field.
- NEWFIELDNAME with the new name for the field.
- TYPE with the field type.
- ATTRIBUTE with the attribute(s) of the field.
| IMPORTANT |
|---|
| It’s always wise to back up a database before making changes to its structure.
When doing the describe TABLENAME; command, note that the Default field |
Check MySQL history
Type this command at the command prompt or in a terminal window on the guest machine to check your MySQL history:
cat ~/.mysql_history
If you find yourself frequently typing the same commands over and over while in MySQL, you can edit this file with any text editor to contain just those commands. Then you can use your up and down arrow keys at a MySQL prompt to shop through the commands.
Check MySQL version
- Make sure you’re logged on.
- Type this command to check the version of MySQL you’re using:
select version();
Choose a database to work with
- Make sure you’re logged on.
- Display all databases on the server by typing this command:
- Choose the database you want to work with by typing this command:
use DATABASENAME;
Replace:
- DATABASENAME with the name of the database.
show databases;
Create a database
This will create a new, empty database after checking whether you already have a database by the same name.
- Make sure you’re logged on.
- Type this command to create a database:
create database if not exists DATABASENAME;
Replace:
- DATABASENAME with the name you’d like to give the database.
Example:
Let’s say I want to create a database named People. I can type this command:
create database if not exists People;
If I already have a database named People, MySQL will respond with a warning and not create the new database:
Query OK, 0 rows affected, 1 warning (0.00 sec)
If I then decide that I’ll name my new database People2, I can type this command:
create database if not exists People2;
MySQL will respond without a warning and create the new database:
Query OK, 1 row affected (0.01 sec)
| IMPORTANT |
|---|
| Database names are case sensitive. |
Create a table
This will create a table with no required fields. This means that any of the cells are allowed to be empty, and MySQL will fill them in with the default NULL placeholder to indicate to you that they’re empty.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create a table:
create table TABLENAME (FIELDNAME TYPE(ATTRIBUTE));
Replace:
- TABLENAME with the name of the table.
- FIELDNAME with the name of the field.
- TYPE with the type of field you’re creating.
- ATTRIBUTE with the attribute(s) of the field.
- Note: You can add as many additional fields as you like by separating each from the one before it with a comma and a space, and defining the type and attribute(s) of each. For example:
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1), FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3));
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into only some of the fields:
- MySQL will insert the data you typed into the field(s).
- MySQL will insert the NULL placeholder into the empty field(s).
- If you do a complete insert and insert the NULL placeholder into any of the fields:
- MySQL will insert the NULL placeholder into the empty field(s).
- MySQL will insert the data you typed into the other fields.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
Example:
Let’s say I want to create the example Phone table. Since this is a default table I’m creating, I’m not specifying that any of the fields in my table are required.
I need a contact field for the names of the people I add to the database. Since names can use letters and other characters, I’ll choose varchar as the type of field. Since names can be of varying lengths, I’ll give the contact field a width of 40, which should give me plenty of space to fit a name.
I need a homephone and cellphone field to hold the numbers where I can reach my contacts. Since phone numbers can use parentheses and dashes, I cannot choose an integer type for the homephone and cellphone fields. I’ll use varchar for those as well. I will make (XXX) XXX-XXXX the accepted method for entering phone numbers with X representing any number. There are 14 characters in that example, so I’ll give the homephone and cellphone fields a width of 14.
Now that all of that is decided, I can create the table:
create table Phone (contact varchar(40), homephone varchar(14), cellphone varchar(14));
I can then verify that my table got created correctly:
describe Phone;
MySQL will then show me the structure of my table:
+-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | contact | varchar(40) | YES | | NULL | | | homephone | varchar(14) | YES | | NULL | | | cellphone | varchar(14) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+
Everything is as I want it to be! Now I can add some data. First I’ll add Hugo’s information:
insert into Phone values ("Hugo", "(800) 111-1111", "(900) 111-1111");
Then I’ll add the information for Lucas:
insert into Phone values ("Lucas", "(800) 222-2222", "(900) 222-2222");
And finally I’ll add Spencer’s information. But let’s say I don’t know Spencer’s cell phone number. I’ll do a partial insert, filling in the fields I do know:
insert into Phone (contact, homephone) values ("Spencer", "(800) 333-3333");
I can then look at my table to see the entries I just added:
select * from Phone;
All my entries are just as I want them to be. Notice that MySQL has kindly inserted the NULL placeholder into the cellphone field of Spencer’s record since I left that field empty:
+---------+----------------+----------------+ | contact | homephone | cellphone | +---------+----------------+----------------+ | Hugo | (800) 111-1111 | (900) 111-1111 | | Lucas | (800) 222-2222 | (900) 222-2222 | | Spencer | (800) 333-3333 | NULL | +---------+----------------+----------------+
Now suppose I have Trent’s home phone number, but can’t remember his name or cell phone number, I could do a partial insert, putting just his home phone number into my table and figuring I’ll check on his name and fill it in later:
insert into Phone (homephone) values ("(800) 444-4444");
I can then look at my table again to see the entry I just added.
select * from Phone;
MySQL has kindly inserted the NULL placeholder into both the contact and cellphone fields of Trent’s record:
+---------+----------------+----------------+ | contact | homephone | cellphone | +---------+----------------+----------------+ | Hugo | (800) 111-1111 | (900) 111-1111 | | Lucas | (800) 222-2222 | (900) 222-2222 | | Spencer | (800) 333-3333 | NULL | | NULL | (800) 444-4444 | NULL | +---------+----------------+----------------+
I could, instead, have done a complete insert by using this command and specifying values for all of the fields:
insert into Phone values (NULL, "(800) 444-4444", NULL);
Notice that in order to insert the NULL placeholder, I typed NULL without quotes around it. If I had used quotes, MySQL would have treated it as a text string (word) rather than a MySQL placeholder which represents the absence of data.
Create a table with a required field or fields
This will create a table specifying (with not null) that one or more of the fields must be filled out when data is entered into the table.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create a table:
create table TABLENAME (FIELDNAME TYPE(ATTRIBUTE) not null);
Replace:
- TABLENAME with the name of the table.
- FIELDNAME with the name of the field.
- TYPE with the type of field you’re creating.
- ATTRIBUTE with the attribute(s) of the field.
- Note: You can add as many additional fields as you like by separating each from the one before it with a comma and a space, and defining the type and attribute(s) of each. You can designate as many of the fields not null as you like:
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1) not null, FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3) not null);
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just the non-required fields:
- MySQL will print a warning.
- MySQL will insert the data you typed into the non-required fields.
- MySQL will place nothing in the empty required field(s).
- If you do a partial insert and type valid data into just the required field(s):
- MySQL will insert the data you typed into the required field(s).
- MySQL will insert the NULL placeholder into the empty, non-required field(s).
- If you do a complete insert and attempt to insert the NULL placeholder into the required field(s):
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
Example:
Let’s say I want to create the example Phone table.
I need a contact field for the names of the people I add to the database. Since names can use letters and other characters, I’ll choose varchar as the type of field. Since names can be of varying lengths, I’ll give the contact field a width of 40, which should give me plenty of space to fit a name. Since my table wouldn’t make much sense without people’s names, I want to make this field required by using the not null designation.
I need a homephone and cellphone field to hold the numbers where I can reach my contacts. Since phone numbers can use parentheses and dashes, I cannot choose an integer type for the homephone and cellphone fields. I’ll use varchar for those as well. I will make (XXX) XXX-XXXX the accepted method for entering phone numbers with X representing any number. There are 14 characters in that example, so I’ll give the homephone and cellphone fields a width of 14. I will not make these fields required.
Now that all of that is decided, I can create the table:
create table Phone (contact varchar(40) not null, homephone varchar(14), cellphone varchar(14));
I can then verify that my table got created correctly:
describe Phone;
MySQL will then show me the structure of my table:
+-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | contact | varchar(40) | NO | | NULL | | | homephone | varchar(14) | YES | | NULL | | | cellphone | varchar(14) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
Everything is as I want it to be! The contact field is required (NOT NULL) and the other two fields are optional (NULL). This command can be misleading in that the Default field shows NULL as the value for all fields, even though the contact field is NOT NULL. What’s important when looking at the results of the describe TABLE; command is to check the Null field. If it says NO, then the field is NOT NULL. If it says YES, then the field is NULL.
Now I can add some data. First I’ll add Hugo’s information:
insert into Phone values ("Hugo", "(800) 111-1111", "(900) 111-1111");
Then I’ll add the information for Lucas:
insert into Phone values ("Lucas", "(800) 222-2222", "(900) 222-2222");
And finally I’ll add Spencer’s information, but let’s say I don’t know Spencer’s cell phone number. I’ll do a partial insert, filling in the fields I do know:
insert into Phone (contact, homephone) values ("Spencer", "(800) 333-3333");
I can then look at my table to see the entries I just added:
select * from Phone;
All my entries are just as I want them to be. Notice that MySQL has kindly inserted the NULL placeholder into the cellphone field of Spencer’s record since I left that field empty:
+---------+----------------+----------------+ | contact | homephone | cellphone | +---------+----------------+----------------+ | Hugo | (800) 111-1111 | (900) 111-1111 | | Lucas | (800) 222-2222 | (900) 222-2222 | | Spencer | (800) 333-3333 | NULL | +---------+----------------+----------------+
Suppose I have Trent’s home phone number, but can’t remember his name or cell phone number, I could do a partial insert, putting just his home phone number into my table and figuring I’ll check on his name and fill it in later:
insert into Phone (homephone) values ("(800) 444-4444");
MySQL will print out a warning letting me know I’ve done something wrong:
Query OK, 1 row affected, 1 warning (0.00 sec)
I can then look at my table again to see the entry I just added. As you can see, MySQL left the required contact field completely empty, filled in the field I gave it data for, and inserted the NULL placeholder into the empty cellphone field:
+---------+----------------+----------------+ | contact | homephone | cellphone | +---------+----------------+----------------+ | Hugo | (800) 111-1111 | (900) 111-1111 | | Lucas | (800) 222-2222 | (900) 222-2222 | | Spencer | (800) 333-3333 | NULL | | | (800) 444-4444 | NULL | +---------+----------------+----------------+
I could, instead, have tried to add Trent’s record as a complete insert by using this command and specifying values for all the fields:
insert into Phone values (NULL, "(800) 444-4444", NULL);
Notice that in order to insert the NULL placeholder, I typed NULL without quotes around it. If I had used quotes, MySQL would have treated it as a text string (word) rather than a MySQL placeholder which represents the absence of data.
MySQL would have immediately kicked out an error message letting me know that the contact field cannot be null:
ERROR 1048 (23000): Column 'contact' cannot be null
Trent’s record would not have been added.
It’s very important to pay careful attention to what you’re doing when you want to manually insert NULL placeholders into a table. If I had tried to do a complete insert and accidentally placed quotes around the first instance of NULL in the command, MySQL would not have printed out an error and it would have added the record, inserting NULL as text rather than as a placeholder. Since they would both look the same when displaying the data in the table, if I wasn’t paying close attention, I might not notice that I had done this.
Create a table with a primary key field
This will create a table with a primary key field that you are required to fill out manually with unique data. When creating a primary key in this way, you are not restricted to using an integer type of field.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create a table:
create table TABLENAME (FIELDNAME TYPE(ATTRIBUTE) not null primary key);
Replace:
- TABLENAME with the name of the table.
- FIELDNAME with the name of the primary key field.
- TYPE with the type of field you’d like to create.
- ATTRIBUTE with the attribute(s) of the field.
- Note that you can add as many additional fields as you like by separating each from the one before it with a comma and a space, and defining the type and attribute(s) of each:
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1) not null primary key, FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3));
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just the non-required fields:
- MySQL will print a warning.
- MySQL will insert the data you typed into the non-required fields.
- MySQL will place nothing in the empty primary key field and any other required fields.
- If you do a partial insert and type valid data into just the primary key field:
- MySQL will insert the data you typed into the primary key field.
- MySQL will insert the NULL placeholder into the empty, non-required fields.
- MySQL will print a warning if there are any empty required fields other than the primary key field.
- MySQL will place nothing in any empty required fields other than the primary key field.
- If you do a complete insert and insert the NULL placeholder into the primary key field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
Example:
Let’s say I want to create a very simple table named Identification with just a primary key field named ID. I would type this command:
create table Identification (ID varchar(6) not null primary key);
For a more complex example, let’s say I have a club whose members are listed in the example Address table. I’d like to create a table just like that, but with the first field being a primary key field named ID that I can type membership numbers into. I’d like the ID and contact fields to be required. I would type this command:
create table Address (ID varchar(6) not null primary key, contact varchar(40) not null, street varchar(40), city varchar(20), state char(2), zip int(5));
If I then filled the table with the same data that’s in the example Address table and added an ID number for each contact, my table would look like this:
+--------+---------+---------------+-------------+-------+-------+ | ID | contact | street | city | state | zip | +--------+---------+---------------+-------------+-------+-------+ | CHXD01 | Hugo | 1 Main Street | Jackson | MS | 11111 | | CHXD02 | Lucas | 2 Main Street | Springfield | IL | 22222 | | CHXD03 | Spencer | 3 Main Street | Salem | OR | 33333 | +--------+---------+---------------+-------------+-------+-------+
- Note that the entries in the primary key field in this example are incremented. You do not need to do this. When adding records manually to this table, you can type any data into the primary key field as long as it’s unique (hasn’t already been used in that field).
Create a table with a primary key field (automatically incremented)
This will create a table with an integer type primary key field that can be automatically added and incremented for you by MySQL.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create a table:
create table TABLENAME (FIELDNAME TYPE(#) not null auto_increment primary key);
Replace:
- TABLENAME with the name of the table.
- FIELDNAME with the name of the primary key field.
- TYPE with an integer type of field.
- # with the number of integers wide you’d like the field to be.
- Note that you can add as many additional fields as you like by separating each from the one before it with a comma and a space, and defining the type and attribute(s) of each:
create table TABLENAME (FIELDNAME1 TYPE1(#) not null auto_increment primary key, FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3));
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just the non-required field(s):
- MySQL will fill in the primary key field for you with an automatically incremented number.
- MySQL will insert the data you typed into the non-required field(s).
- MySQL will insert the NULL placeholder into any empty non-required fields.
- MySQL will print a warning if there are any empty required fields other than the primary key field.
- MySQL will place nothing in any empty required fields other than the primary key field.
- If you do a partial insert and type an integer into just the primary key field:
- MySQL will insert the integer you typed into the primary key field.
- MySQL will use the integer you entered into the primary key field as the new starting point from which to continue automatically incrementing integers in that field.
- MySQL will insert the NULL placeholder into the empty, non-required fields.
- MySQL will print a warning if there are any empty required fields other than the primary key field.
- MySQL will place nothing in any empty required fields other than the primary key field.
- If you do a complete insert and insert the NULL placeholder into the primary key field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
- MySQL will use the integer you entered into the primary key field as the new starting point from which to continue automatically incrementing integers in that field.
Example:
Let’s say I want to create a very simple table named Identification with just a primary key field named ID. I would type this command:
create table Identification (ID tinyint(3) not null auto_increment primary key);
For a more complex example, let’s say I want to create the example Address table and I’d like to have the first field be a primary key field named ID. I would type this command:
create table Address (ID tinyint(3) not null auto_increment primary key, contact varchar(40) not null, street varchar(40), city varchar(20), state char(2), zip int(5));
If I then did a partial insert to fill in the table with the same data that’s in the example Address table, MySQL would automatically number the ID field for me, and my table would look like this:
+----+---------+---------------+-------------+-------+-------+ | ID | contact | street | city | state | zip | +----+---------+---------------+-------------+-------+-------+ | 1 | Hugo | 1 Main Street | Jackson | MS | 11111 | | 2 | Lucas | 2 Main Street | Springfield | IL | 22222 | | 3 | Spencer | 3 Main Street | Salem | OR | 33333 | +----+---------+---------------+-------------+-------+-------+
Create a table with a foreign key field (simple)
This will create a table with a foreign key field that is linked to the primary key field of any other table in the same database. The parent table has a primary key field, and the child table has a foreign key field that links to the parent table’s primary key field. This example is as simple as possible. Each of the tables has only two fields.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create the parent table:
create table TABLENAME1 (FIELDNAME1 TYPE1(ATTRIBUTE1) not null primary key, FIELDNAME2 TYPE2(ATTRIBUTE2) not null) engine=innodb;
Replace:
- TABLENAME1 with the name of the parent table.
- FIELDNAME1 with the name of the first (primary key) field.
- TYPE1 with the type of primary key field you’d like to create.
- ATTRIBUTE1 with the attribute(s) of the primary key field.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attribute(s) of the second field.
- Type this command to create the child table:
create table TABLENAME2 (FIELDNAME1 TYPE1(ATTRIBUTE1) not null, FIELDNAME2 TYPE2(ATTRIBUTE2) not null, foreign key (FOREIGNKEYFIELDNAME) references TABLENAME1 (TABLENAME1PRIMARYKEYFIELDNAME)) engine=innodb;
Replace:
- TABLENAME2 with the name of the child table.
- FIELDNAME1 with the name of the first field.
- TYPE1 with the type of the first field.
- ATTRIBUTE1 with the attribute(s) of the first field.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attribute(s) of the second field.
- FOREIGNKEYFIELDNAME with the name of the field you’d like to use as the foreign key (pick one of the fields you just created).
- TABLENAME1 with the name of the parent table.
- TABLENAME1PRIMARYKEYFIELDNAME with the name of the primary key field from the parent table.
- Note that you can add as many additional fields as you like by separating each from the one before it with a comma and a space, and defining the type and attribute(s) of each.
| IMPORTANT |
|---|
| The foreign key field should have the same type and attribute(s) as the parent table’s primary key field. |
HOW THE PARENT TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just one field:
- MySQL will print a warning.
- MySQL will insert the data you typed into the required field.
- MySQL will place nothing in the empty required field.
- If you do a complete insert and insert the NULL placeholder into either field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into both fields:
- MySQL will insert the data you typed into both fields.
HOW THE CHILD TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just the foreign key field:
- MySQL will print a warning.
- MySQL will insert the data you typed into the foreign key field.
- MySQL will place nothing in the empty required field.
- If you do a partial insert and type valid data into just the non-foreign key field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and insert the NULL placeholder into either field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into both fields:
- MySQL will insert the data you typed into both fields.
- If you do a partial or complete insert and type invalid data into the foreign key field:
- MySQL will print an error message.
- MySQL will not add the record.
Example:
Let’s say I want to give Hugo and his pals some tests and I’d like to keep track of whether they pass or fail. I can create a parent table with a primary key field named code to store the values I want to use in the child table, and another field named grade to specify which code value means passing or failing:
create table Table1 (code tinyint(1) not null primary key, grade char(6) not null) engine=innodb;
Then I can insert the records for passing and failing into the table:
insert into Table1 values ("1", "passed"), ("2", "failed");
I can take a look at the table:
select * from Table1;
MySQL shows me that the table was created properly:
+------+--------+ | code | grade | +------+--------+ | 1 | passed | | 2 | failed | +------+--------+
Next I’ll create the child table with a name field for Hugo and his pals to be listed in and a results foreign key field which links to (references) the code field in Table1:
create table Table2 (name varchar(40) not null, results tinyint(1) not null, foreign key (results) references Table1 (code)) engine=innodb;
Since Hugo passed the test, I’ll add his record with a 1 in the results field:
insert into Table2 values ("Hugo", "1");
Since Lucas failed the test, I’ll add his record with a 2 in the results field:
insert into Table2 values ("Lucas", "2");
Now let’s say that Spencer passed the test and I intended to put a 1 in the results field, but for some reason I pressed the 3 key instead:
insert into Table2 values ("Spencer", "3");
MySQL immediately lets me know that it’s refusing to add the record because I chose a value for the foreign key field that doesn’t exist in the code field of the parent table:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`Test/Table2`, CONSTRAINT `Table2_ibfk_1` FOREIGN KEY (`code`) REFERENCES `Table1` (`code`))
I can add that record properly by choosing a value from the code field from Table1:
insert into Table2 values ("Spencer", "1");
I can take a look at the table:
select * from Table2;
MySQL will show me that Spencer got added:
+---------+---------+ | name | results | +---------+---------+ | Hugo | 1 | | Lucas | 2 | | Spencer | 1 | +---------+---------+ 3 rows in set (0.00 sec)
Let’s say I just want to see the records of those who passed the test. I can type this command, selecting the name field from the child table and the grade field from the parent table, and specifying that I only want the records from those who have a value of 1 in the results field of the child table:
select Table2.name, Table1.grade from Table2, Table1 where Table2.results=Table1.code and Table2.results="1";
MySQL would show me who passed the test:
+---------+--------+ | name | grade | +---------+--------+ | Hugo | passed | | Spencer | passed | +---------+--------+
Or maybe I’d like to see who failed the test. I can type this command, selecting the name field from the child table and the grade field from the parent table, and specifying that I only want the records from those who have a value of 2 in the results field of the child table:
select Table2.name, Table1.grade from Table2, Table1 where Table2.results=Table1.code and Table2.results="2";
MySQL would show me who failed the test:
+-------+--------+ | name | grade | +-------+--------+ | Lucas | failed | +-------+--------+
Create a table with a foreign key field (advanced)
This will create a table with a foreign key field that is linked to the primary key field of any other table in the same database. This example is a bit more advanced. The parent table has two fields. The child table has three fields. Both tables have an automatically incremented primary key field. The child table has a foreign key field that links to the parent table’s primary key field. In this example, the third field is the foreign key field. Adjust the instructions to specify whichever field you’d like as the foreign key field.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create the parent table:
create table TABLENAME1 (FIELDNAME1 TYPE1(#) not null primary key auto_increment, FIELDNAME2 TYPE2(ATTRIBUTE2) not null) engine=innodb;
Replace:
- TABLENAME1 with the name of the parent table.
- FIELDNAME1 with the name of the first (primary key) field.
- TYPE1 with an integer type.
- # with the number of characters wide you’d like the primary key field to be.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attribute(s) of the second field.
- Type this command to create the child table:
- TABLENAME2 with the name of the child table.
- FIELDNAME1 with the name of the first (primary key) field.
- TYPE1 with an integer type.
- # with the number of characters wide you’d like the primary key field to be.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attribute(s) of the second field.
- FIELDNAME3 with the name of the third field.
- TYPE3 with the type of the third field.
- ATTRIBUTE3 with the attribute(s) of the third field.
- All instances of FOREIGNKEYFIELDNAME with the name of the field you’d like to use as the foreign key (pick one of the fields you just created).
- TABLENAME1 with the name of the parent table.
- TABLENAME1PRIMARYKEYFIELDNAME with the name of the primary key field from the parent table.
create table TABLENAME2 (FIELDNAME1 TYPE1(#) not null primary key auto_increment, FIELDNAME2 TYPE2(ATTRIBUTE2) not null, FIELDNAME3 TYPE3(ATTRIBUTE3) not null, index (FOREIGNKEYFIELDNAME), foreign key (FOREIGNKEYFIELDNAME) references TABLENAME1 (TABLENAME1PRIMARYKEYFIELDNAME)) engine=innodb;
Replace:
- Note that you can add as many additional fields as you like by separating each from the one before it with a comma and a space, and defining the type and attribute(s) of each.
| IMPORTANT |
|---|
| The foreign key field should have the same type and attribute(s) as the parent table’s primary key field. |
HOW THE PARENT TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just one field:
- MySQL will print a warning.
- MySQL will insert the data you typed into the required field.
- MySQL will place nothing in the empty, required fields.
- If you do a partial insert and type an integer into just the primary key field:
- MySQL will print a warning.
- MySQL will insert the integer you typed into the primary key field.
- MySQL will use the integer you entered into the primary key field as the new starting point for continuing to automatically increment integers in that column.
- MySQL will place nothing in the empty, required fields.
- If you do a complete insert and insert the NULL placeholder into either field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into both fields:
- MySQL will insert the data you typed into both fields.
- MySQL will use the integer you entered into the primary key field as the new starting point for continuing to automatically increment integers in that column.
HOW THE CHILD TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into just the foreign key field:
- MySQL will print a warning.
- MySQL will insert the data you typed into the foreign key field.
- MySQL will leave the other two fields empty.
- If you do a partial insert and type valid data into just the non-foreign key fields:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and insert the NULL placeholder into any of the fields:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
- MySQL will use the integer you entered into the primary key field as the new starting point for continuing to automatically increment integers in that column.
- If you do a partial or complete insert and type invalid data into the foreign key field:
- MySQL will print an error message.
- MySQL will not add the record.
Example:
Let’s say I have a laboratory in which I do experiments and I’d like to keep track of which ones have succeeded, which have failed, which are currently incomplete, and which ones I’m completely unsure of. I can create a parent table that lists the four different types of classification using the primary key field to assign numbers to them:
create table Table1 (id tinyint(3) not null primary key auto_increment, classification varchar(50) not null) engine=innodb;
I can then do a partial insert, filling in only the classification field, since MySQL will handle the id field for me:
insert into Table1 (classification) values ("succeeded"), ("failed"), ("incomplete"), ("unknown");
I can then take a look at my table to verify that the primary key numbered my classifications for me:
select * from Table1;
MySQL shows me that all is well:
+----+----------------+ | id | classification | +----+----------------+ | 1 | succeeded | | 2 | failed | | 3 | incomplete | | 4 | unknown | +----+----------------+ 4 rows in set (0.01 sec)
I can then create a child table with a primary key field of its own, and a foreign key field named FK that links to the primary key field in the parent table:
create table Table2 (id tinyint(3) not null primary key auto_increment, experiment varchar(50) not null, FK tinyint(3) not null, index (FK), foreign key (FK) references Table1 (id)) engine=innodb;
I can then add records to my table as I do my experiments. All of these records will be considered acceptable since I chose a value for the FK field from the id field of the parent table:
insert into Table2 (experiment, FK) values ("Experiment1", "1");
insert into Table2 (experiment, FK) values ("Experiment2", "2");
insert into Table2 (experiment, FK) values ("Experiment3", "4");
insert into Table2 (experiment, FK) values ("Experiment4", "4");
Just as an example, if I tried to add this record, I’d run into trouble:
insert into Table2 (experiment, FK) values ("Experiment5", "5");
MySQL would immediately let me know that it’s refusing to add the record because I chose a value for the foreign key field that doesn’t exist in the primary key field of the parent table:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`Test/Table2`, CONSTRAINT `Table2_ibfk_1` FOREIGN KEY (`FK`) REFERENCES `Table1` (`id`))
I can take a look at the entire child table:
select * from Table2;
MySQL will show me the entire table:
+----+-------------+----+ | id | experiment | FK | +----+-------------+----+ | 1 | Experiment1 | 1 | | 2 | Experiment2 | 2 | | 3 | Experiment3 | 4 | | 4 | Experiment4 | 4 | +----+-------------+----+ 4 rows in set (0.00 sec)
Let’s say I just want to see the experiment names and classification of any experiments that succeeded. I can type this command:
select Table2.experiment, Table1.classification from Table2, Table1 where Table2.FK=Table1.id and Table2.FK="1";
MySQL will show me the experiments that succeeded:
+-------------+----------------+ | experiment | classification | +-------------+----------------+ | Experiment1 | succeeded | +-------------+----------------+ 1 row in set (0.00 sec)
Let’s say I just want to see the experiment names and classification of any experiments whose classification is unknown. I can type this command:
select Table2.experiment, Table1.classification from Table2, Table1 where Table2.FK=Table1.id and Table2.FK="4";
MySQL will show me all experiments whose classification is unknown:
+-------------+----------------+ | experiment | classification | +-------------+----------------+ | Experiment3 | unknown | | Experiment4 | unknown | +-------------+----------------+ 2 rows in set (0.00 sec)
Create a table with an index field
This will create a table with one index field.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create the table:
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1), FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3), index (FIELDNAMETOINDEX));
Replace:
- TABLENAME with the name you’d like to give the table.
- FIELDNAME1 with the name of the first field.
- TYPE1 with the type of the first field.
- ATTRIBUTE1 with the attribute(s) of the first field.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attributes of the second field.
- FIELDNAME3 with the name of the third field.
- TYPE3 with the type of the third field.
- ATTRIBUTE3 with the attributes of the third field.
- FIELDNAMETOINDEX with the name of the field you’d like to index (pick one of the fields you just created).
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into only some of the fields:
- MySQL will insert the data you typed into the field(s).
- MySQL will insert the NULL placeholder into the empty fields.
- If you do a complete insert and insert the NULL placeholder into any of the fields:
- MySQL will insert the NULL placeholder into the field(s) you specified.
- MySQL will insert the data you typed into the other fields.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
Example:
Let’s say I want to create the example Phone table, and I’d like the contact field to be indexed. I would type this command:
create table Phone (contact varchar(40) not null, homephone varchar(14), cellphone varchar(14), index (contact));
I could then verify that I indexed the contact field by typing this command:
show indexes from Phone;
MySQL would show me the indexes:
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Phone | 1 | contact | 1 | contact | A | NULL | NULL | NULL | | BTREE | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 1 row in set (0.00 sec)
Create a table with more than one index field
This will create a table with more than one index field:
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create the table:
- TABLENAME with the name of the table.
- FIELDNAME1 with the name of the first field.
- TYPE1 with the type of the first field.
- ATTRIBUTE1 with the attribute(s) of the first field.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attributes of the second field.
- FIELDNAME3 with the name of the third field.
- TYPE3 with the type of the third field.
- ATTRIBUTE3 with the attributes of the third field.
- FIELDNAMETOINDEX1 with the first name of a field you’d like to index (pick one of the fields you just created).
- FIELDNAMETOINDEX2 with the second name of a field you’d like to index (pick another of the fields you just created).
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1), FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3), index (FIELDNAMETOINDEX1, FIELDNAMETOINDEX2));
Replace:
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into only some of the fields:
- MySQL will insert the data you typed into the fields.
- MySQL will insert the NULL placeholder into the empty fields.
- If you do a complete insert and insert the NULL placeholder into any of the fields:
- MySQL will insert the NULL placeholder into the fields you specified.
- MySQL will insert the data you typed into the other fields.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
Example:
Let’s say I want to create the example Phone table and I’d like the contact and homephone fields to be indexed. I would type this command:
create table Phone (contact varchar(40) not null, homephone varchar(14), cellphone varchar(14), index (contact, homephone));
I could then verify that I indexed the contact and homephone fields by typing this command:
show indexes from Phone;
MySQL would show me the indexes:
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Phone | 1 | contact | 1 | contact | A | NULL | NULL | NULL | | BTREE | | | Phone | 1 | contact | 2 | homephone | A | NULL | NULL | NULL | YES | BTREE | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 2 rows in set (0.00 sec)
Create a table with a unique field
This will create a table with a unique field.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create the table:
- TABLENAME with the name of the table.
- FIELDNAME1 with the name of the first field.
- TYPE1 with the type of the first field.
- ATTRIBUTE1 with the attribute(s) of the first field.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attributes of the second field.
- FIELDNAME3 with the name of the third field.
- TYPE3 with the type of the third field.
- ATTRIBUTE3 with the attributes of the third field.
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1) unique, FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3));
Replace:
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type valid data into only one non-unique field:
- MySQL will insert the NULL placeholder into the unique field.
- MySQL will insert the data you typed into the non-unique field.
- MySQL will insert the NULL placeholder into the other non-unique field.
- If you do a partial insert and type valid data into both non-unique fields:
- MySQL will insert the NULL placeholder into the unique field.
- MySQL will insert the data you typed into the non-unique fields.
- If you do more than one partial insert in which you don’t fill in the unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a partial insert and type valid (unique) data into only the unique field:
- MySQL will insert the data you typed into the unique field.
- MySQL will insert the NULL placeholder into the non-unique fields.
- If you do a partial insert and type invalid (non-unique) data into only the unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a partial insert and insert the NULL placeholder into only the unique field:
- MySQL will insert the NULL placeholder into the unique field.
- MySQL will insert the NULL placeholder into the non-unique fields.
- If you do a complete insert and insert the NULL placeholder into the unique field and valid data into the non-unique fields:
- MySQL will insert the NULL placeholder into the unique field.
- MySQL will insert the data you typed into the non-unique fields.
- If you do a complete insert and type invalid (non-unique) data into the unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
Example:
Let’s say I want to create the example Phone table with no required fields, and I want to make the contact field unique so that no two names can be identical. I would type this command:
create table Phone (contact varchar(40) unique, homephone varchar(14), cellphone varchar(14), index (contact));
I could then insert all the data that’s in the example Phone table with this command:
insert into Phone values ("Hugo", "(800) 111-1111", "(900) 111-1111"), ("Lucas", "(800) 222-2222", "(900) 222-2222"), ("Spencer", "(800) 333-3333", "(900) 333-3333");
Let’s say I’m not aware that there’s already a Hugo in the database and I try to add a new record with another Hugo as the contact:
insert into Phone values ("Hugo", "(800) 444-4444", "(900) 444-4444");
MySQL would print an error message and refuse to add the record, since no two records may be the same in a unique field:
ERROR 1062 (23000): Duplicate entry 'Hugo' for key 1
Create a table with a unique required field
This will create a table with a unique, required field.
- Make sure you’re logged on.
- Choose the database you want to work with – creating it first, if needed.
- Type this command to create the table:
- TABLENAME with the name of the table.
- FIELDNAME1 with the name of the first field.
- TYPE1 with the type of the first field.
- ATTRIBUTE1 with the attribute(s) of the first field.
- FIELDNAME2 with the name of the second field.
- TYPE2 with the type of the second field.
- ATTRIBUTE2 with the attribute(s) of the second field.
- FIELDNAME3 with the name of the third field.
- TYPE3 with the type of the third field.
- ATTRIBUTE3 with the attribute(s) of the third field.
create table TABLENAME (FIELDNAME1 TYPE1(ATTRIBUTE1) not null unique, FIELDNAME2 TYPE2(ATTRIBUTE2), FIELDNAME3 TYPE3(ATTRIBUTE3));
Replace:
HOW THIS TABLE WILL BEHAVE:
- If you do a partial insert and type data into only one non-required field:
- MySQL will print a warning.
- MySQL will leave the required, unique field empty.
- MySQL will insert the data you typed into the non-required field.
- MySQL will insert the NULL placeholder into the other non-required field.
- If you do a partial insert and type data into both non-required fields:
- MySQL will print a warning.
- MySQL will leave the required, unique field empty.
- MySQL will insert the data you typed into the non-required fields.
- If you do more than one partial insert in which you don’t fill in the required, unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a partial insert and type valid data into only the required, unique field:
- MySQL will insert the data you typed into the required, unique field.
- MySQL will insert the NULL placeholder into the non-required fields.
- If you do a partial insert and type invalid (non-unique) data into the required, unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a partial insert and insert the NULL placeholder into the required, unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and type valid data into all of the fields:
- MySQL will insert the data you typed into all of the fields.
- If you do a complete insert and type invalid (non-unique) data into the required, unique field:
- MySQL will print an error message.
- MySQL will not add the record.
- If you do a complete insert and insert the NULL placeholder into the required, unique field:
- MySQL will print an error message.
- MySQL will not add the record.
Example:
Let’s say I want to create the example Phone table and I want to make the contact field unique so that no two names can be identical. I would type this command:
create table Phone (contact varchar(40) not null unique, homephone varchar(14), cellphone varchar(14), index (contact));
I could then insert all the data that’s in the example Phone table with this command:
insert into Phone values ("Hugo", "(800) 111-1111", "(900) 111-1111"), ("Lucas", "(800) 222-2222", "(900) 222-2222"), ("Spencer", "(800) 333-3333", "(900) 333-3333");
Let’s say I’m not aware that there’s already a Hugo in the database and I try to add a new record with another Hugo as the contact:
insert into Phone values ("Hugo", "(800) 444-4444", "(900) 444-4444");
MySQL would print an error message and refuse to add the record, since no two records may be the same in a unique field:
ERROR 1062 (23000): Duplicate entry 'Hugo' for key 1
Delete a cell
To delete a single cell in a record, you can update the record, setting the field the cell is in to “” to remove its contents:
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to delete a cell:
- TABLENAME with the name of the table.
- FIELDNAME1 with the name of the field the cell you want to delete is in.
- FIELDNAME2 with the name of the field you’d like to use to identify the record(s).
- CRITERION with the data that must be in that field to identify the record(s).
- Note that FIELDNAME2 can be the same as FIELDNAME1.
update TABLENAME set FIELDNAME1="carview.php?tsp=" where FIELDNAME2="CRITERION";
Replace:
| IMPORTANT |
|---|
| This will not make the cell NULL. To make the cell NULL, use this command instead:
update TABLENAME set FIELDNAME1=NULL where FIELDNAME2=”CRITERION”; |
Example:
To see how this works, take a look at the example Phone table in the Examples section. Let’s say Hugo no longer has a home phone, so I wish to remove his home phone entry from my table. I would type this command:
update Phone set homephone="carview.php?tsp=" where homephone="(800) 111-1111";
Or this command:
update Phone set homephone="carview.php?tsp=" where contact="Hugo";
My table would then look like this:
+---------+----------------+----------------+ | contact | homephone | cellphone | +---------+----------------+----------------+ | Hugo | | (900) 111-1111 | | Lucas | (800) 222-2222 | (900) 222-2222 | | Spencer | (800) 333-3333 | (900) 333-3333 | +---------+----------------+----------------+
Delete a database
- Make sure you’re logged on.
- Type this command to delete a database:
- DATABASENAME with the name of the database.
drop database if exists DATABASENAME;
Replace:
Example:
Let’s say I want to delete the People database. I can type this command:
drop database if exists People;
If the People database exists, MySQL will delete it.
Delete a field
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to delete a field:
- TABLENAME with the name of the table.
- FIELDNAME with the name of the field you’d like to remove.
alter table TABLENAME drop column FIELDNAME;
Replace:
Example:
To see how this works, take a look at the example Phone table in the Examples section. Let’s say I no longer wish to contact Hugo or his pals by cell phone. To remove the cellphone field from that table, I’d type this command:
alter table Phone drop column cellphone;
My table would then look like this:
+---------+----------------+ | contact | homephone | +---------+----------------+ | Hugo | (800) 111-1111 | | Lucas | (800) 222-2222 | | Spencer | (800) 333-3333 | +---------+----------------+
Delete a record or several records
- Make sure you’re logged on.
- Choose the database you want to work with.
- To delete all records that contain a specific criterion in a specific field, type this command:
- TABLENAME with the name of the table.
- FIELDNAME with the name of the field.
- CRITERION with the contents of the field you’d like to use to specify the record(s) to remove.
delete from TABLENAME where FIELDNAME="CRITERION";
Replace:
Example:
To see how this works, take a look at the example Address table in the Examples section. Let’s say I no longer want anything to do with Hugo. To remove him from the database, I’d type this command:
delete from Address where contact="Hugo";
My table would then look like this:
+---------+---------------+-------------+-------+-------+ | contact | street | city | state | zip | +---------+---------------+-------------+-------+-------+ | Lucas | 2 Main Street | Springfield | IL | 22222 | | Spencer | 3 Main Street | Salem | OR | 33333 | +---------+---------------+-------------+-------+-------+
Delete a table
- Make sure you’re logged on.
- Choose the database you want to work with.
- Remove a table by typing this command:
- TABLENAME with the name of the table.
drop table TABLENAME;
Replace:
Example:
Let’s say I want to delete the Phone table from the People database. I can type this command to choose the People database:
use People;
I can then type this command to get rid of the Phone table:
drop table Phone;
MySQL will delete the Phone table.
Delete all data from a table
This will delete all the data from a table, but not the table or its structure.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to delete all the data from a table:
- TABLENAME with the name of the table.
delete from TABLENAME;
Replace:
Example:
Let’s say I want to delete the data in the Phone table, which is in the People database. I can type this command to choose the People database:
use People;
I can then type this command to delete the data from the Phone table:
drop table Phone;
MySQL will delete all the data from the Phone table, but the table and its structure will still exist, so I can enter new data into it.
Display all databases
This will display a list of all the databases on your server.
- Make sure you’re logged on.
- Type this command:
show databases;
| IMPORTANT |
|---|
| Administrators will see all databases.
Users will see only databases they have access to. |
Display all data in a table (tabular output)
The use of ; at the end of a select statement will display the output as a table.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to display all the data in a table:
- TABLENAME with the name of the table.
select * from TABLENAME;
Replace:
Example:
To see all the data in the example Address table, I would type this command:
select * from Address;
MySQL would show me all the data in the table in table format:
+---------+---------------+-------------+-------+-------+ | contact | street | city | state | zip | +---------+---------------+-------------+-------+-------+ | Hugo | 1 Main Street | Jackson | MS | 11111 | | Lucas | 2 Main Street | Springfield | IL | 22222 | | Spencer | 3 Main Street | Salem | OR | 33333 | +---------+---------------+-------------+-------+-------+ 3 rows in set (0.00 sec)
Display all data in a table (block output)
The use of \G at the end of a select statement will display the output as a block of text with dividing header rows.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to display all the data in a table:
- TABLENAME with the name of the table.
select * from TABLENAME\G
Replace:
Example:
To see all the data in the example Address table, I would type this command:
select * from Address\G
MySQL would show me all the data in the table in block format:
*************************** 1. row ***************************
contact: Hugo
street: 1 Main Street
city: Jackson
state: MS
zip: 11111
*************************** 2. row ***************************
contact: Lucas
street: 2 Main Street
city: Springfield
state: IL
zip: 22222
*************************** 3. row ***************************
contact: Spencer
street: 3 Main Street
city: Salem
state: OR
zip: 33333
3 rows in set (0.00 sec)
Display all tables
This will display all the tables in the current database.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to show all the tables in the database:
show tables;
Display current database
This will display which database you’re currently using.
- Make sure you’re logged on.
- Type this command:
select database();
Display indexes in a table
This will show the indexes in a table, if it has any.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to show the indexes in a table:
- TABLENAME with the name of the table.
show indexes from TABLENAME;
Replace:
Example:
Let’s say I had used this command to create the example Phone database with the contact field indexed:
create table Phone (contact varchar(40) not null, homephone varchar(14), cellphone varchar(14), index (contact));
I could then display the indexes of the Phone table with this command:
show indexes from Phone;
MySQL would show me that the contact field is indexed:
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | Phone | 1 | contact | 1 | contact | A | NULL | NULL | NULL | | BTREE | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
Display table structure
This will display the structure of the specified table.
- Make sure you’re logged on.
- Choose the database you want to work with.
- If you’re not sure of the table name, display all the tables in the current database.
- Type this command to display the table structure:
- TABLENAME with the name of the table.
describe TABLENAME;
Replace:
Example:
Let’s say I want to look at the structure of the example Phone table, which should have the contact field as a required field. I can type this command:
describe Phone;
MySQL will show me the structure of the table:
+-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | contact | varchar(40) | NO | | NULL | | | homephone | varchar(14) | YES | | NULL | | | cellphone | varchar(14) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
I can tell from the Null field that the contact field is not null, which is exactly what I want it to be.
| IMPORTANT |
|---|
| When doing the describe TABLENAME; command, note that the Default field is misleading. It lists NULL as the default for all fields. To check whether you’ve defined a field as null or not null, check whether it says YES or NO in the Null field. |
Edit a record or several records in a table (simple)
This will allow you to edit a record or several records in a table. This example uses one criterion.
- Make sure you’re logged on.
- Choose the database you want to work with.
- Type this command to edit the record(s) in a table:
- TABLENAME with the name of the table.
- FIELDNAME1 with the name of the field the cell(s) you’d like to change is/are in.
- NEWVALUE with the new contents for the cell(s) in that field.
- FIELDNAME2 with the name of the field to use for determining which record(s) to change.
- CRITERION with the data that must be in that field to determine which record(s) to change.
update TABLENAME set FIELDNAME1="NEWVALUE" where FIELDNAME2="CRITERION";
Replace:
Example
To see how this works, take a look at the example Phone table in the Examples section. Let’s say that Hugo changed his name to Hugh. If I wished to change every instance of “Hugo” to “Hugh” in my table, I would type this command:
update Phone set contact=”Hugh” where contact=”Hugo”;


thanks for the post
Comment by linuxhospital — October 10, 2012 @ 10:32 am