mysql命令行基本操作
2018-03-08 20:21
387 查看
命令行连接
在终端连接mysql -uroot -p # 回车后输入密码
退出数据库
exit/quit/ctrl+d
修改输入提示符
prompt python>
查询命令
select version(); # 查看版本 select now(); # 显示当前时间
数据库
查看所有数据库show databases;
使用数据库
use 数据库名; 例: use python;
查看当前数据库
select database();
创建数据库
create database 数据库名 charset=utf8; 例: create database python charset=utf8;
查看创建数据库的语句
show create databases 数据库名; 例: show create database python;
删除数据库
drop database 数据库名; 例: drop database python;
数据表
查看当前数据库中所有的表show tables;
查看表结构
desc 数据表名; 例: desc students;
创建表
auto_increment表示自动增长
not null 表示不能为空
primary 表示主键
default 默认值
create table 数据表名 (字段 类型 约束, 字段 类型 约束...); CREATE TABLE table_name( column1 datatype contrai, column2 datatype, column2 datatype, ...... columnN datatype, PRIMARY KEY(one or more columns) ); 例:创建classes表 create table classes( id int unsigend auto_increment primary key not null, name varchar(10) ); 例:创建students表 create table students( id int unsigned not null auto_increment primary key, # varchar类型补不全空位 name varchar(30), # xxx unsigned类型不包含负数 age tinyint unsigned default 0, # decimal类型(总位数,小数位数) high decimal(5,2), # 枚举类型下标从1开始 gender enum("男", "女", "中性", "保密") default "保密", cls_id int unsigned );
查看表的创建语句
show create table 数据表名; 例: show create table students; show create table classes;
修改表-添加字段
alter table 表名 add 列名 类型; 例: alter table students add birthday datatime;
修改表-修改字段:重命名版
alter table 表名 change 原名 新名 类型及约束; -- 修改字段顺序 -- alter table 表名 change 原名 新名 类型及约束 after 字段; 例: alter table students change birthday birth datetime not null;
修改表-修改字段:不重命名版
alter table 表名 modify 列名 类型及约束; 例: alter table students modify birth date not null default "1991-01-01";
修改表-删除字段
alter table 表名 drop 列名; 例: alter table students drop birth;
删除表
drop table 表名; 例: drop table classes;
增删改查(curd)
Createupdate
Retrieve
Delete
查询基本使用
查询所有列,指定条件select * from 表名; select * from 表名 where 条件; 例: select * from students; select * from students where id>1;
查询指定列,可用as为列或表指定别名,指定条件
select 列1,列2,...from 表名; select 列1,列2,...from 表名 where 条件; 例: select id,name, from cstudents; select id as "编号", name as "姓名", from students; select id, name as "姓名", from students where gender=2;
增加
全列插入:值的顺序与表中字段的顺序对应insert into 表名 value(...) 例: insert in students value(0,'Gg',1,'武汉','2018-03-04');
部分列插入:值的顺序与给出的列顺序对应
insert into 表名(列1,...) value(值1,...); 例: insert into students(id,name,hometown,birthday) value(0,'Gg','武汉','2018-03-04');
全列多行插入:值的顺序与给出的列顺序对应
insert into 表名 value(...),(...)...; 例: insert into classes value(0,'python1',(0,'python2'));
insert into 表名(列1,...) value(值1,...),(值1,...)...; insert into students(name) value('杨康'),('杨过'),('小龙女');
修改
update 表名 set 列1=值1, 列2=值2... where 条件; 例: update students set gender=0,hometown='北京' where id=5;
删除
物理删除,无法找回数据,慎用delete from 表名 where 条件; 例: delete from students where id=5;
逻辑删除,本质就是修改操作,添加字段表示不能再使用
# bit只占1位 alter table students add is_delete bit default 0; update students set is_delete=1 where id=1;
备份
运行MySQLdump命令mysqldump -uroot -p 数据库名 > python.sql # 提示输入mysql的密码
恢复
连接,创建新的数据库退出连接,执行如下操作
mysql -uroot -p 新数据库名 < python.sql # 提示输入mysql的密码
相关文章推荐
- MySQL命令行基本命令操作
- mysql命令行基本操作
- MySQL命令行工具和基本操作
- Mysql命令行基本操作
- MySQL命令行实现数据库的基本操作
- MySql命令行基本操作
- MYSQL命令行基本操作
- Mysql命令行基本操作
- MySql命令行基本操作
- mysql命令行基本操作
- MySql命令行基本操作
- Oracle 11g 发行版2 新安装后关于登录的一些基本操作
- 对MySQL日志操作的一些基本命令总结
- Linq 查询基本操作
- Vector的几个基本操作
- mysql 基本操作汇集
- 【POJ 1635】树基本操作(Tree 重心 + Tree hash + ULL 的用法)
- linux shell脚本基本操作
- 基本阈值的操作
- MySql 第四天 数据库基本操作