MySQL Field Types
I can't remember the ranges for various MySQL numberic data types, such as
TINYINT, SMALLINT, MEDIUMINT, INT, etc. nor how many characters a TEXT field
hods versus a MEDIUMTEXT field and when I need the information it sometimes
takes me a little while to find it again, so I've placed a page listing
those values on this site for ready reference.
[ More Info ]
[/software/database/mysql]
permanent link
Backing Up a MySQL Database
The
mysqldump command can be used to backup a
MySQL database.
The mysqldump command will write its output, which consists of SQL statements
that can be used to recreate the database, to standard output by default;
you can use
> filename to redirect its output to a file.
The format of the command to backup one or more databases is
mysqldump --databases db1 db2 db3 > outputfile.sql where
db1,
db2, etc. are the names of the databases
you wish to back up and
outputfile.sql is the name of the output
file to which the dababase will be dumped.. The
--databases
option causes all names on the command line to be treated as database
names. Without this option, mysqldump treats the first name as a database
name and those following as table names.
# mysqldump -p --databases partsdb > partsdb-20111016.sql
Enter password:
In the above example, the -p option causes mysql to prompt
for the mysql account password with the username assumed to be the same as
the login name for the account from which you are running the command.
If you wish to specify the username, use --user=user_name or
-u user_name.
If you want to back up all databases you can use the command
mysqldump --all-databases > dump.sql to back up all databases
to which the account has access on the system to a file named dump.sql
in this case.
A dump file can be used in several ways:
-
As a backup to enable data recovery in case of data loss.
-
As a source of data for setting up replication slaves.
-
As a source of data for experimentation:
An example script to dump mysql databases to a gzip file which will be
transferred to another server via SSH for safekeeping can be found at
How to have a rock solid Linux backup without pro budget. Placing
you dump file on another system ensures you don't lose your data should
the system on which your databases reside suffer a hard drive crash or some
other catastrophe.
References:
-
6.4.1. Dumping Data in SQL Format with mysqldump
MySQL :: Developer Zone
-
6.4. Using mysqldump for Backups
MySQL :: Developer Zone
-
How to have a rock solid Linux backup without pro budget
documentation planet
[/software/database/mysql]
permanent link
Show Warnings in MySQL
You can use the
SHOW WARNINGS statement in MySQL to show
the error, warning, and note messages that resulted from the last
statement that generated messages in the current session. It shows nothing
if the last statement used a table and generated no messages. (That is, a
statement that uses a table but generates no messages clears the message
list.) Statements that do not use tables and do not generate messages
have no effect on the message list.
For example, if I issue the statement DROP TABLE IF EXISTS
smf_testing statement as below, I see a notice that 0 rows were
affected, but there was 1 warning. To see the warning, I can immediately
afterwards issue the statement SHOW WARNINGS. I see that the
warning was simply indicating that the table I asked be dropped, didn't
actually exist.
mysql> DROP TABLE IF EXISTS smf_testing;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+-------+------+-----------------------------+
| Level | Code | Message |
+-------+------+-----------------------------+
| Note | 1051 | Unknown table 'smf_testing' |
+-------+------+-----------------------------+
1 row in set (0.00 sec)
mysql>
References:
-
12.4.5.41. SHOW WARNINGS Syntax
MySQL :: Developer Zone
[/software/database/mysql]
permanent link
Copying Table Structure
If you want to create a new table in an MySQL database that has the same
structure, i.e., the same fields (columns) as an existing table, you can
use the command below:
create table New_Table like Existing_Table;
New_Table = the name you wish to give to the new table
Existing_Table = the name of the existing table
Remember to use the same case of letters for the existing table name as
were actually used for that table name. E.g., if the table name is
"Existing_Table" use "Existing_Table" not "existing_table"
[/software/database/mysql]
permanent link
Installing and Configuring MySQL on a Linux System
MySQL is free
Database Management System (DBMS) software that runs on a variety of
platforms, including Microsoft Windows, Linux, and Unix. To install
and configure the software on a Linux system, so that it starts
when the system boots follow
these
instructions. It is important to set a root password after you've
started the MySQL daemon, so be sure to do so.
Once you have the software installed and configured, you can set up a
new database using the instructions in
Creating a MySQL
Database.
[/software/database/mysql]
permanent link
Adding a Column to a MySQL Database
To add a column to a MySQL database, you can use the following steps:
- Start the text-based MySQL client
$ mysql -u testacct -p
The -u testacct parameter specifies that the client should
be started using the account named testacct, while the
-p parameter indicates that the system should prompt you
for the password.
- At the mysql> prompt, enter the command use dbname;, where dbname is the database name. If you don't know
the name of the database, you can see a list of available databases with
the show databases; command.
-
Use the alter table command to modify the appropriate table. If
you need to see a list of tables in the database, you can use the show
tables; command. E.g. to add a column,
delivered, which
will hold a delivery date for a shipment, to the table requests,
you could use the command below:
ALTER TABLE requests ADD delivered DATE;
That would put the new column at the end of the existing columns.
If you want to add the column after a specific column, you can specify that
column with AFTER colname. E.g., suppose I wish to add the
column delivered after the column orderdate.
I could use the command below:
ALTER TABLE requests ADD delivered DATE AFTER orderdate;
If you don't know the names of the existing columns, you can use the
command SHOW COLUMNS FROM dbname;. E.g., if the table is
named requests, I could use the command below:
mysql> show columns from requests;
References:
-
Add a column to an existing MySQL table
Created: February 8, 2004
Updated: July 17, 2004
tech-recipes
[/software/database/mysql]
permanent link