MySQL数据库基础命令

mysql -uroot -p  进入数据库

root@spliu-vi:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, 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.
create database name; 创建数据库

mysql> create database test;
Query OK, 1 row affected (0.13 sec)
create table 表名 (列名 类型(长度),列名 类型(长度));  创建表

mysql> create table name (names VARCHAR(20),sex CHAR(1));
Query OK, 0 rows affected (0.38 sec)
insert into 表名 values(“值”,”值”);  向表内插入数据

mysql> insert into name values(“spliu”,”m”);
Query OK, 1 row affected (0.00 sec)
describe name;  查看表结构

mysql> describe name;
+——-+————-+——+—–+———+——-+
| Field | Type        | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| names | varchar(20) | YES  |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
+——-+————-+——+—–+———+——-+
2 rows in set (0.10 sec)
select * from name;  查看表内容

mysql> select * from name;
+——-+——+
| names | sex  |
+——-+——+
| spliu | m    |
+——-+——+
1 row in set (0.03 sec)
alter table 表名 modify column 列名 新的列类型;  更改列类型

mysql> alter table tests modify column sex char(6);
Query OK, 5 rows affected (0.19 sec)
Records: 5  Duplicates: 0  Warnings: 0
alter table 表名 change 原字段名 新字段名 字段类型 是否允许非空;  更改表的字段名

mysql> describe tests;
+———+———+——+—–+———+——-+
| Field   | Type    | Null | Key | Default | Extra |
+———+———+——+—–+———+——-+
| name    | char(6) | YES  |     | NULL    |       |
| sex     | char(6) | YES  |     | NULL    |       |
| ranking | int(1)  | YES  |     | NULL    |       |
+———+———+——+—–+———+——-+
3 rows in set (0.00 sec)
mysql> alter table tests change sex sexs char(6) null;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> describe tests;
+———+———+——+—–+———+——-+
| Field   | Type    | Null | Key | Default | Extra |
+———+———+——+—–+———+——-+
| name    | char(6) | YES  |     | NULL    |       |
| sexs    | char(6) | YES  |     | NULL    |       |
| ranking | int(1)  | YES  |     | NULL    |       |
+———+———+——+—–+———+——-+
3 rows in set (0.00 sec)
update 表名 set 更改值得列 = ‘更改的值’ where 主键列 = ‘主键’;

mysql> select * from tests;
+——–+——+———+
| name   | sexs | ranking |
+——–+——+———+
| jsu    | w    |       1 |
| ynpeng | man  |       2 |
| hhpeng | man  |       3 |
| jjdeng | man  |       4 |
| spliu  | man  |       5 |
+——–+——+———+
5 rows in set (0.00 sec)
mysql> update tests set sexs = ‘woman’ where name = ‘jsu’;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tests;
+——–+——-+———+
| name   | sexs  | ranking |
+——–+——-+———+
| jsu    | woman |       1 |
| ynpeng | man   |       2 |
| hhpeng | man   |       3 |
| jjdeng | man   |       4 |
| spliu  | man   |       5 |
+——–+——-+———+
5 rows in set (0.00 sec)
show databases; 查看所有数据库

mysql> show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+——————–+
5 rows in set (0.00 sec)
use test; 选择数据库

mysql> use test;
Database changed
select database();  查看当前数据库
mysql> select database();
+————+
| database() |
+————+
| test       |
+————+
1 row in set (0.00 sec)
drop database test 直接删除数据库

mysql> drop database test ;
Query OK, 1 row affected (0.18 sec)
show tables; 显示表

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| name           |
+—————-+
1 row in set (0.00 sec)
select version(),current_date; 显示当前mysql版本和当前日期

mysql> select version(),current_date;
+————————-+————–+
| version()               | current_date |
+————————-+————–+
| 5.7.19-0ubuntu0.16.04.1 | 2017-08-11   |
+————————-+————–+
1 row in set (0.09 sec)
flush privileges; 刷新数据库

mysql> flush privileges;
Query OK, 0 rows affected (0.15 sec)
exit;  退出数据库
posted @ 2018-05-02 09:21  阿鹏2019  阅读(125)  评论(0编辑  收藏  举报