MySQL基本使用

1、MySQL简介?

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于Oracle旗下产品

2、MySQL的特点?

  1. 在线DDl更改功能
  2. MySQL使用标准的SQL数据语言形式
  3. 提供用于管理、检查、优化数据库操作的管理工具
  4. 支持多语言,常见的编码如GB2312、BIG5、UTF8
  5. 优化的SQL查询算法,有效地提高查询速度
  6. 大型数据库
  7. 支持多种储存引擎

3、连接操作

  1. 连接数据库

    mysql -uroot -p 密码
    
  2. 退出登录

    quit 和 exit
    或者
    ctrl + d
    
  3. 登录后的查看效果

    查看版本:select version();
    显示当前时间:select now();
    
  4. 修改提示符

    prompt python>
    

4、数据库操作

  1. 查看所有数据库

    show databases;
    
  2. 使用数据库

    use 数据库名;
    
  3. 查看当前使用的数据库

    select database;
    
  4. 创建数据库

    create database 数据库名 charset=utf8;
    例如:
    create database python charset=utf8;
    
  5. 删除数据库

    drop database 数据库名;
    例如:
    drop database python;
    

5、数据表操作

  1. 查看当前数据库中所有表

    show tables;
    
  2. 查看表结构

    desc 表名;
    
  3. 创建表

    CREATE TABLE table_name(
        column1 datatype contrai,
        column2 datatype,
        column3 datatype,
        .....
        columnN datatype,
        PRIMARY KEY(one or more columns)
    );
    例如:
    	创建班级表
    		create table classes(
    	    id int unsigned auto_increment primary key not null,
    	    name varchar(10)
    	);
    	创建学生表
    		create table students(
    		id int unsigned primary key auto_increment not null,
    	    name varchar(20) default '',
    	    age tinyint unsigned default 0,
    	    height decimal(5,2),
    	    gender enum('男','女','人妖','保密'),
    	    cls_id int unsigned default 0
    	)
    
  4. 修改表

    添加字段
    	alter table 表名 add 列名 类型;
    	例如:
    	alter table students add birthday datetime;
    修改字段(重命名)
    	alter table 表名 change 原名 新名 类型及约束;
    	例如:
    	alter table students change birthday birth datetime not null;
    修改字段(不重命名)
    	alter table 表名 modify 列名 类型及约束;
    	例如:
    	alter table students modify birth data not null;
    删除字段
    	alter table 表名 drop 列名;
    	例如:
    	alter table students drop birthday;
    
  5. 删除表

    drop table 表名;
    例如:
    drop table students;
    
  6. 查看表的创建语句

    show create table 表名;
    例如:
    show create table classes;
    

6、数据增删改查

  1. 查询
    # 所有列
    	select * from 表名;
    	例如:
    	select * from classes;
    # 指定列
    # 可以使用as为列或表指定别名
    	select 列1,列2... from 表名;
    	例如:
    	select id,name from classes;
    
  2. 增加
    # 全列插入(值的顺序与表中字段的顺序对应)
    	insert into 表名 values(...);
    	例如:
    	insert into students values(0,'郭靖','蒙古','2013-03-21');
    # 部分插入
    	insert into 表名(1,...) values(1,...);
    	例如:
    	insert into students(name,hometown,birthday) values('黄蓉','桃花岛','2014-6-5');
    # 全列多行插入
    	insert into 表名 values(...),(...)...;
    	例如:
    	insert into classes values(0,'python12'),(1,'python34');
    	或者
    	insert into 表名(1,...) values(1,...),(1,...);
    	例如:
    	insert into students(name) values('哈哈'),('滴滴');
    
    
  3. 修改
    update 表名 set1=1,2=2... where 条件
    例:
    update students set gender=0,hometown='北京' where id=5;
    
  4. 删除
    delete from 表名 where 条件;
    例如:
    delete from students where id=5;
    
    # 逻辑删除,本质就是修改操作
    update students set isdelete=1 where id=1;
    

7、数据备份与恢复

  1. 备份
    mysqldump -uroot -p 密码 数据库名 > python.sql;
    
  2. 恢复
    mysql -uroot -p 密码 新数据库名 < python.sql;
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值