mysql -h localhost -u 用戶名 -p密碼 //链接数据库
use desk_show; //使用数据库
show tables; //显示数据表
describe desk6_0; //显示表结构desk6_0为数据表
mysql其他命令:
show databases; 显示数据库
create database name; 创建数据库
use databasename; 选择数据库
drop database name 直接删除数据库,不提醒
show tables; 显示表
describe tablename; 显示具体的表结构
SELECT * FROM 表名; 显示表中记录
select 中加上distinct去除重复字段
mysqladmin drop databasename
删除数据库前,有提示。
显示当前mysql版本和当前日期
select version(),current_date;
建表
create table 表名 (字段设定列表);
mysql> create table name(
-> id int auto_increment not null primary key ,
-> uname char(8),
-> gender char(2),
-> birthday date );
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+------------------+
| Tables_in_userdb |
+------------------+
| name |
+------------------+
1 row in set (0.00 sec)
mysql> describe name;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| uname | char(8) | YES | | NULL | |
| gender | char(2) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
注: auto_increment 自增
primary key 主键
insert into name(uname,gender,birthday) values('张三','男','1971-10-01'); 增加记录
update name set birthday='1971-01-10' where uname='张三'; 修改记录
delete from name where uname='张三'; 删除记录
drop table 表名; 删除表
drop database 库名; 删除库
mysqldump -u root -p --opt 数据库名>备份名; //进入到库目录 备份数据库
mysql -u root -p 数据库名<备份名; //恢复时数据库必须存在,可以为空数据库 恢复
数据库授权
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
例1、增加一个用户user001密码为123456,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
mysql> grant select,insert,update,delete on *.* to user001@"%" Identified by "123456";
例2、增加一个用户user002密码为123456,让此用户只可以在localhost上登录,也可以设置指定IP,并可以对数据库test进行查询、插入、修改、删除的操作 (localhost指本地主机,即MySQL数据库所在的那台主机)
//这样用户即使用知道user_2的密码,他也无法从网上直接访问数据库,只能通过MYSQL主机来操作test库。
//首先用以root用户连入MySQL,然后键入以下命令:
mysql>grant select,insert,update,delete on test.* to user002@localhost identified by "123456";
本文来自于网络