linux下mysql常用基本操作

来源:互联网 发布:config.json是什么 编辑:程序博客网 时间:2024/06/06 00:15

此文章记录了linux下mysql的常用操作,主要方便自己忘记时查询。


1、登录数据库

mysql -u用户 -h主机 -p123456

2、显示数据库

show databases;

3、选择数据库

use 数据库名;

4、选择数据库后,显示数据库中的表
show tables;

5、显示数据库中表的结构

describe 表名;

6、显示表中全部记录
select  * from 表名;select 字段名[,字段名] from 表名;

7、新建数据库

create database 数据库名;

8、新建数据库

CREATE TABLE test (    id  INT(11)  NOT  NULL AUTO_INCREMENT,    text1 char(32) DEFAULT NULL,    num   INT(11) NOT NULL DEFAULT '0',    PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;

9、增加记录
insert into user(name,sex) values('张三',1);

10、修改记录
update user set sex=0 where name='张三';

11、删除记录
delete from user where name='张三';

12、删除表
drop table 表名;

13、删除数据库
drop database 数据库名;

14、备份数据库
mysqldump -u用户 -h主机 -p密码 数据库名 [数据库名] > 文件名  //备份结构和数据//加-d备份数据库结构//加-t备份数据库数据//加-A备份全部数据库

15、恢复数据库
//系统命令行mysql -u用户 -p密码 数据库名<备份名; //恢复时数据库必须存在,可以为空数据库//mysql命令行source  路径\文件名

16、数据库授权
grant select on 数据库.* to 用户名@登陆主机 identified by "密码";mysql> grant select,insert,update,delete on *.* to user001@"%" Identified by "123456";mysql>grant select,insert,update,delete on test.* to user002@localhost identified by "123456";

17、当where子句条件为null时,用is,而不是“=”

...... where columnName is null

18、对数据表的操作,alert命令的基本用法
alter table tableName1 to tableName2;  //修改表名alter table tableName add column columnName columnType [after column1];  //添加列alter table tableName drop column columnName;   //删除列alter table tableName change oldColumnName newColumnName ColumnType;   //修改列名和属性alter table tableName modify columnName newColumnType;     //修改列属性

19、添加、删除主键约束

alter table 表名 add constraint 主键名(PK_表名)  primary key 表名(主键字段);alter table 表名 drop primary key;


20、添加外键约束

alter table 从表 add constraint 外键名(FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);alter table 表名  drop foreign key 外键;













0 0