create database 数据库名称;
创建数据库:mysql> create database TEMP;
Query OK, 1 row affected (0.00 sec)
mysql> create database if not exists TEMP;
Query OK, 1 row affected, 1 warning (0.00 sec)
charset
可以指定数据库的编码集(字符集):mysql> create database if not exists TEMP charset utf8;
Query OK, 1 row affected, 1 warning (0.00 sec)
show create database 数据库名称;
查看数据库生成的命令:mysql> show create database TEMP;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| TEMP | CREATE DATABASE `TEMP` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
使用drop database 数据库名称;
删除指定的数据库:
mysql> drop database TEMP;
drop database TEMP;
在对数据表进行操作前,需要先指定使用的数据库:
mysql> use TEMP;
Database changed
使用select databse();
查看当前选择的数据库(不要漏了小括号):
mysql> select database();
+------------+
| database() |
+------------+
| TEMP |
+------------+
1 row in set (0.00 sec)
使用show databses;
展示当前所有的数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| TEMP |
| performance_schema |
| sys |
| utf8_data_demo |
+--------------------+
12 rows in set (0.00 sec)
使用show tables;
查看当前数据库的所有数据表:
mysql> show tables;
Empty set (0.00 sec)
Empty表示当前数据库没有数据表
使用create table 数据表名称 (字段 类型 约束,字段 类型 约束,……)
创建数据表:
mysql> create table student (id int primary key,name varchar(20) not null);
Query OK, 0 rows affected (0.03 sec)
使用show create table 数据表名称;
查看数据表的生成命令:
mysql> show create table student;
+---------+-------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------+
1 row in set (0.00 sec)
使用desc 数据表名称
查看数据表的结构:
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
alter table 数据表名称 add 字段 类型 约束;
为数据表添加字段:mysql> alter table student add age tinyint default 18;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(4) | YES | | 18 | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
alter table 数据表名称 change 数据表名称 字段 类型 约束;
对数据表的字段进行修改并重命名:mysql> alter table student change name username varchar(30) default 'abc';
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| username | varchar(30) | YES | | abc | |
| age | tinyint(4) | YES | | 18 | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
alter table 数据表名称 modify 字段 类型 约束;
对数据表字段的结构进行修改mysql> alter table student modify username varchar(20) default '建国';
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| username | varchar(20) | YES | | 建国 | |
| age | tinyint(4) | YES | | 18 | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
alter table 数据表名称 drop 字段
对数据表的字段进行删除:mysql> alter table student drop username;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| age | tinyint(4) | YES | | 18 | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
insert into 数据表名称 values (字段对应数据,字段对应数据,……)
向数据表插入数据:mysql> insert into student values(1, 20);
Query OK, 1 row affected (0.01 sec)
主键可以为0,或者NULL
多行插入使用insert into 数据表名称 values (数据,数据,……),(数据,数据,……)
使用insert into 数据表名称 (字段名称,……) values (字段对应数据,……)
向数据表插入数据,区别在于不需要填写所有字段的数据,前提是未填写的字段有默认值或者自增(auto_increment):
mysql> insert into student (age) values (24);
ERROR 1364 (HY000): Field 'id' doesn't have a default value
ysql> alter table student modify id int auto_increment;
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into student (age) values (24);
Query OK, 1 row affected (0.01 sec)
使用update 数据表名称 set 字段1=值1,字段2=值2 where 条件;
对数据进行修改:
mysql> select * from student;
+----+------+
| id | age |
+----+------+
| 1 | 20 |
| 2 | 24 |
+----+------+
2 rows in set (0.00 sec)
mysql> update student set age=30 where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+------+
| id | age |
+----+------+
| 1 | 20 |
| 2 | 30 |
+----+------+
2 rows in set (0.00 sec)
使用delete from 数据表名称 where 条件;
删除指定的数据:
mysql> select * from student;
+----+------+
| id | age |
+----+------+
| 1 | 20 |
| 2 | 30 |
+----+------+
2 rows in set (0.00 sec)
mysql> delete from student where id =2;
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+------+
| id | age |
+----+------+
| 1 | 20 |
+----+------+
1 row in set (0.00 sec)
使用select * from 数据表 where 条件
查询数据,显示所有字段:
mysql> select * from student;
+----+------+
| id | age |
+----+------+
| 1 | 20 |
| 3 | 23 |
| 4 | 21 |
| 5 | 28 |
+----+------+
4 rows in set (0.00 sec)
使用select 字段名称,字段名称…… from 数据表 where 条件
查询数据,显示指定的字段:
mysql> select age from student;
+------+
| age |
+------+
| 20 |
| 23 |
| 21 |
| 28 |
+------+
4 rows in set (0.00 sec)
在字段后添加as
可以为字段起别名:
mysql> select id as 序号,age as 年龄 from student;
+--------+--------+
| 序号 | 年龄 |
+--------+--------+
| 1 | 20 |
| 3 | 23 |
| 4 | 21 |
| 5 | 28 |
+--------+--------+
4 rows in set (0.00 sec)