MySQL数据库基本操作之登录/退出,数据库(新建/删除/查看),以及表(新建/修改/删除)等操作

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

由于篇幅有限,表数据的增删改查(crud)请看:https://blog.csdn.net/qq_38276669/article/details/82286624

--01 mysql 数据库的操作
    ctrl + a 快速回到行首
    ctrl + e 回到行末
    ctrl + l 清屏
    ctrl + c + 回车  结束

    -- 链接数据库
	mysql -uroot -pmysql
    
	-- 不显示密码
    mysql -uroot -p
    mysql

    -- 退出数据库
    quit/exit/ctrl + d

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

    -- 显示时间
    select now();
    
	-- 查看当前使用的数据库
    select database();

    -- 查看所有数据库
    show databases;
	
    -- 创建数据库
    -- create database 数据库名 charset=utf8;
	create database python16;
    create database python16 charset=utf8;(注意)
	

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

    -- 使用数据库
    -- use 数据库的名字
    use python16;

    -- 删除数据库
    -- drop database 数据库名;
    drop database python16;

--02 数据表的操作

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

    -- 创建表
	-- int unsigned 无符号整形
    -- auto_increment 表示自动增长
    -- not null 表示不能为空
    -- primary key 表示主键
    -- default 默认值
    -- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
    create table xxxx (
        id int unsigned primary key not null auto_increment,
        name varchar(20)
    );
	

    -- 查看表结构
    -- desc 数据表的名字;
	desc xxxx;

   
    -- 创建 classes 表(id、name)
	create table classes(
        id int unsigned primary key not null auto_increment,
        name varchar(20) 
    );
	
	
    -- 创建 students 表(id、name、age、high (decimal)、gender (enum)、cls_id)
    create table students(
        id int unsigned primary key not null auto_increment,
        name varchar(20),
        age int unsigned,
        high decimal(5,2),
        gender enum("男","女","中性","保密") default "保密",
        cls_id int
    );


    -- 查看表的创建语句
    -- show create table 表名字;
    show create table students;


    -- 修改表-添加字段 mascot (吉祥物)
    -- alter table 表名 add 列名 类型;
    alter table classes add jixiangwu varchar(20);

    -- 修改表-修改字段:不重命名版
    -- alter table 表名 modify 列名 类型及约束;
	alter table classes modify jixiangwu varchar(30);


    -- 修改表-修改字段:重命名版
    -- alter table 表名 change 原名 新名 类型及约束;
	alter table classes change jixiangwu mascot varchar(20);


    -- 修改表-删除字段
    -- alter table 表名 drop 列名;
    alter table classes drop mascot;

    -- 删除表
    -- drop table 表名;
    -- drop database 数据库;
    drop table xxxx;

猜你喜欢

转载自blog.csdn.net/qq_38276669/article/details/82286576