mysql笔记一之库操作与 表操作

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/kan2016 https://blog.csdn.net/kan2016/article/details/82771110


数据库用户名: root
密码:            mysql

char(5) 长度固定为5的字符串 ab  --> "ab   "
varchar(5) abc --> "abc" 不能够超出长度


1.-- 数据库的操作

    -- 链接数据库
    mysql -uroot -pmysql


    -- 退出数据库
    exit 或者  quit  或者 ctr + d

    -- 查看创建数据库
    show databases;

    -- 查看当前使用的数据库
    select database();

    -- 使用数据库 切换
    use python_info;

    -- sql语句最后需要有分号;结尾
    -- 显示数据库版本
    select version();
    -- 显示时间
    select now();


    -- 创建数据库 create 
    create database demo;
    -- 指定字符集
    create database demo charset=utf8;

    -- 查看数据库的创建语句
    show create database demo;

    -- 删除数据库
    drop database demo;

    


2.-- 数据表的操作

    -- 查看当前数据库中所有表
    show tables;


    -- 创建表
    -- auto_increment表示自动增长
    -- 创建一个学生的数据表(id、name、age、high、gender、cls_id)
    -- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
    -- 字段名 + 类型 + 约束(不分先后顺序)
    -- unsigned不能够为负数
    -- facebook 56个性别
    -- enum 表示枚举
    -- 班级id 在班级表中设置的是 int unsigned类型  学生表中需要引用班级的编号 需要保证类型是一致的
    create table students(
        id int unsigned primary key auto_increment not null,
        name varchar(10) not null,
        age tinyint unsigned default 0,
        high decimal(5,2) default 0,
        gender enum("男", "女", "中性", "保密"),
        cls_id int unsigned default 0
    );
    -- 创建students表

    -- 查看表的创建语句
    show create table classes;
    engine(引擎) = innodb (支持外键并且还是事务)  转账 a - 500-> b 500元  需要使用事务 提供了一种可以撤销的机制

    MyISAM 全文检索引擎  查询速度  更新非常慢 不支持 事务 和外键
    -- 查看表结构
    desc classes;
    +-------+------------------+------+-----+---------+----------------+
    | Field | Type             | Null | Key | Default | Extra          |
    +-------+------------------+------+-----+---------+----------------+
    | id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(15)      | NO   |     | NULL    |                |
    +-------+------------------+------+-----+---------+----------------+

    
    -- 修改表结构  alter
    -- 修改表-添加字段
    -- alter table 表名 add 列名 类型/约束;
    -- 生日信息 
    alter table students add birthday datetime;

    -- 修改表-修改字段:不重命名版
    -- alter table 表名 modify 列名 类型及约束;
    alter table  students modify birthday date;

    -- 修改表-修改字段:重命名版
    -- alter table 表名 change 原列名 新列名 类型及约束;
    alter table students change birthday birth date;

    -- 删除对应的字段
    alter table students drop birth;

    -- 删除表
    drop table students;
 

猜你喜欢

转载自blog.csdn.net/kan2016/article/details/82771110
今日推荐