一、数据库操作
1. MySQL创建数据库
- 基本语法:CREATE DATABASE 数据库名称;
- 操作命令: CREATE DATABASE PHP;
MariaDB [(none)]> CREATE DATABASE php;
Query OK, 1 row affected (0.00 sec)
2. 查看数据库信息:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| php |
| test |
+--------------------+
5 rows in set (0.00 sec)
3. 选中指定数据库信息:
- 语句格式: use 数据库名称;
- 操作命令: use php;
MariaDB [(none)]> use php;
Database changed
4. 查看库中具有表信息:
MariaDB [php]> show tables;
Empty set (0.00 sec)
5. 删除指定数据库信息:
- 语句格式: drop database 数据库名称;
- 操作命令: drop database php;
MariaDB [php]> drop database php;
Query OK, 0 rows affected (0.00 sec)
二、数据库表操作
1. 创建表信息:
- 语句格式: CREATE TABLE 表名(字段名1 字段类型,字段名2 …字段名n 字段类型n);
- 操作命令:
create table xueyuan(
name varchar(15),
sex char(5),
age int(5),
xueli char(10),
jingyan bool,
xinzi float(10,2)
);
MariaDB [php]> create table xueyuan(
-> name varchar(15),
-> sex char(5),
-> age int(5),
-> xueli char(10),
-> jingyan bool,
-> xinzi float(10,2)
-> );
Query OK, 0 rows affected (0.01 sec)
2. 查看表中字段信息:
- 语句格式: desc 表名称
- 操作命令: desc xueyuan;
MariaDB [php]> desc xueyuan;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(15) | YES | | NULL | |
| sex | char(5) | YES | | NULL | |
| age | int(5) | YES | | NULL | |
| xueli | char(10) | YES | | NULL | |
| jingyan | tinyint(1) | YES | | NULL | |
| xinzi | float(10,2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
3. 查看表信息创建方法:
- 语句格式: SHOW CREATE TABLE 表名称;
- 操作命令: SHOW CREATE TABLE xueyuan\G;
MariaDB [php]> SHOW CREATE TABLE xueyuan\G;
*************************** 1. row ***************************
Table: xueyuan
Create Table: CREATE TABLE `xueyuan` (
`name` varchar(15) DEFAULT NULL,
`sex` char(5) DEFAULT NULL,
`age` int(5) DEFAULT NULL,
`xueli` char(10) DEFAULT NULL,
`jingyan` tinyint(1) DEFAULT NULL,
`xinzi` float(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR: No query specified
4. 删除表信息:
- 语句格式: drop table 名名称;
- 操作命令: drop table oldboy;
MariaDB [php]> drop table qwqw;
Query OK, 0 rows affected (0.00 sec)
5. 创建表设置引擎信息和默认字符编码信息:
mysql> create table test(
name varchar(15),
sex char(5),
age int(5),
xueli char(10),
jingyan bool,
xinzi float(10,2)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
三、数据库表字段操作
1. 修改字段数据类型信息
- 语句格式: alter table 表名 modify 字段名 修改后的数据类型;
- 操作命令: alter table test modify sex varchar(10);
MariaDB [php]> alter table qwqw modify sex varchar(10);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
2. 增加表中字段信息:
- 语句格式: alter table 表名 add column 字段名 字段类型;
- 操作命令: alter table test add column oldboy date;
MariaDB [php]> alter table qwqw add column oldboy date;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
3. 增加表中字段信息: 可以指定控制字段添加的位置
- 语句格式: alter table 表名 add 字段名 bool after 在哪个字段后添加;
- 操作命令: alter table test add oldgirl bool after age;
MariaDB [php]> alter table qwqw add pin bool after age;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
4. 增加表中字段信息: 将指定字段插入到第一列
- 语句格式: alter table 表名 add 字段 类型 first;
- 操作命令: alter table test add oldbaby char first;
MariaDB [php]> alter table qwqw add todu int first;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
5. 删除表中字段信息:
- 语句格式: alter table 表名 drop column 字段名称;
- 操作命令: alter table test drop column oldbaby;
MariaDB [php]> alter table qwqw drop column todu;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
6. 修改字段名称信息:
- 语句格式: alter table 表名 change 原字段名称 修改后字段名 字段类型;
- 操作命令: alter table test change xueli edu varchar(15);
MariaDB [php]> alter table qwqw change xueli edu varchar(15);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
7. 修改已有字段顺序:
- 语句格式:
alter table 表名 modify 字段名 类型 first; — 直接将指定字段移到第一列
MariaDB [php]> alter table qwqw modify jingyan tinyint(1) first;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
- 语句格式:alter table test modify 字段名 类型 after 字段名; — 将字段移动到指定列之后
MariaDB [php]> alter table qwqw modify pin tinyint(1) after name;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
8. 修改数据库中表的名称:
- 语句格式: alter table 旧表名 rename 新的表名;
- 操作命令: alter table test rename new_test;
MariaDB [php]> alter table qwqw rename qwqwq;
Query OK, 0 rows affected (0.01 sec)