一、数据库操作
1.1、创建数据库(增)
CREATE DATABASE 也可以使用小写,(注意不要漏掉分号 ;)
mysql> create database test;
或
mysql> create database test charset utf8;
1.2、删除数据库(删)
mysql> drop database test;
1.3、修改数据库(改)
mysql> alter database test charset gbk;
1.4、查看数据库(查)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)
查看mysql模式(一定要是严格模式)
mysql> select @@sql_mode; +--------------------------------------------+ | @@sql_mode | +--------------------------------------------+ | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +--------------------------------------------+ 1 row in set (0.00 sec)
二、数据库表操作
2.1、指定数据库,或者称为切换数据库,后面的操作都会针对这个数据库进行
mysql> use test;
Database changed
查看当前所在文件夹
mysql> select database();
2.2、创建表user实例(增)
mysql> create table employee (id int(10),name char(20),phone int(12)); mysql> create table user -> ( -> id int(10), -> name varchar(20), -> type varchar(20) -> );
2.3、删除数据库表(删)
mysql> drop table user;
2.4、修改数据库表(改)
# modify修改的意思 mysql> alter table user modify name char(6); # 改变name为大写的NAME mysql> alter table user change name NAMA char(7);
2.5、查看数据库表(查)
#查看当前的这张user表 mysql> show create table user; # 查看所有的表 mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | user | +----------------+ # 查看表结构的详细信息 mysql> desc user; 或 mysql> describe user; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | varchar(20) | NO | | NULL | | | type | varchar(20) | NO | | NULL | | +----------+-------------+------+-----+---------+----------------+
2.6、复制表
复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_user select * from user;
只复制表结构,不拷贝记录
mysql> create table new_user select * from user where 1=2; 或 mysql> create table new_employee like employee;
三、数据库表内容操作
3.1、增
# 插入一条数据,规定id,name数据类型
mysql> insert user(id, username, password, type) value(1, 'super', 'super123', 'super), (2, 'admin', 'admin123', 'admin'); 或 mysql> insert into user value (1, 'super', 'super123', 'super'); mysql> insert into user value (2, 'admin', 'admin123', 'admin');
3.2、删
mysql> delete from user; mysql> delete from user where id=2;
3.3、改
mysql> update db1.user set name='Tom'; mysql> update db1.user set name='Mike' where id=2;
3.4、查
mysql> select id from db1.user; mysql> select id,name from db1.user; mysql> select * from db1.user; 或 mysql> select * from user; +----+----------+----------+-------+ | id | username | password | type | +----+----------+----------+-------+ | 1 | super | super | super | | 2 | admin | admin | admin | +----+----------+----------+-------+
四、退出
使用命令 quit 或者 exit 退出 MySQL。