Wamp MySQL Console

Source: Internet
Author: User

SQL command Check of MySQL open-source database

1. Install wamp5.xx
Open the installation file in the compressed package and install it on the Windows2000 Server or Windows XP
The installation directory is tentatively set to D:/Wamp
/*************************************** ********************/
2.1 log on to the MySQL console [the user name is root, and the password is blank by default]
Switch to the MySQL bin directory cd d:/Wamp/MySQL/bin
Log on to the MySQL Console
Syntax: mysql-H hostname-u username-P
Connect to another host
Code: mysql-H 192.168.1.1-u root-P
Password: Press enter.
Connect to Local Machine
Code: mysql-H 127.0.0.1-u root-P
Password: Press enter.
Mysql> indicates that the logon to the MySQL console is successful.

2.2 log out of the MySQL Console
Mysql> exit;
/*************************************** ********************/
3.1 check the list of all MySQL databases;
Syntax: mysql> show databases;
Code: mysql> show databases;

3.2 display the list of all tables in the database
View tables in the current database
Syntax 1: mysql> show tables;
Code 1: mysql> show tables;

3.3 View tables in other databases jxc
Syntax 1: mysql> show tables from databasename;
Code 1: mysql> show tables from jxc;
/*************************************** ********************/
4. Create/delete/select a database
Create a jxc database:
Syntax: mysql> Create Database databasename;
Code: mysql> Create Database jxc;
Delete A jxc database:
Syntax: mysql> drop database databasename;
Code: mysql> Create Database ABC;
Code: mysql> drop database abc;
Select jxc database:
Syntax: mysql> use database;
Code: mysql> Use jxc;
/*************************************** ********************/
5. view the data structure of a table
5.1 describte View table MERs Structure
Syntax 1: mysql> describe tablename;
Code 1: mysql> describe MERs MERS;
5.2.show columns view the customers table structure
Syntax 1: mysql> show columns from tablename;
Code 1: mysql> show columns from MERs MERS;

. View the data structure of the specified column name in a table
Syntax 1: mysql> show index from tablename column;
Code 1: mysql> show index from MERs name;

5. 4. view the customers index of a table
Syntax 1: mysql> show index from tablename;
Code 1: mysql> show index from MERs MERS;

6. Common Data Operations (select, insert, update, delete)
6.1 select:
Syntax: Select * from [Table Name 1, table name 1,] where [condition range]
Code: Select * from orders where orderid> 100;

6.2 insert
Syntax: insert into Table1 (column1, column,) values (value1, value2 ,,,);
Code: insert into books (ISBN, author, title, price) values ('iso-000000', 'jahn. D', 'mysql6. 0', 902126 );

6.3 Update:
Syntax: Update Table1 set [column name] = [new data] Where [condition range]
Code: Update books set Title = "thinking in Java" where ISBN = 'iso-902126 ';

6.4 delete:
Syntax: delete from [Table name] Where [condition range]
Code: delete from books where ISBN = 'iso-902126 ';

6.5 other methods
Query: Select * From Table1 where field1 like '% value1 %' --- the like syntax is exquisite.
Sort: Select * From Table1 order by field1, field2 [DESC]
Total: Select count as totalcount from Table1
Sum: Select sum (field1) as sumvalue from Table1
Average: Select AVG (field1) as avgvalue from Table1
MAX: Select max (field1) as maxvalue from Table1
Min: select Min (field1) as minvalue from Table1

/*************************************** ********************/

7. Use grant to create database users and permissions
Grant command syntax:
Grant [permission List 1], [permission List 2]
On [database. Table name]
To [user name @ host name]
Identified by 'Password ';

Code Implementation 1:
Grant select, insert, delete, update
On discuz. * to Jake @ localhost


Identified by '20140901 ';
Function Description
Select, insert, delete, and update permissions for all tables in the discuz Database
Add the password to the new user, "201314 ';

Code Implementation 2:
Grant all
On discuz. * to Tom @ localhost


Identified by '20140901 ';
Add all the tables in the database discuz permission to the new user Tom with the password '123456 ';

[Permission List 1] Options:
Select Table, column
Insert table, column
Udpate Table, column
Delete table
Index Table
ALTER TABLE
Create Database, table
Drop database, table

[Permission List 2] Options:
Create temporary tables allow the use of the temporary keyword
File allows the database to import and export data to files
Lock tables allows the use of the lock talbes command
Reload allows re-loading the authorization table
Show databases allows you to view all database lists
Shutdown allows you to disable MySQL

All.
Usage allows logon only, but does not allow any operation

The [database. Table name] option is as follows:
Database. Select a table in the database for the XX user
Database. * select all tables in the database to XX users.

/*************************************** ********************/
8. Revoke cancels user and User Permissions
Revoke format:
Revoke [permission List 1], [permission List 2] privileges, [columns]
On [database. Table name]
From [user name @ host name]

Code:
Grant permissions to laoliu first (laoliu)
Grant all
On books .*
To laoliu
Identified by 'laoliu11 ';

Revoke some Permissions
Revoke alter, create, drop
On books .*
From laoliu;
All permissions of laoliu
Revoke all
On books .*
From laoliu;

/*************************************** ********************/
9. Add other methods for mysql users
Shell> mysql-u root-p1234 MySQL
Mysql> insert into user (host, user, password) values ('localhost', 'backup ', 'databse ');
Add a MySQL user backup from the local machine with the password 1234

Shell> mysql-u root-P
Mysql> grant file on *. * To backup@192.168.1.200

Identified by '20140901 ';
Mysql>/exit
Open an account backup password 1234 to the IP address 192.168.1.200 with the permission to process the file

/*************************************** ********************/
10. Create/modify/delete a table // optimize a table

10.1 create a table
Syntax: Create Table tablename (columns ,...)
Code:
Create Table order_items
(Orderid int unsigned not null,
ISBN char (13) not null,
Quantity tinyint unsigned,
Primary Key (orderid, ISBN)
);

10.2 modify a table
10.2.1 Add/delete a column
Syntax: alter table [Table name] add column [column name] [type];
Add a remark column to the table
Code: alter table order_items add column remark char (50 );
Delete a column
Syntax: alter table [Table name] Drop column [column name];
Delete A Remark column from the table
Alter table order_items drop column remark;

10.2.2 Add/delete a primary key
Add orderid and ISBN as the primary key
Syntax: alter table [Table name] add primary key [column name 1, column name 1];
Code: alter table order_items add primary key (orderid, ISBN );
Delete primary key
Syntax: alter table [Table name] Drop primary key
Code: alter table tabname drop primary key

10.2.3 create/delete an index
Create an index
Syntax: Create index [index name] on [Table name] (column name );
Code: Create index orderid_ix on orders (orderid );
Delete Index
Syntax: drop index [index name] on [Table name] (column name );
Code: drop index orderid_ix on orders;

10.3 delete a table:
Delete table
Syntax: Drop table [Table name]
Code: Drop table orders;

10.4 optimization table:
When there are tens of thousands of rows of data in a table, the access speed is slow and they must be optimized.
The common method is to create an optmize. SQL file,
Import the optimization script file directly to optimize some key tables in batches to speed up access.

Optimize table MERs data (customers)
Syntax: mysql> optmize table tablename;
Code: mysql> optmize table MERs MERS;

10.5 use the command line to load a new_tb. SQL File
This allows MySQL to execute SQL statements in batches in the * SQL file.
1. Complete the SQL command set in the text file and copy it to the command line for one-by-one execution.
2. If there are too many tables, save them as *. SQL files and load the files with commands.

Format: mysql-H [Host IP address]-U [user name]-d [database name]-P <[name of *. SQL file in this directory]
Run cmd
Cd d:/Wamp/MySQL/bin
Mysql-H 127.0.0.1-u root-D pubs-P <new_tb. SQL;
MySQL is used to load the D:/Wamp/MySQL/bin/new_tb. SQL file to the books database,
Note: The database pubs must exist and-D must be capitalized.

New_tb. SQL file content (more than 1000 SQL commands can be stored in this file)
Create Table MERs
(Customerid int unsigned not null auto_increment primary key,
Name char (50) not null,
Address char (100) not null,
City char (30) not null
);

Create Table orders
(Orderid int unsigned not null auto_increment primary key,
Customerid int unsigned not null,
Amount float (6, 2 ),
Date not null
);

Create Table books
(ISBN char (13) not null primary key,
Author char (50 ),
Title char (100 ),
Price float (6, 2)
);

Create Table order_items
(Orderid int unsigned not null,
ISBN char (13) not null,
Quantity tinyint unsigned,
Primary Key (orderid, ISBN)
);

Create Table book_reviews
(ISBN char (13) not null primary key,
Review text
);

After running OK, check whether the checklist is automatically created?
C:> mysql-H 127.0.0.1-u root-P
Mysql> show tables from pubs;
The results show that the above five tables have been created with OK;
/*************************************** ********************/

11. Create and delete a table View
Create View
Syntax: Create iview [view name] as [SELECT statement );
Code: Create view v_orders as select * from orders;
Delete View
Syntax: Drop iview [view name]
Code: Create view v_orders

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.