mysql的常用命令

通过python连接mysql创建一个表

# !/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='testdb', charset='utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
#cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
#data = cursor.fetchone()

#print("Database version : %s " % data)

# 关闭数据库连接

sql = """CREATE TABLE EMPLOYEE (
         FIRST_NAME  CHAR(20) NOT NULL,
         LAST_NAME  CHAR(20),
         AGE INT,  
         SEX CHAR(1),
         INCOME FLOAT )"""

cursor.execute(sql)

# 关闭数据库连接
db.close()

查看数据库中的表
mysql> desc EMPLOYEE;
+------------+----------+------+-----+---------+-------+
| Field      | Type     | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| FIRST_NAME | char(20) | NO   |     | NULL    |       |
| LAST_NAME  | char(20) | YES  |     | NULL    |       |
| AGE        | int(11)  | YES  |     | NULL    |       |
| SEX        | char(1)  | YES  |     | NULL    |       |
| INCOME     | float    | YES  |     | NULL    |       |
+------------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)

插入数据

# !/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='testdb', charset='utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
#cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
#data = cursor.fetchone()

#print("Database version : %s " % data)

# 关闭数据库连接

sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
         LAST_NAME, AGE, SEX, INCOME)
         VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
cursor.execute(sql)
db.commit()

# 关闭数据库连接
db.close()

查看表中的数据
mysql> select * from EMPLOYEE;
+------------+-----------+------+------+--------+
| FIRST_NAME | LAST_NAME | AGE  | SEX  | INCOME |
+------------+-----------+------+------+--------+
| Mac        | Mohan     |   20 | M    |   2000 |
+------------+-----------+------+------+--------+
1 row in set (0.00 sec)

查询表中的数据并且打印出来

# !/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='testdb', charset='utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
#cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
#data = cursor.fetchone()

#print("Database version : %s " % data)

# 关闭数据库连接

sql ="SELECT * FROM EMPLOYEE WHERE INCOME > '%d'" % (1000)
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
    fname = row[0]
    lname = row[1]
    age = row[2]
    sex = row[3]
    income = row[4]
    print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % (fname, lname, age, sex, income ))
# 关闭数据库连接
db.close()

#输出
[root@shanwu python]# python3 db5.py
fname=Mac,lname=Mohan,age=20,sex=M,income=2000

数据库的更新操作

# !/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='testdb', charset='utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
#cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
#data = cursor.fetchone()

#print("Database version : %s " % data)

# 关闭数据库连接

sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
cursor.execute(sql)
db.commit()
# 关闭数据库连接
db.close()

#查看数据库
mysql> select * from EMPLOYEE;
+------------+-----------+------+------+--------+
| FIRST_NAME | LAST_NAME | AGE  | SEX  | INCOME |
+------------+-----------+------+------+--------+
| Mac        | Mohan     |   21 | M    |   2000 |
+------------+-----------+------+------+--------+
1 row in set (0.00 sec)

删除操作

# !/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='testdb', charset='utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询
#cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
#data = cursor.fetchone()

#print("Database version : %s " % data)

# 关闭数据库连接

sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
cursor.execute(sql)
db.commit()
# 关闭数据库连接
db.close()

查看数据库
mysql> select * from EMPLOYEE;
Empty set (0.00 sec)

mysql的常用操作


查看有那些库
show databases;
查看某个库里的表
use db;show tabels;
查看表的字段
dec db;
查看建表的语句
show create table tb;
当前是哪个用户
show user();
查看当前库
select database();
创建库
create database db;
创建表
create table t1(
idint4,namechar(40));
插入数据
insert into t1(id,name)values(1,'aming');
查看数据库的版本
select version();
查看mysql状态
show status;
修改mysql参数
show variables like'max_connect%';
set global max_connect_errors=1000;
查看mysql队列
show processlist;
创建普通用户并授权
grant all on *.* to user1 indentified by '123456';
grant all on *.* to 'user2'@'10.10.10.10' indentified by '123456';
grant all on *.* to 'user2'@'%' indentified by '123456';
更改密码
updata mysql.user set password=password('密码') where user='root';
查询
select count(*) from mysql.user;
select * from mysql.bd;select * from mysql.db where host like '10.0.%';
插入
updata db1.t1 set name='aaa' where id=1;
清空表
truncate table db1.t1;
删除表
drop table db1.t1;
修复表
repair table tb1 [user frm];
在shell的命令下执行mysql的操作
mysql -uroot -paminglinux mysql -e "show tables"
-e前面是库的名字,后面是操作
多表联合查询
select a.id a.name, c.id from a join c on c.组id = a.id and 条件
删除表中的一条数据
delete from table where 条件判断(没有条件的话就是删除整张表)

猜你喜欢

转载自blog.csdn.net/wushan1992/article/details/80260286