查看数据库结构
创建及删除库和表
管理表的记录
查看数据库列表信息的命令
show databases;
use 数据库名;
show tables;
describe [数据库名.]表名;
或 use 库;desccribe 表名;
Structuered Query Language 的缩写,即结构化查询语言
关系型数据库的标准语言
用于维护管理数据库
包括数据查询,数据更新,访问控制,对象管理等功能
mysql> create database school;
mysql> use school;
mysql> create table ky03 (id int(4) not null primary key auto_increment,name varchar(10) not null,age int(5) not null );
注意: int (5) 整型 00000-99999
double 浮点型 8字节
decimal(5,2) 有效数字是5位,小数点后面保留两位 100.00 099.50
float 单精度浮点 4字节
char (10) 固定长度字符串
varchar (50) 可变长度字符串
字段01的约束:
非空约束: 内容不允许为空
主键约束: 非空且唯一 标识
默认值: 假如没有填数据,默认预先设定的值填写
自增特性: id 1234
存储引擎:
myisam innodb
字符集:
UTF8
删除指定的数据表:drop table [数据库名.]表名
删除指定的数据库:drop database 数据库名
mysql> drop table school.ky03; '//删除表' //绝对路径
Query OK, 0 rows affected (0.00 sec)
mysql> drop database school; '//删除库' //相对路径
Query OK, 0 rows affected (0.00 sec)
alter table:修改表的属性
alter table t2 add id int first; 增加一列成为第一列
alter table t2 add id2 int after id; 在id后面增加一列叫id2
alter table t2 drop id2; 删除id2这个列
alter table t2 change id ID bigint; 修改列名和数据类型
alter table t2 modify ID int; 修改列的数据类型
show engines; 查看数据库有哪些存储引擎
alter table t20 engine MyISAM; 修改表的存储引擎
show create table t20; 查看修改存储引擎是否成功
alter table t20 default charset=utf8; 修改表的语言编码
mysql> create table kgc (id int(4) not null primary key auto_increment,name varchar(10) not null,age int(4) not null );
mysql> select * from kgc;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | zhangsan | 18 |
| 2 | lisi | 28 |
+----+----------+-----+
2 rows in set (0.00 sec)
mysql> update kgc set name='wangwu' where id=2;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from kgc;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | zhangsan | 18 |
| 2 | wangwu | 28 |
+----+----------+-----+
2 rows in set (0.01 sec)
mysql> delete from kgc where id=2;
Query OK, 1 row affected (0.00 sec)
mysql> select * from kgc;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | zhangsan | 18 |
+----+----------+-----+
1 row in set (0.00 sec)
mysql> delete from kgc;
Query OK, 1 row affected (0.00 sec)
mysql> select * from kgc;
Empty set (0.00 sec)
drop 库.表; //删除表,删除记录同时删除表结构
delete from tablename; //删除表的记录
truncate table tmp; //删除所有记录,保留表的结构
mysql> select id,name from kgc;
+----+--------+
| id | name |
+----+--------+
| 1 | wangwu |
| 2 | lisi |
+----+--------+
2 rows in set (0.00 sec)
mysql> select id,name from kgc where id=1;
+----+--------+
| id | name |
+----+--------+
| 1 | wangwu |
+----+--------+
1 row in set (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
'//all privileges:所有权限,%:所有终端'
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for 'root'@'%';
+-------------------------------------------------------------+
| Grants for root@% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> revoke all privileges on *.* from 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'root'@'%';
+----------------------------------------------------+
| Grants for root@% |
+----------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'%' WITH GRANT OPTION |
+----------------------------------------------------+
1 row in set (0.00 sec)
mysql> create temporary table temp_kgc (id int(4) not null auto_increment,name varchar(10) not null,hobby varchar(10) not null, primary key(id))engine=innodb default charset=utf8;
mysql> insert into temp_kgc (name,hobby) values ('tom','cat');
Query OK, 1 row affected (0.00 sec)
mysql> show tables; //看不到临时表,其保存在硬盘内
+------------------+
| Tables_in_school |
+------------------+
| kgc |
| ky03 |
+------------------+
mysql> select * from temp_kgc; //能够查看临时表的内容
+----+------+-------+
| id | name | hobby |
+----+------+-------+
| 1 | tom | cat |
+----+------+-------+
mysql> exit
Bye
[root@mysql ~]# mysql -uroot -pabc123
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from temp_kgc; //重新连接数据库,临时表丢失
ERROR 1146 (42S02): Table 'school.temp_kgc' doesn't exist
mysql>
like方法
从kgc完整复制表结构生成test表,再导入数据
mysql> create table test like kgc; //复制表结构
mysql> insert into test select * from kgc; //插入数据
mysql> select * from test;
+----+--------+-----+
| id | name | age |
+----+--------+-----+
| 1 | wangwu | 18 |
| 2 | lisi | 28 |
+----+--------+-----+
show create table 方法
先查看kgc表完整结构,根据次结构创建名字不同结构相同的表test ,再插入数据
mysql> show create table kgc\G '\G代表竖行显示'
*************************** 1. row ***************************
Table: kgc
Create Table: CREATE TABLE "kgc" (
"id" int(4) NOT NULL AUTO_INCREMENT,
"name" varchar(10) NOT NULL,
"age" int(5) NOT NULL,
PRIMARY KEY ("id")
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> create table test02 (id int(4) not null primary key auto_increment,name varchar(10) not null,age int(5) not null );
mysql> insert into test02 (id,name,age) values (2,'lisi','28'),('1','wangwu','18');
mysql> select * from test02;
+----+--------+-----+
| id | name | age |
+----+--------+-----+
| 1 | wangwu | 18 |
| 2 | lisi | 28 |
+----+--------+-----+
create user user@host identified by 'password'; @'%' @ip @'network'
//说明:用户的信息保存在mysql数据库中的user表中,验证用户是否创建成功如下:
select user,host,password from mysql.user;
mysql> drop user user01@'localhost'; 删除用户
mysql> select user from mysql.user where user='user01'; 验证用户是否删除成功
mysql> drop user user; 默认删除该用户从任意主机登陆
mysql> rename user u01@'instructor.example.com' to u001@'localhost'; 重命名用户名
mysql> show grants; 查看用户权限
mysql> show grants for user02@'%'; 查看指定用户的权限
mysql> drop user ''@'rhel6.example.com'; 删除一个匿名用户
mysql> delete from mysql.user where user=''; 删除mysql中的匿名用户
mysql> delete from mysql.user where user='root' and host='::1';
mysql> flush privileges;
复制环境路径
此电脑–C盘–Program Files–MySQL–MySQL Server 5.7–bin–复制路径