MySQL基本操作

请参考教程:MySQL 教程

 

MySQL一般默认不启用table键命令补全。为了启用命令补全功能,可以修改/etc/my.cnf 或者 /etc/mysql/my.cnf 文件。添加:(目前最新版本应该不支持,添加后mysql无法启动)

[mysql]
auto-rehash

 

1. 启动mysql

service mysql start

 

2. 链接mysql

$ mysql -u root -p
Enter password:

 

3. 用户设置

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    -> ON TUTORIALS.*
    -> TO 'zara'@'localhost'
    -> IDENTIFIED BY 'zara123';

 

mysql> show grants for 'zara'@'localhost';

 

4. 查看database

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

 

5. 

以下列出了使用Mysql数据库过程中常用的命令:

  • USE 数据库名 :选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库。
  • SHOW DATABASES: 列出 MySQL 数据库管理系统的数据库列表。
  • SHOW TABLES: 显示指定数据库的所有表,使用该命令前需要使用 use 命令来选择要操作的数据库。
  • SHOW COLUMNS FROM 数据表: 显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。
  • SHOW INDEX FROM 数据表: 显示数据表的详细索引信息,包括PRIMARY KEY(主键)。
  • SHOW TABLE STATUS LIKE 数据表\G: 该命令将输出Mysql数据库管理系统的性能及统计信息。

6. 新建database

create database [new database];

DROP database if exists test;

CREATE DATABASE `test`
    DEFAULT CHARACTER SET utf8
    COLLATE utf8_general_ci;

 

7. 删除database

drop database [existed database];

drop database if exists [database not sure if exist];

8. 使用database

use [one database];

9. 新建table

create table student(NO char(20),name varchar(20),primary key(NO));

10. 删除table

drop table [a table];

11. table重命名

rename table [old name] to [new name]

12. table插入数据

insert into student(NO,name) values('2012001','Jodan');

 

13. 导入sql文件:

mysql> source /home/xxxx/db_setup.sql;

 

14. 查看表结构:

mysql> desc users;

mysql> describe users;
mysql> show columns from users;
mysql> show create table users;

mysql> use information_schema;
mysql> select * from columns where table_name='users';

 

15. autocommit:

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set autocommit=0;             # 0 OFF, 1 ON
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

 

16. 事务:

BEGIN       开始一个事务
ROLLBACK    事务回滚
COMMIT      事务确认      

 

posted @ 2015-12-16 23:35  raindream  阅读(198)  评论(0)    收藏  举报