zoukankan      html  css  js  c++  java
  • Python:MySQL数据库

    数据库的操作

    链接数据库

    句式:mysql -uroot -p密码
    例子:mysql -uroot -p123456

    查看所有数据库    sql语句最后需要有分号结尾,分号不能是中文的

    show databases;     

    创建数据库

    句式:create database 数据库名 charset=utf8;
        错误例子:create database python34luoye;
        正确例子:create database python34luoye charset=utf8;

    注意:一定不要忘记写charset=utf8;

    查看创建数据库的语句

    show create database python34luoye;

    查看当前使用的数据库

    select database();

    使用数据库

    句式:use 数据库的名字
    例子:use python34luoye;

    删除数据库

    句式:drop database 数据库名
    例子:drop database python34luoye;

    数据表的操作

    查看当前数据库中所有的数据表

    show tables;

    创建表

    创建班级表 (id、name)
    句式:create table 数据表的名字(字段 类型 约束[,字段 类型 约束]);
    示例:create table classes(
            id int unsigned auto_increment primary key not null,
            name varchar(30) not null
        );
    
    auto_increment 表示自动增长
    primary key 表示主键
    not null 表示该字段不能为空
    varchar(30) 表示字符串类型, 30代表长度

    查看表的创建语句

    句式:show create table  表名字;
    例子:show create table classes;

    查看表结构

    句式:desc 数据表的名字
    例子:desc classes;

    修改表-添加字段

    句式:alter table 表名 add 列名(也是字段名) 类型
    例子:alter table students add birthday datetime;

    修改表-修改字段: 重命名版 直接修改字段的名字

    句式:alter table 表名 change 原名 新名 类型 约束
    例子:alter table students change birthday birth date default '2001-01-01';

    修改表- 删除字段

    句式:alter table 表名 drop 列名
    例子:alter table students drop high;

    删除表

    句式:drop table 表名;
    例子:drop table classes;
  • 相关阅读:
    TOJ1017: Tour Guide
    tzcacm去年训练的好题的AC代码及题解
    Educational Codeforces Round 40 (Rated for Div. 2)
    AtCoder Regular Contest 092
    浙南联合训练赛20180318
    [Offer收割]编程练习赛50
    牛客练习赛13
    AtCoder Regular Contest 091
    Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)
    csa Round #73 (Div. 2 only)
  • 原文地址:https://www.cnblogs.com/wutongluo/p/12661283.html
Copyright © 2011-2022 走看看