pythonWeb~肆~MySql

1. 初识网站

  • 默认编写的静态的效果
  • 动态:需要用到Web框架的功能。
    在这里插入图片描述

可以做数据的存储媒介有:

  • txt文件

  • excel文件

  • 专业的软件:数据库管理系统。

    MySQL(*)免费
    Oracle/SQLServer/DB2/Access...
    

在这里插入图片描述

2.安装MySQL

MySQL,本质上就是一个软件。

  • 8.x
  • 5.x ,本次采用 5.7.31版本。

安装MySql步骤操作,可自行百度,保证MySql能正常使用即可

3.MySQL指令

在这里插入图片描述

在MySQL和我们平时认知不同的概念。

MySQL 认知
数据库 文件夹
数据表 文件(Excel文件)

3.1 数据库管理(文件夹)

  • 查看已有的数据库(文件夹)

    show databases;
    
  • 创建数据库(文件夹)

    create database 数据库名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    
    create database gx_day14 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    
  • 删除数据库(文件夹)

    drop database gx_day14;
    
  • 进入数据库(进入文件夹)

    use gx_day14;
    
  • 查看文件夹下所有的数据表(文件)

    show tables;
    

3.2 数据表的管理(文件)

  • 进入数据库(进入文件夹)

    use 数据库;
    use gx_day14;
    
  • 查看当前数据库下的所有 表(文件)

    show tables;
    
  • 创建表(文件文件)

    create table 表名称(
    	列名称 类型,
        列名称 类型,
        列名称 类型
    )default charset=utf8;
    

在这里插入图片描述

create table tb1(id int, name varchar(16),age int) default charset=utf8;
create table tb1(
    id int, 
    name varchar(16),
    age int
) default charset=utf8;
create table tb1(
    id int, 
    name varchar(16) not null,   -- 不允许为空
    age int null,                -- 允许为空(默认)
) default charset=utf8;
create table tb1(
    id int, 
    name varchar(16),
    age int default 3        -- 插入数据时,age列的值默认3
) default charset=utf8;
create table tb1(
    id int primary key,     -- 主键(不允许为空,不允许重复)
    name varchar(16),
    age int
) default charset=utf8;

主键一般用于表示当前行的数据的编号(类似于人的身份证)。

create table tb1(
    id int auto_increment primary key, -- 内部维护,自增
    name varchar(16),
    age int
) default charset=utf8;

一般情况下,我们再创建表时都会这样来写:【标准】

create table tb1(
    id int not null auto_increment primary key,
    name varchar(16),
    age int
) default charset=utf8;
mysql> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(16) | YES  |     | NULL    |                |
| age   | int(11)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
  • 删除表

    drop table 表名称;
    

常用数据类型:

  • tinyint

    有符号,取值范围:-128 ~ 127 (有正有负)【默认】
    无符号,取值范围:0 ~ 255(只有正)
    
    create table tb2(
        id int not null auto_increment primary key,
        age tinyint   -- 有符号:取值范围:-128 ~ 127
    ) default charset=utf8;
    
    create table tb3(
        id int not null auto_increment primary key,
        age tinyint unsigned -- 无符号:取值范围:0 ~ 255
    ) default charset=utf8;
    
  • int

    int				表示有符号,取值范围:-2147483648 ~ 2147483647
    int unsigned	表示无符号,取值范围:0 ~ 4294967295
    
  • bigint

    有符号,取值范围:-9223372036854775808 ~ 9223372036854775807
    无符号,取值范围:0  ~  18446744073709551615
    

    练习题:

    # 创建表
    create table tb2(
        id bigint not null auto_increment primary key,
        salary int,
        age tinyint
    ) default charset=utf8;
    
    # 插入数据
    insert into tb2(salary,age) values(10000,18);
    insert into tb2(salary,age) values(20000,28);
    insert into tb2(salary,age) values(30000,38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猴子key

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值