mysql常用的操作

数据库的常用操作:
create database db1; #创建数据库
show databases; #查看所有数据库
show create database db1;#查看创建的指定数据库
alter database db1 charset utf8;#更改编码
drop database db1; #删除数据库
表的常用操作:
use db1; #进入db1数据库
create table t1(id int primery key auto_increment,name char(10))default charset utf8;#创建表设置默认编码
show tables; #查看所有表
desc t1; #查看表结构
show create table t1;#查看指定的表
alter table t1 add age int;#添加表结构
alter table t1 modify name char(12);#修改表结构
drop table t1; #删除表
表中记录的常用操作:
insert into t1(name) values('wxp');#在表中插入记录
select * from t1; #查询表中所有的记录
select name from t1; #查询表中的一行记录
update t1 set name='fugui' where id=1;#修改表中的记录
delete from t1 where id=1; #删除表中的记录

表的复制:

create table new_t1 select * from t1;复制表结构+记录 (key不会复制: 主键、外键和索引)

 select * from service where 1=2; //条件为假,查不到任何记录 

create table new1_service select * from service where 1=2; #只复制表结构

设置id自增:create table 表名(id int primary key auto_increment,name char(10));

创建用户与权限的设置:

#创建用户
create user 'w'@'localhost' identified by '123';创建用户(这样是没权限的)

insert,delele,update,select
#级别1:对所有库,下的所有表,下的所有字段
grant select on *.* to 'w1'@'localhost' identified by '123';#对所有的都有查询权限

#级别2:对db1库,下的所有表,下的所有字段
grant select on db1.* to 'w2'@'localhost' identified by '123';#db1下的所有表有查询权限

#级别3:对表db1.t1,下的所有字段
grant select on db1.t1 to 'w3'@'localhost' identified by '123';#只有对t1表有查询权限

#级别4:对表db1.t1,下的id,name字段
grant select (id,name) on db1.t1 to 'w4'@'localhost' identified by '123'; #只有对表t1下的id和name有查询权限
grant select (id,name),update (name) on db1.t1 to 'lin5'@'localhost' identified by '123';#对t1下的id,name有查询权限,

对name有修改权限

#修改完权限后,要记得刷新权限
flush privileges;

net start mysql 命令行开始mysql服务
net stop mysql 命令行停止mysql服务

原文地址:https://www.cnblogs.com/wxp5257/p/7481576.html