梅科尔工作室-MySQL学习笔记

MySQL指令

数据库管理:

  • 查看已有数据库

    show databases;
    
  • 创建数据库

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

    drop database 数据库名字;
    
  • 进入数据库

    use 数据库名字;
    
  • 查看当前数据库中所有的数据表

    show tables;
    

数据表管理:

  • 创建表

    create table 表名称(
    	列名称 类型,
    	列名称 类型,
    	列名称 类型,
    )default chartset=utf8;
    
    例子:
     create table tb1(
    	id int auto_increment primary key,				 
    	name varchar(16) not null,		
    	age int default 3			   
    )default charset=utf8;
    
    	-- primary ket:主键(不允许为空,不允许重复),一般用于表示当前行的数据的编号
    	-- auto_increment:内部维护自增
    	-- not null:不允许为空、null:允许为空
    	-- default 3:插入数据时设置默认值为3
    
  • 删除表

    drop table 表名称;
    
  • 查看表结构详细信息

    desc 表名称;
    

数据行操作

  • 新增数据

    insert into 表名(属性1,属性2) values(值1,值2;
    
    insert into 表名(属性1,属性2) values(值1,值2,(值,值),(值,值);	-- 批量插入 行数据
    
  • 删除数据

    delete from 表名;
    
    delete from 表名 where 条件;
    
  • 修改数据

    update 表名 set=;
    
    update 表名 set=,=;
    
    update 表名 set=where 条件;
    
  • 查询数据

    select * from 表名;
    
    select 列名,列名 from 表名 where 条件;
    
    create table tb3(
    	id int not null auto_increment primary key,	
        username varchar(64) not null,
        password char(64) not null,
        email varchar(64) not null,	
    	age tinyint	,
        salary decimal(10.2),
        ctime datetime
        
    )default charset=utf8;
    insert into tb3(name,password,email,age,salary,ctime)values("申晨哲","123","xxx@xx.com",19,1000.99,"2022-02-22 09:29:33");
    insert into tb3(name,password,email,age,salary,ctime)values("张三","123","xxx@xx.com",19,1000.99,"2022-02-22 09:29:33");
    insert into tb3(name,password,email,age,salary,ctime)values("李四","123","xxx@xx.com",19,1000.99,"2022-02-22 09:29:33");
    insert into tb3(name,password,email,age,salary,ctime)values("王五","123","xxx@xx.com",19,1000.99,"2022-02-22 09:29:33");
    insert into tb3(name,password,email,age,salary,ctime)values("赵六","123","xxx@xx.com",19,1000.99,"2022-02-22 09:29:33");
    
    create table admin(
    	id int not null auto_increment primary key,	
        username varchar(64) not null,
        password varchar(64) not null,
        mobile varchar(64) not null
    )default charset=utf8;
    

常用数据类型

  • 整数

    • tinyint

      tinyint				-- 默认为有符号,-128 ~ 127
      tinyint unsigned	 -- 无符号,0 ~ 255
      
    • int

    • bigint

  • 浮点数

    • float

    • double

    • decimal

      decimal(m,d)		-- m是数字总位数(负号不算),d是小数点后位数。最多位数 65 = ? + 30 ,存储会四舍五入
      
  • 字符串

    • char 定长字符串:只按固定长度存储(最大为255个字节),数据太长会报错(优点:查询速度更快)
    • varchar 变长字符串:数据多长就按多长存储(最大65535字节,/3=21844个汉字)
    • text 用于保存变长的大字符串,最多2**16-1=65535个字节。一般用于文章、新闻等长文本
    • mediumtext 2**24-1
    • longtext 2**32-1
  • 时间

    • datetime:YYYY-MM-DD HH:MM:SS
    • date:YYYY-MM-DD

利用Python操作数据库

创建数据:

import pymysql

while True:
    user = input("用户名:")
    if user.upper == 'Q':
        break
    pwd = input("密码:")
    mobile = input("手机号:")

    # 1. 连接MySQL
    conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="123456", charset='utf8',db='mydb')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    # 2.1 发送指令
    cursor.execute("insert into user(username,password,mobile) values('zhangsan','qwe123','16887988')")
    conn.commit()

    # 3. 关闭连接
    cursor.close()
    conn.close()

    ——————————————————————————————————————————————————————————————————————
    # 2.2 发送指令
    sql = "insert into user(username,password,mobile) values(%s,%s,%s)"
    cursor.execute(sql,[user, pwd, mobile])
    conn.commit()


    # 2.3 发送指令
    sql = "insert into user(username,password,mobile) values(%(n1)s,(n2)%s,(n3)%s)"
    cursor.execute(sql,{"n1":user, "n2":pwd, "n3":mobile})
    conn.commit()

查询数据:

# 2. 发送查询指令
cursor.execute("select * from user where id > %s",[2, ])
data_list = cursor.fetchall()		// fetchall()获取的是 [字典,字典],无数据是空列表
for row_dict in data_list:			// fetchone()只获取符合条件的的第一条数据,是个字典,无数据是None
print(row_dict)

删除数据

# 2. 发送删除数据指令
cursor.execute("delete from user where id = %s",[3, ])
conn.commit()

修改数据

# 2. 发送修改数据指令
cursor.execute("update user set mobile = %s where id = %s",["18888888", 4,  ])
conn.commit()

注意:新增、删除、修改数据时,一定要记得conn.commit(),不然数据库没有数据

​ 查询时不需要,但是要fetchall()/fetchone()

​ 对SQL语句不要用Python的字符串格式化进行拼接(会被SQL注入),一定要用 execute + 参数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值