mysql数据库基本操作学习记录


安装MySQL

安装mysql很简单,这里不多赘述了,网上很多教程。
mysql下载地址
图形界面工具(gitee)
图形界面工具(github)
视频教程
文章

开启mysql

win+R
开启:net start mysql
关闭:net stop mysql

mysql一些简单操作

显示当前数据库 show databases;

使用一个数据库 use test;

重命名

– 修改表名
rename table old_table to new_table;

– 或者
alter table old_table rename as new_table;

– 修改列名称
alter table table_name
change column old_name new_name varchar(255);

– 修改字段类型
alter table table_name
modify column column_name varchar(255) default ‘’ COMMENT ‘注释’;

插入数据

insert into score (name,score) values(‘小明’,’80’);

删除

drop table tb;
drop 是直接将表格删除,无法找回。例如删除 user 表:
drop table user;
truncate (table) tb;
truncate 是删除表中所有数据,但不能与where一起使用;
TRUNCATE TABLE user;
delete from tb (where);
delete 也是删除表中数据,但可以与where连用,删除特定行;
— 删除表中所有数据
delete from user;
— 删除指定行
delete from user where username =’Tom’;

delete from score where score = 80;

查询

select * from score where name like ‘%刘%’;

python 连接mysql

import pymysql
from pymysql.cursors import DictCursor
# 创建数据库连接  localhost等效于127.0.0.1
conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="123456",db="test",charset="utf8")
# 建立游标,指定游标类型,返回字典
cur = conn.cursor(DictCursor)
# 操作语句,只查询前两行

try:
    sql = """select download_url from urls where file_name like '%cat%' limit 20;"""
    # 执行sql语句
    cur.execute(sql)
    # 获取查询的所有结果
    res = cur.fetchall()
    # 打印结果
    # print(res)
    with open("xxx.md","a+" )as f:
        for x in res:
            # print(x['download_url'])
            f.write(f"![]({x['download_url']})\n")
except:
    print("操作失败")
finally:
    # 关闭游标
    cur.close()
    # 关闭连接
    conn.close()

PS :以后一些操作会记录。
23333


文章作者: anlen123
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 anlen123 !
 上一篇
Linux命令学习 Linux命令学习
后台运行命令nohup 命令 & 如:nohup python test.py & 可用ps -e查看 具体看哪个运行可以这么写 ps -e|grep xxxx 如我想看后台的python命令: ps -e|grep
2020-02-19 anlen123
下一篇 
vim的自我修养 vim的自我修养
更新vim8 wget https://github.com/vim/vim/archive/v8.1.1766.tar.gz tar -zxvf v8.1.1766.tar.gz cd vim-8.1.1766/ ./config
2019-10-31 anlen123
  目录