mysql 命令笔记

 

修改mysql管理员密码
mysqladmin -u root password 123456
创建数据库:
create database 数据库名称;
删除数据库:
drop databases 数据库名称;
查看数据库:
show databases;
进入数据库:
use 数据库名称;
创建表:
create table 表名称(字段1;字段2;.......,[表级约束])[Type=表类型];
create table student(sno varchar(7) not null,sname varchar(20) not null,ssex char(1) default 't',sbirthday date,sdeoa char

(20),primary key (sno));
复制表:
create table 新表名 like 源表名;
删除表:
drop table 表名称1[,表名称2,........];

修改表:
更改表名称:
alter table 表名 rename to  新表名;
alter table student rename to wangxiaofei;
增加字段:
alter table 表名 add 字段名 字段类型;
alter table student add saddress varchar(25);
更改字段:
alter table 表名 change 字段名 新字段名 字段类型;
alter table student change saddress saddres text;
删除字段:
alter table 表名 drop 字段名;
alter table student drop saddress;
查看表:
show tables;

插入记录:
insert into 表名 (字段1,..........) values (值1,..........);
insert into 表名 values (值1,...........);
insert into 表名 values (值1,...........),(值1,...........),......;
insert into student (sno,sname,ssex,sbirthday,sdeoa) values ('0321002','王小飞',default,19801011,'math');

查看表:
select * from 表名;
select * from student;
条件查询:
select * from 表名 where 表达式;

删除表字段值(记录):
delete  from 表名;
truncate table 表名;(删除重建,比delete快)
delete from 表名 where 表达式;
delete from student where left(sno,4)='0322';删除表中sno字段前4位为“0322”的字段。

修改记录:
updata 表名 set 字段1=值[,字段2=值,.......] where 表达式;
updata student set sbirthday=19801012,sdeoa='math' where sno='0321002'; 把sno值为0321002的记录中的sbirthday的值改为:19801012,

sdeoa值改为math

创建索引:
使用primary key子句指定一个字段为主键时,就自动创建了索引。
create table course (cno varchar(5) not null,cname varchar(30) not null,teacher varchar(20),primary key (cno),index cna(cname));
或create table course (cno varchar(5) not null,cname varchar(30) not null,teacher varchar(20),primary key (cno),unique(cname));
用unique创建要求索引字段中的值必须是唯一的。
create index(或unique) 索引名 on 表名 (字段1[(长度)],.....);
create index sna on student (sname);
create index sna on student (sname(10));

删除索引:
drop index 索引名 on 表名;
drop index sna on student;

创建用户:
insert into mysql.user (host,user,password) values ('%','guest',password('guest'));

删除用户:
delete from mysql.user where user='guest';
修改用户密码:
update mysql.user set password=password('123456') where user='guest';
set password for guest @'%'=password('123456');
重载mysql重权表:
flush privileges;
打开3306端口:
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

授权:
grant 权限列表[(字段列表)] on  数据库名称.表名称 to 用户名@域名或IP地址[identified by '密码'][with grant option];
grant all on xscz.* to guets@'%' identified by 'guest'; 授权guest用户可以从任一主机连接到数据库,并有完全访问xscz这个数据库。
grant select on xscz.* to tom@'192.168.1.%' identified by '123456';grant update(teache) on xscz.coures to tom@'192.160.1.%' :授权

书tom用户可以从192.168.1.0网段读取xscz这个数据库,并能修改表coures。
查看mysql.user表中与用户guest有关的记录:
select host,user,password,select_prive from mysql.user where user='guest';
grant all on xsxk.* to admin@localhost identified by '123456' with grant option;授权admin用户可以从本地连接到数据库,并有完全访问

xsxk这个数据库,并将其所拥有的权限授予其它用户。

撤权:
revoke  权限列表[(字段列表)] on  数据库名称.表名称 from 用户名@域名或IP地址;
revoke create,drop on xsxk.* from admin@localhost;

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值