1. 连接mysql数据库:
输入:
mysql> mysql -uroot -p123(注意后面没有分号!!)
输出:
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.7.10-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2. 查看所有数据库:
输入:
mysql> show databases;
输出:
+--------------------+
| Database |
+--------------------+
| information_schema |
| dedecms |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
3. 创建数据库:
输入:
mysql> create database school;
显示:
Query OK, 1 row affected (0.03 sec)
4. 删除数据库:
输入:
mysql> drop database school;
输出:
Query OK, 0 rows affected (0.27 sec)
5. 进入某个数据库:
输入:
mysql> use test;
输出:
Database changed
6. 创建表:
输入:
(顺序为:字段名,数据类型(数据字节数),是否为空,是否为主键,自动增加,默认值。)
mysql> create table class(
-> id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> sex int(4) not null default '0',
-> degree double(16,2));
输出:
Query OK, 0 rows affected (0.62 sec)
7. 向表中插入数据:
输入:
(insert into最好带上into;可同时插入多条数据,但是要对应。)
mysql> insert into class(name,degree) values('jia',98),('chen',76);
输出:
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0
8. 查询表:
输入:
mysql> select * from class;
输出:
+----+------+-----+--------
| id | name | sex | degree
+----+------+-----+--------
| 1 | jia | 0 | 98.00
| 2 | chen | 0 | 76.00
+----+------+-----+--------
2 rows in set (0.00 sec)
9. 更改表中数据
输入:
mysql> update class set degree='100' where id = 2;
输出:
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
10. 删除表中数据
输入:
mysql> delete from class where id = 2;
输出:
Query OK, 1 row affected (0.45 sec)
11. 查看表结构
输入:
mysql> desc class;
(另外还有这几个:show columns from 表名;describe 表名;show create table 表名;)
输出:
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| sex | int(4) | NO | | 0 | |
| degree | double(16,2) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
12. 增加字段
输入:
mysql> alter table class add age int not null;
13. 删除字段
输入:
mysql> alter table class drop age;
14. 修改字段
输入:
mysql> alter table class change age sex int not null;
15. limit用法
输入:
mysql>SELECT * FROM users ORDER BY id LIMIT 5;
(选择前5行数据)
输入:
mysql>SELECT * FROM users ORDER BY id LIMIT 5,10;
(从第5行开始,选择10行数据)