|
|
|
Mysql Command Line UtilityAuthor: Mike Walton MySQL is a very powerful open source database which means that it is free to use. One of the many tools that come with MySQL is their MySQL Command Line that will allow you to do pretty much everything from create a database to add and edit entries in the database. This article will help you to begin using MySQL Command Line Successfully. When you install MySQL you will receive a command line application where you can create new databases, modify current databases, and delete databases. Once you launch the MySQL Command Line Client you will be prompted for a password assuming you set one up when you installed MySQL. From here you will be given a mysql> prompt and you will be able to enters your commands. Lets start with creating a database in our system: mysql> CREATE DATABASE temp; Be sure that when creating databases, tables, and fields that the names are case sensitive and cannot include spaces. In order to edit this new database we have created we need to switch to it and that is where the USE command comes into play. mysql> USE temp; Now that we have a working database lets create a table in the database to store our relatives mysql>CREATE TABLE family (name char(25), age integer, relation char(50)); We just created a table that contains three fields so lets go into a little more detail about that command we just ran. First we used the CREATE TABLE command which does exactly what it says does and it creates the table. Where we typed in family is the name of the table that we just created. Everything else within the parenthasis are fields in the table and what type of information is going to be stored in that field. For example that name char(25) tells SQL to create a field within the table called “name” and the char(25) tells it that it will be a character field meaning you can type any character you want in it and that it is 25 characters long. The age integer tells SQL to create a field in the table called age and that it will be a integer meaning that only whole numbers are stored in this field. You can add a size to the integer field like when using char but it is not nessecary as the default length of an interger field is 11. The last one is just like the first one realtion char(50) except for the fact that we made this field able to hold 50 characters instead of 25. Now that we have created a database and a table lets look at a coupld show commands to see what we have. The show commands are pretty straight forward in terms of what they do. Lets start with SHOW DATABASES; mysql> SHOW DATABASES; +—————-+ 3 rows in set (0.01 sec) You can see that the SHOW DATABASES command will print you a list of all the databases on the system so can you guess what the SHOW TABLES command will do once you have used the USE temp command. Yep you guessed it, it will show you a list of all the tables in the database temp. mysql> SHOW TABLES; +———————-+ Lets keep going deeper with the SHOW commands and lets look at the table itself and see all of the fields we have. We will simply use the SHOW COLUMNS FROM family command. mysql> SHOW COLUMNS FROM family; +———-+————-+——-+——–+————+———+ Now that we have create a database and a table and confirmed that they are there and setup the way we would like, lets add some information to the table because a database is not good without information. If you have every done any MySQL and PHP programming these next 2 commands should look very familiar. Lets start by adding in some people into the table by using the INSERT INTO table command. We are going to add Derek who is 22 and my cousin, Chelsie who is 21 and my cousin, and Leslie who is 27 and my sister. mysql>INSERT INTO family (name, age, relation) VALUES (’Derek’, 22, ‘Cousin’); mysql>INSERT INTO family (name, age, relation) VALUES (’Chelsie’, 21, ‘Cousin’); mysql>INSERT INTO family (name, age, relation) VALUES (’Leslie’, 27, ‘Sister’); Now that we have added our information lets take a look at it and confirm it is all there. We will be using the SELECT command to view content in the table. mysql> SELECT * FROM family; +———–+——+————-+ We can also use the SELECT command to order our list or to only pick a specific person. mysql> SELECT * FROM family ORDER BY name; +———–+——+————-+ As you can see by simple adding ORDER BY name we were able to resort the information in the table by name. Now lets pull a specific person from the table mysql> SELECT * FROM family WHERE name=’Leslie’; +———+——+————-+ By qualifying what we want name to be equal to we can pull specific records out of the table. Article Source: http://www.articlesbase.com/databases-articles/mysql-command-line-771316.html About the Author: Mike Walton has been in the technology field for over 8 years and has 6+ years in hospitality technolgy. Mike has experience with Microsoft Windows Server 2000, 2003, Windows 98, XP, and Vista, Networking, Cisco Equipment, PCI DSS, and many more. Mike Walton is also the founder of MikeNet PC Free Articles and Videos. Related Posts Comments |