MySql:简单使用

pymysql:

from pymysql import connect
# 连接
db = connect(host=host, user=user, password=password, database=database)

cursor = db.cursor()
# 执行mysql命令
cursor.execute(cmd)
# 返回查询的数据
cursor.fetchone() # fetchall()
# 返回执行命令后影响的数据条数
cursor.rowcount
# 提交保存
db.commit()
# 回退至上一次操作
db.rollback()
db.close

Mysql基本语句:

# mysql -u root -p
# 大小写不敏感
# 数据库
## 查看所有数据库
SHOW DATABASES;

## 创建数据库
create database Practice;

## 选择数据库
USE Practice;

## 删除数据库
DROP DATABASE Practice;
# 数据表
## 查看表
show tables;
## 建表
create table if not exists student(id  int primary key, name char(10) not null, age int);
create temporary table tablename(select * from student) # 临时表,数据库断开链接后销毁
## 删除表
drop table student;

## 更改表结构
alter table tablename add colname coltype first| after col;# 在第一行或某一行后添加列
alter table tablename drop colname;# 删除列
alter table tablename modify colname newtype not null default 100; # 更改某一列的数据类型,设置不为空,默认值为100
alter table tablename change colname newcolname newtype; # 更改某一列的名称和数据类型
alter table tablename alter col set default 100; # 修改默认值
alter table tablename alter col drop default; # 设置无默认值
alter table tablename rename to newtablename; #修改表名
alter table tablename add column sum float GENERATED ALWAYS AS (col1+col2) VIRTUAL; # 添加列,列的值为其他列计算所得 GENERATED ALWAYS as <calculation> virtual

# 数据
## 增
insert into tablename(col1,col2..) values(val1,val2..),(vala, valb...), ..;

## 删
delete from tablename where col=val;

## 改
update tablename set col=newval where col2=val;
update tablename set col=replace(col,"old-string","new-string")更新部分字符

## 查
select col1,col2 from table where col1=val1 and col2 like "%val%" order by col2 desc limit 2;

    DESC: 降序 ASC: 升序(默认)

## union
SELECT country FROM Websites UNION SELECT country FROM apps;
        union:对两个表取出的值合并,并去重
        union all: 不去重
## join 连接
inner|outer|left|right|across join 交集|并集|交集+左表|交集加右表|交叉连接(a * b)
        select a.col1,b.col2 from table1 a inner join tabl2 b on a.col3 = b.col4
        a,b :给表取别名,缩短代码
        on: 筛选条件,

# 计算
算术运算符:
        + - * / %
    比较运算符:
        = != >  < <= >= between  not between  in  not in  like rlike(正则匹配) is null  is not null  <=>(严格比较两个null是否相等)
        between 1 and 10  在1或10之间
        in (1,2,3,4) 在1234中m
    逻辑运算符:
        not and or xor(异或)
    位运算:
        & | ^(按位异或) ! <<  >>
select max/min(col) from table;  求某一列的最值
select greatest/least(col1,col2,col3) from table; 求某一行中的某几列的最值

# 索引
当查询条件中包含拥有索引的列名时,会调用对应的索引来进行查找
## 查看
show index from tablename\G # \G 格式化输出
## 创建
1. 直接创建
CREATE INDEX index_name ON table_name (col1[ASC|DESC], col2 [ASC|DESC], ...);
2. 改表属性添加
ALTER TABLE table_name ADD INDEX index_name (col1 [ASC|DESC], col2 [ASC|DESC], ...);
3. 创建表时指定
CREATE TABLE students (id INT PRIMARY KEY,age INT,INDEX idx_age (age));

## 删除索引
drop index index_name  on tablename;

## 唯一索引:唯一索引确保索引中的值是唯一的,不允许有重复值。
create unique index index_name on table(..)

#导出:
        select * from table into outfile filepath
#导人:
        mysql -u用户名    -p密码    <  要导入的数据库数据(.sql)






        



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值