You are on page 1of 5

Table 12-5: Commands in MySQL Command Task create <database name> To create a database with the specified name

drop <database name> To delete the specific database along with all its tables extended-status To display the extended status information from the server flush-tables To clear all the table information flush-host To clear all the cached information flush-threads To clear all the stored thread information flush-logs To clear all the logged information about the server refresh To clear all the table information and to refresh all the logs ping To check and monitor connectivity with the mysql daemon variables To print all the information about variables status To display the status information at the command prompt version To display the version information from the server shutdown To shut down the server password To change the password

//////////////////////////////////////////////////////////////////////////////// ///////// If password not installed press inter in command prompt; To know about databases present: mysql> show databases; To know who u are logged in as: mysql> select user ();

Creating Databases In the previous section, you learned that you can create <database> command at the command prompt. ways execute this command from outside MySQL. You d from inside MySQL. The syntax of the command is ate database Books

create a database by using the However, you do not need to al can also use the create comman as follows:

mysql> create database if not exists Books;// or cre Caution It is necessary to end all the MySQL commands with a semicolon (;) to indicate the conclusion of the command. Otherwise, MySQL keeps on waiting for a semicolon or returns an error.

mysql> drop database if exists Manuscript;// drop databaes books; Using a Database As explained earlier, the first step in creating a table is to obtain access to the database in which you want to insert the table. You use the syntax given bel ow to access a database. At the MySQL prompt, enter the code given below. mysql> use Books Field Datatypes in MySQL As you already know, tables contain a set of fields that you can use to store va lues. The field types are the datatypes that determine the type of data that can be stored in each field. These field types and their description are listed in Table 12-6. Table 12-6: Field Datatypes in MySQL Type Description char Used to store the string type data. varchar Used to store string values but of a larger size than a char datatype. blob/text Used to store strings of characters. The fields can store a maximum of 65535 ch aracters. int Used to store integer type data. The maximum size of the datatype is 2147483646 numeric values. bigint Used to store integer values. This is similar to int, the only difference is th at you can store a maximum of 9223372036854775806 numeric values. smallint Used to store small integer values. A smallint field can store a maximum of 327 66 numeric values. tinyint Used to store integer values. This is the smallest of the integer datatypes and

can store a maximum of 126 numeric values. float Used to store decimal values. date Used to store date values. The values are stored in the yyyy-mm-dd format and r ange from 1000-01-01 to 9999-12-31. datetime Used to store the time along with the date. year Used to store the year value.

Using the create Command to Construct Tables mysql> create tablename (first_fieldname fieldtype, second_fieldname fieldtype,...,n_fieldname fieldtype);//create table table_name (......... create table Products ( Productid bigint(20) NOT NULL auto_increment, Name varchar(40) NOT NULL default ', Description varchar(200) NOT NULL default ', Price varchar(20) NOT NULL default ', Category varchar(20) NOT NULL default ', PRIMARY KEY (Productid) );

Viewing the Table in a Database mysql> show tables; Viewing the Table Structure mysql> explain Products; Entering Data into a Table mysql> insert into <tablename> values value_for_fieldtwo', ....'value_for_nthfield' ); Let's use the above syntax to insert new records in the table. ( value_for_fieldone', ( ', mysql> insert into Products values Written by Charles Dickens','200', Classic'); ... mysql> insert into Products values ( ', Last of the Mohicans', Written by Charles Dickens', 150', Classic'); Great Expectations', Viewing the Data in a Table mysql> select [fieldname] from [tablename] where [expression] order by [fieldname];

mysql> select * from Products; Complex select Statements mysql > select * from Products where Price =200; Table 12-7: Comparison Operators Operator Purpose == Equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to != Not equal to like Compares string text mysql >select distinct Category from Products; mysql > select Productid from Products where Category = Fiction'OR Price >100 AND Price<150; Another clause that can be used along with the select statement is the group by clause. You use this clause to group the records containing similar data in spec ific columns. Once the data is grouped, you can use aggregate functions such as sum and max to perform calculations on this data. For example, in the code given below, all the records are grouped together based on the Category field and the maximum price in each Category is displayed. mysql > max(Price), Product_id, Name from Products GROUP BY Category;??????????????? Deleting Data from a Table The syntax of the command for deleting a record is given below. mysql> delete <fieldname> from <tablename> [where expression]; You can use the code given below to delete the records of all the products whose

price is below 200. mysql> delete from Products where Price<200; Modifying the Table Structure You can modify the structure of a table by using the alter command. You can perf orm the following tasks by using the alter command. Adding a column to a table mysql> alter table <tablename> add <new field> <definition> ; Changing the datatype of a table mysql> alter table <tablename> change <columnname> <newdefinition>; Indexing a table mysql> alter table <tablename> add index <columnname> (<columnname>) ; Adding a unique column to a table mysql> alter table <tablename> add unique <columnname> (<columnname>) ; Deleting a column from a table mysql> alter table <tablename> drop <columnname>;

You might also like