我主要是在维护OpenStack云平台的时候会涉及MySQL数据库的操作,这里就跟大家分享一下常用的简单命令,也为自己做个小练习。
1.登录MySQL数据库
mysql -h localhost -u root -p 123456
其中,-h:mysql服务器的IP地址或主机名,如果是本机登录,该选项可以省略;
-u:登录用户名
-p:登录用户对应的登录密码
2.查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
3.删除数据库
命令格式:DROP DATABASE 数据库名;
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (0.00 sec)
4.创建数据库
命令格式:CREATE DATABASE 新建数据库名;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (0.00 sec)
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
5.选定操作数据库
命令格式:USE 数据库名;
mysql> use test;
Database changed
6.在选定的数据库中创建表单
table的创建其实不是个简单的操作,涉及实体完整性、参照完整性等条件,下面对常用命令进行下介绍。
1)创建table
命令格式:CREATE TABLE 表名
->(
->列名1 数据类型(完整性约束条件),
->列名2 数据类型(完整性约束条件),
->列名3 数据类型(完整性约束条件)
->);
mysql> create table students(id int(10),name char(20),sex char(10)); Query OK, 0 rows affected (0.07 sec) mysql> create table course(id int(10),name char(20)); Query OK, 0 rows affected (0.07 sec)
上述示例中,完整性约束条件为数据长度,属于比较简单的一种。
2)完整性约束条件说明
PRIMARY KEY | 标识该属性为该表的主键,可以唯一标识一组记录 |
FOREIGN KEY | 标识该属性为该表的外键,是与该表形成关联的某一表的主键 |
NOT NULL | 标识该属性的值不能为空 |
UNIQUE | 标识该属性的值是唯一的 |
AUTO_INCREMENT | 标识该属性的值是自动增加的 |
DEFAULT | 为该属性设置默认值 |
3)设置table的主键
单字段主键格式:列名 数据类型 PRIMARY KEY,或者如多字段主键格式
mysql> create table course(id int(10) PRIMARY KEY,name char(20)); Query OK, 0 rows affected (0.08 sec)
或者
mysql>create table course(id int(10),name char(20),PRIMARY KEY(id));
Query OK, 0 rows affected (0.08 sec)
多字段主键格式:PRIMARY KEY(列名1,列名2,列名3......)
mysql> create table num
-> (
-> num_id int AUTO_INCREMENT NOT NULL,
-> stu_id int(10) NOT NULL,
-> name char(20) NOT NULL,
-> PRIMARY KEY(num_id,stu_id)
-> )
-> ;
Query OK, 0 rows affected (0.03 sec)
为已存在的表格删除主键:alter table 表名 drop PRIMARY KEY
mysql> alter table score drop PRIMARY KEY; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0
为已存在的表设置或重新设置主键:alter table 表名 add PRIMARY KEY(属性1,属性2......);
mysql> alter table score add PRIMARY KEY (stu_id,cour_id); Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0
4)设置table的外键
设置外键命令格式:CONSTRAINT 外键别名 FOREIGN KEY(属性1,属性2,....属性n) REFERENCES 表名(属性1‘,属性2‘,...属性n‘)
此处只是一个简单的外键设置命令,下一篇将会详细介绍外键约束设置
mysql> CREATE TABLE teacher ( -> id int PRIMARY KEY, -> stu_id int, -> name varchar(20), -> CONSTRAINT STUID FOREIGN KEY(stu_id) REFERENCES student1(id) -> ); Query OK, 0 rows affected (0.00 sec)
删除外键命令格式:ALTER TABLE 表名 DROP FOREINGN KEY 外键名;
mysql> alter table teacher drop foreign key STUID; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0
5)设置表的非空约束
简单的说就是不让这个属性的值为空,不填的话就会报错
格式:属性名 数据类型 NOT NULL
6)设置表的唯一性约束
就是这个属性的值是不能重复的
格式:属性名 数据类型 UNIQUE
7)设置表的属性值自动增加
AUTO_INCREMENT约束的字段可以是任何整数类型(TINYINT、SMALLINT、INT和BIGINT),在默认的情况下,该字段的值是从1开始自增
格式:属性名 数据类型 AUTO_INCREMENT
8)设置表的属性的默认值
格式:属性名 数据类型 DEFAULT 默认值
mysql> CREATE TABLE student3 ( -> id int PRIMARY KEY AUTO_INCREMENT, -> teacher_id int UNIQUE, -> name varchar(20) NOT NULL, -> sex varchar(10) DEFAULT ‘male‘ -> ); Query OK, 0 rows affected (0.01 sec)
7.查看指定数据库的所有表单
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| course |
| students |
+----------------+
2 rows in set (0.00 sec)
8. 删除指定数据库中的指定表单
执行该命令之前,必须使用USE命令选定数据库,否则会出现会出现“No database selected”的错误。
命令格式:DROP TABLE 表单名称;
mysql> drop table course;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| students |
+----------------+
1 row in set (0.00 sec)
9.查看表结构
1)查看表的基本结构
命令格式:DESCRIBE 表名;
mysql> DESCRIBE num; +--------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+----------------+ | num_id | int(11) | NO | PRI | NULL | auto_increment | | stu_id | int(10) | NO | PRI | NULL | | | name | char(20) | NO | | NULL | | +--------+----------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
2)查看表的详细结构
命令格式:SHOW CREATE TABLE 表名
mysql> show create table num; +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | num | CREATE TABLE `num` ( `num_id` int(11) NOT NULL AUTO_INCREMENT, `stu_id` int(10) NOT NULL, `name` char(20) NOT NULL, PRIMARY KEY (`num_id`,`stu_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
10. 修改表的基本属性
1)修改表名:表名可以在数据库中唯一标识一个table
命令格式:ALTER TABLE 旧名 RENAME 新名;
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | course | | num | | teacher | +----------------+ 3 rows in set (0.00 sec) mysql> alter table course rename courses; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | courses | | num | | teacher | +----------------+ 3 rows in set (0.00 sec)
2)修改属性的数据类型
命令格式:ALTER TABLE 表名 MODIFY 属性名 数据类型
mysql> describe num; +--------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+----------------+ | num_id | int(11) | NO | PRI | NULL | auto_increment | | stu_id | int(10) | NO | PRI | NULL | | | name | int(20) | YES | | NULL | | +--------+---------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> alter table num modify name char(20); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> describe num; +--------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+----------------+ | num_id | int(11) | NO | PRI | NULL | auto_increment | | stu_id | int(10) | NO | PRI | NULL | | | name | char(20) | YES | | NULL | | +--------+----------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
3)修改字段名
命令格式:ALTER TABLE 表名 CHANGE 属性名 新属性名 新数据类型
mysql> describe num;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| num_id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(10) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table num change name sex char(10);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe num;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| num_id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(10) | NO | PRI | NULL | |
| sex | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
4)增加字段
命令格式:alter table 表名 ADD 属性名1 [完整性约束条件] [FIRST | AFTER 属性名2]
其中,FIRST参数表示将新加的属性设置为该表的第一个字段;AFTER 属性名2表示将新加的字段置于属性名2(已存在)之后。
mysql> describe num;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| num_id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(10) | NO | PRI | NULL | |
| sex | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table num add name char(20) NOT NULL FIRST;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe num;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| name | char(20) | NO | | NULL | |
| num_id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(10) | NO | PRI | NULL | |
| sex | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
6)删除字段
命令格式:ALTER TABLE 表名 DROP 属性名;
mysql> describe num;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| name | char(20) | NO | | NULL | |
| num_id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(10) | NO | PRI | NULL | |
| sex | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table num drop name;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe num;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| num_id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(10) | NO | PRI | NULL | |
| sex | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)