MySQL引入

1 创建配置文件

  1. 安装目录
  2. 创建文件my.ini
  3. 文件内容
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

2 数据库基础操作

        登录数据库 mysql -uroot -p

2.1 创建数据库(文件夹)

create database 数据库名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

2.2 删除数据库(文件夹)

drop database 数据库名;

2.3 进入数据库(文件夹)

use 数据库名;

2.4 查看文件夹下所有的数据表(文件)

show tables;

2.5 创建表

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

create table tb1(id int, name varchar(16),age int) default charset=utf8;

2.5.1 创建表字段设置-必传/非必传

  create table tb1(
      id int, 
      name varchar(16) not null,   -- 不允许为空
      age int null,                -- 允许为空(默认)
  ) default charset=utf8;

2.5.2 创建表字段设置-默认值

 create table tb1(
      id int, 
      name varchar(16),
      age int default 3        -- 插入数据时,age列的值默认3
  ) default charset=utf8;

2.5.3 主键

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

 create table tb1(
      id int primary key,     -- 主键(这列不允许为空,不允许重复)
      name varchar(16),
      age int
  ) default charset=utf8;

2.5.4 标准创建表

 create table tb1(
      id int not null auto_increment primary key,  --auto_increment 自增数字,不用传值
      name varchar(16),
      age int
  ) default charset=utf8;

3 常用数据类型

 3.1 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;

3.2 int

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

3.3 bigint

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

3.4 decimal精准小数

        m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。

  create table tb3(
  	id int not null primary key auto_increment,
  	salary decimal(8,2)
  )default charset=utf8;
  
  insert into tb3(salary) values(1.28);
  insert into tb3(salary) values(5.289);
  insert into tb3(salary) values(5.282);
  insert into tb3(salary) values(122115.11);

3.5 字符串char

char(m),定长字符串,速度快,m代表字符串的长度,最多可容纳255个字符。

char(11),固定用11个字符串进行存储,哪怕没有11个字符,也会按照11存储。

create table tb4(
  	id int not null primary key auto_increment,
  	mobile char(11)
  )default charset=utf8;
  
  insert into tb4(mobile) values("151");
  insert into tb4(mobile) values("15131255555");

3.5 字符串varchar

varchar(m)节省空间。变长字符串(动态变化),m代表字符的长度。 最大65535字节/3 = 最大的m

varchar(11),真实数据有多少长久按照多长存储。

create table tb5(
  	id int not null primary key auto_increment,
  	mobile varchar(11)
  )default charset=utf8;
  
  insert into tb5(mobile) values("151");
  insert into tb5(mobile) values("15131255555");

3.6 text

text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。

一般情况下,长文本会用text类型。例如:文章、新闻等。

create table tb6(
  	id int not null primary key auto_increment,
      title varchar(128),
  	content text
  )default charset=utf8;

3.7 年月日时分秒

  YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)

3.8 年月日

  YYYY-MM-DD(1000-01-01/9999-12-31)

4 数据行操作

4.1 新增数据

insert into 表名(列名,列名) values(值,值);
insert into 表名(列名,列名) values(值,值),(值,值),(值,值),(值,值);

4.2 删除数据

delete from 表名; ---删除表内所有数据
delete from 表名 where 条件;
delete from tb7;
delete from tb7 where id = 3;
delete from tb7 where id = 4 and name="谢涛";
delete from tb7 where id = 4 or name="谢涛";
delete from tb7 where id > 4;
delete from tb7 where id >= 4;
delete from tb7 where id != 4;
delete from tb7 where id in (1,5);

4.3 修改数据

update 表名 set 列=值;
update 表名 set 列=值,列=值;
update 表名 set 列=值 where 条件;
update tb7 set password="哈哈哈";
update tb7 set email="哈哈哈" where id > 5;
update tb7 set age=age+10 where id > 5;

4.4 查询数据

select * from 表名称;
select 列名称,列名称 from 表名称;
select 列名称,列名称 from 表名称 where 条件;
select * from tb7;
select id,name from tb7;
select id,name from tb7 where id > 10;
select id,name from tb7 where name="xx" and password="xx";

5.Python链接数据库

# -*- coding: utf-8 -*-
import pymysql.cursors


class Mysql:

    def __init__(self):
        # 打开数据库连接
        self.db = pymysql.Connect(
            host='localhost',
            port=3306,
            user='root',
            passwd='',
            db='MySQL',
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor)
        # 使用 cursor() 方法创建一个游标对象 cur
        self.curor = self.db.cursor()

    def getMysql(self, sql, numb=1):
        # 使用 execute()  方法执行 SQL 查询
        self.curor.execute(sql)
        if numb == 1:
            # 使用 fetchone() 方法获取单条数据,格式是元组
            res = self.curor.fetchone()[0]
        else:
            # 使用fetchall() 方法获取多条数据,格式是列表嵌套元组
            res = self.curor.fetchall()
        # 关闭游标
        self.curor.close()
        # 关闭连接
        self.db.close()
        # 返回数据
        return res

    def otherMysql(self, sql):
        try:
            # 使用 execute()  方法执行SQL
            self.curor.execute(sql)
            # 提交更改
            self.db.commit()
            # 关闭游标
            self.curor.close()
            # 关闭连接
            self.db.close()
            print('操作数据库成功')
        except:
            print('操作数据库失败')
            # 回滚
            self.db.rollback()
            # 关闭连接
            self.db.close()
            raise


if __name__ == '__main__':
    db = Mysql()
    db.otherMysql("insert into unicom.admin(username,password,mobile) values('yueqiang','123123',17610266910)")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秦朝胖子得加钱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值