您的位置:首页 > 数据库 > MySQL

MySQL之二:基本操作(综合详解)

2015-04-22 17:47 330 查看
修改管理员命令
mysql -u root -h 192.168.44.10 -p      ###-u指定用户名  -h 指定主机   -p  指定密码,一般回车输入密码
mysqladmin -u root -p  password  new_password                       回车后输入就密码,如果root已经设置密码,需要使用改命令修改
mysqladmin -u root -pold_password password  new_password            -p后面直接跟密码
update user set password=password('new_password') where user='root' and host='localhost';
flush privileges        update更新密码,需要刷新
set password=password('new_password');   为root用户修改密码
set password for 'user_name'@'host_name'=password('user_name');   为用户‘user_name'@'host_name'修改密码
查看数据库版本等内容
mysql> select version();    查看版本信息
mysql> select now();     查看时间
mysql> select dayofmonth(current_date);        显示年月日
mysql> select (9+9)-10;        数学运算
数据库的切换,查看,建立,删除
mysql> show databases;    查看当前所有数据库
mysql> create database new_database;    创建数据库
mysql> create database if not exists database_name;        if not exist  如果不存在,则建立
mysql> drop database new_database;    删除数据库
mysql> drop database if exists database_name ;     if exist  判断数据库是否存在
mysql> use mysql;    切换到mysql数据库中
mysql> rename  old_database to new_database;发现这条命令在MySQL 5.1.7的时候被添加进来,5.1.23的时候又被去掉了
表的查看,建立,删除,新增内容,修改,删除;新增字段,修改,删除,
mysql> show tables;    查看当前数据库中所有表
mysql> desc new_table;      查看表中的字段类型
mysql> create table new_table (id char(4),named char(10),num int(4));    创建表,必须同时指定数据类型
mysql> create table new1_table like new_table;     新建表,只是字段,没有内容
mysql> create table new2_table select user,name from new1_table;   选择模式创建的表,某些默认属性等都不存在
mysql> drop table new_table;    删除表
mysql> rename table new_table to new;        表的重命名
mysql> drop database if exists drop_database;//if exists 判断数据库是否存在,不存在也不产生错误
mysql> insert into new_table values ('1','ddh','100');    新增内容    (格式:insert into   table_name values (......))
mysql> insert into new_table (named,num) values ('username','101');     插入部分字段内容
mysql> update new_table set named='ddh' where num='100';    修改更新内容    (格式:update table_name set   ' '=' '  where    ......)
mysql> delete from new_table where named='king';         删除某行内容    (格式:delete from table_name where ...........)
mysql> alter table new_table add xingbie int(4) default'0';        增加一个字段   (格式:alter table_name  add|change|drop ....)
mysql> alter table new_table change id idd char(4) default '0';    某字段的修改
mysql> alter table new_table drop num;            删除num字段
mysql> alter table new_table rename to tb_name;    修改表名为tb_name
rename table  testcourses to test;
用户新增,删除,授权与回收(grant 授权)
mysql> create user 'king'@'%' identified by 'king';     用户新增        (格式:create user ' '@' '  identified by ' ')
mysql> drop user 'king'@'%';         用户删除(_匹配任意单个字符172.17.0._,%匹配任意字符)    (格式:drop user ' '@' ')
mysql> show grants;    查看自己的权限
mysql> show grants  for  'king'@'localhost'; 查看用户'king'@'localhost'的权限
mysql> grant create,insert,update,delete on mysql.* to 'king'@'localhost';    数据库授权  (格式:grant .......on .......  to ........identified by ........)
mysql> grant create,insert,update,delete on mysql.* to 'king'@'localhost' identified by 'hello';    数据库授权并修改密码
mysql> revoke INSERT, UPDATE, DELETE, CREATE ON `mysql`.* from 'king'@'localhost';      权限回收   (格式:  ......on .......from ............)
本文出自 “anka” 博客,请务必保留此出处http://anka0501.blog.51cto.com/10129669/1637215
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: