好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

数据库管理与高可用----第二章数据库管理

数据库管理与高可用----第二章数据库管理

一:数据库基本操作命令

◆ DDL语句创建库,表的命令

创建数据库:create database 数据库名

创建数据表:create table 表名(字段定义…)

 create database school;
create table ky03 (id int(4) not null primary key auto_increment,name varchar(10) not null,age int(5) not null ); 

◆ DDL语句删除表库命令

删除指定的数据表:drop table [数据库名.]表名

删除指定的数据库:drop database 数据库名

 mysql> drop table school.ky03;    ‘//删除表‘   //绝对路径
mysql> drop database school;  ‘//删除库‘   //相对路径 

◆ DDL语句修改表的属性

 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; 修改表的语言编码
alter table am rename to  hasee;  修改am表名改为hasee
alter table info add column hobby int(3) not null; 

◆ DML操作命令

向数据表中插入新的数据记录

 insert into 表名(字段1,字段2,…)values(字段1的值,字段2的值,…) 

修改,更新数据表中的数据记录

 update 表名 set 字段名 1=值1[,字段名2=值2] where条件表达式 

在数据表中删除指定的数据记录语句

 delete from 表名 where条件表达式   (不带where条件的语句表示删除表中所有记录(谨慎操作)) 

清空表

 drop 库.表;                 //删除表,删除记录同时删除表结构
delete from tablename;      //删除表的记录
truncate table tmp;           //删除所有记录,保留表的结构 

◆ DQL操作命令

DQL是数据查询语句,只有一条:SELECT 用于从数据表中查找符合条件的数据记录 不指定条件查询语句 SELECT字段名1,字段名2…FROM表名 指定条件查询的语句 SELECT字段名1,字段名2…FROM表名 WHERE条件表达式

◆ DCL操作命令

设置用户权限的命令;若用户已存在,则更改用户密码,若用户不存在,则新建用户 GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [IDENTIFIED BY ‘密码′ ]

 mysql> grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘abc123‘ with grant option; 

查看用户权限的命令 SHOW GRANTS FOR 用户名@来源地址

 mysql> show grants for ‘root‘@‘%‘; 

撤销用户权限的命令 REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址

 mysql> revoke all privileges on *.* from ‘root‘@‘%‘; 
临时表建立
 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‘); 

克隆表

 1.直接as复制
mysql> create table kelong as select * from info;   ‘克隆表结构和数据‘ as不能省略
2.like方法   
从kgc完整复制表结构生成test表,再导入数据 
mysql> create table test like kgc;  //复制表结构
mysql> insert into test select * from kgc;  //插入数据
3.show create table 方法  
先查看kgc表完整结构,根据次结构创建名字不同结构相同的表test ,再插入数据
mysql> show create table kgc\G         ‘\G代表竖行显示‘
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> insert into info (id,name,score) values (1,‘zhangsan‘,99),(2,‘lisi‘,80);
- 从一个库的表克隆到另外一个库的表
mysql> use temporary;
mysql> create table am like school.kgc;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------------+
| Tables_in_temporary |
+---------------------+
| accp                |
| am                  |
| kgc                 |
mysql> insert into am select * from school.kgc;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select * from am;
+----+----------+-----+-------+-------+
| id | name     | age | hobby | score |
+----+----------+-----+-------+-------+
|  1 | wangwu   |  18 |     0 | 88.00 |
|  2 | lisi     |  28 |     0 | 90.00 |
|  4 | wangwu   |  30 |     0 | 88.00 |
|  3 | zhangsan |  20 |     0 | 80.00 |
+----+----------+-----+-------+-------+
4 rows in set (0.00 sec) 

◆ 创建用户

 create user username@host identified by ‘password‘;
可选项 @‘%‘ @ip @‘network‘  

删除用户

 DROP USER ‘username‘@‘host‘;
mysql> delete from mysql.user where user=‘‘; 删除mysql中的匿名用户  

设置与更改用户密码

 - SET PASSWORD FOR ‘username‘@‘host‘ = PASSWORD(‘newpassword‘);
如果是当前登陆用户用:
SET PASSWORD = PASSWORD("newpassword");
例子:
SET PASSWORD FOR ‘pig‘@‘%‘ = PASSWORD("123456");

mysql> show grants for ‘root‘@‘%‘;
+----------------------------------------------------+
| Grants for root@%                                  |
+----------------------------------------------------+
| GRANT USAGE ON *.* TO ‘root‘@‘%‘ WITH GRANT OPTION |
+----------------------------------------------------+
USAGE 默认授权的登权限 

数据库管理与高可用----第二章数据库管理

标签:key   删除   for   alt   incr   插入数据   col   into   表名   

查看更多关于数据库管理与高可用----第二章数据库管理的详细内容...

  阅读:25次