MySQL常用命令及pycharm链接MySQL

MySQL常用命令及pycharm链接MySQL
1.mysql 的基本使用命令
1.启动mysql服务
1.windows中启动服务
net start mysql
关闭: net stop mysql
2.linux启动服务, 默认开启的
service mysql start
关闭: service mysql stop
2.进入mysql
命令: mysql -u 用户名 -p
然后输入密码即可进入, 如果看到 mysql> 则成功进入

**3.window与linux的数据库命令一致
4.查看数据库的版本
select version();
5.查看当前的时间
select now();
6.退出
exit 或者 quit

----增,删,改,查

2.库的操作
1.展示所有的数据库
show databases;
展示所有的已经存在的数据库
mysql默认会有管理自己的库,表,用户,配置的数据库
2.创建一个数据库
格式: create database 数据库名 charset=“utf8”;
例: create database school charset=“utf8”;

3.删除一个数据库
   格式:  drop  database  数据库名;
   例: drop  database  school;
 
4.改**

5.使用某个仓库
  格式: use 数据库名
  
6.查看当前使用的数据库:  
   格式: select  database();

3.表的操作
1.查询当前库中的所有的表
格式: show tables;
2.创建一个表
格式: create table 表名(字段名 字段描述,字段名n 字段描述,…);
例: create table student(id int primary key auto_increment,name varchar(20) not null,age int default 17,address varchar(20),sex bit default 1);

 例: create  table student(id int primary key auto_increment,name varchar(20)  not null,age int default 17,address varchar(20),sex bit default 1,data1 varchar(20),data2 varchar(20));
  1. 查看表的结构
    1.格式: desc 表名;
    2.格式2: show create table 表名; 查看创建表的sql语句

  2. 删除一个表
    格式: drop table 表名;
    例: drop table student;

5.修改表
1.修改表名
格式1: rename table 旧表名 to 新表名;
2.修改表结构:
格式: alter table 表名 add|drop|change
1.添加一个新的字段
格式: alter table 表名 add 字段名 字段描述;
例: alter table student add phonenumber varchar(20);
2.删除一个字段
格式: alter table 表名 drop 字段名;
例: alter table student drop phone;

      3.修改一个字段
     格式: alter table 表名  change 要修改的字段名 新的字段名  新的字段类型描述;
       例: alter table student change phonenumber  phone  varchar(20);
   **3.开发过程中尽量的不要修改已经有数据的字段 ,
       设计表的时候尽量设计一些预留字段

4.数据的操作
1.增加数据
1.插入一条数据
格式: insert into 表名 values(对应的值1,对应的值2,对应的值n)
例: insert into student values(0,“三胖胖”,38,“平壤”,1);
注意:插入的值要与表对应字段一一对应
如果是自动增长的数据类型,将该值设置为 0即会自动的增长
2. 缺省值插入
 格式: insert into 表名(字段1,字段2,字段n,…) values(字段1对应的值,字段2对应的值,字段n对应的值,…);
例: insert into student(name,age,sex) values(“三棒子”,74,0);
3. 插入多条数据
格式: insert into 表名 values(对应的值1,对应的值2,对应的值n),(对应的值1,对应的值2,对应的值n) ,(对应的值1,对应的值2,对应的值n) …;
例: insert into student values(0,“习大大”,66,“北京”,1),(0,“李克强”,55,“北京”,1),(0,“习书记”,67,“中南海”,1),(0,“朱镕基”,78,“北京”,1);

2.删除数据
格式: delete from 表名 where 条件
例1: delete from student where id =2;
delete from student where name =‘三棒子’;
注意:删除一条数据后,该数据后的数据对应的id不会变

3.修改数据
格式: update 表名 set 字段名 = 值1,字段名2 = 值2 where 条件
例2: update student set name = ‘金三胖胖’, address = ‘中国’ where id =1;

4.查询数据库的表中的所有数据
查询所有数据 格式: select * from 表名;
例: select * from student;

5.查询语句
1.格式1: select 字段名1,字段名n,… from 表名 where 查询条件
格式2: select 字段名1 as 别名,字段名n,… from 表名 where 查询条件

  1.select表示查询
  2.select 后面是查询结果要显示的字段名
         例: select name,age  from student; 
      3. * 表示显示所有字段的数据    
  4.from 后面是表名,表示从那个表中查询
  5.where 条件  表示以某个条件进行筛选
  6.如果没有where条件,表示查询所有
      7.字段名1 as 别名   可以给显示的字段名取个别名,方便查看

  需求: 展示所有的学生的姓名和年龄?
      select name as '姓名',age as '年龄'  from student;
      
   
   查询所有:  select * from 表名
2.查询条件 表达式    
   1.比较运算符
     >   大于
 <   小于
 >=  大于等于
 <=   小于等于
 =    等于 
 !=   不等于
    需求: 查询班上大于60岁的学生 ?
  例: select *  from student where age > 60;
     需求: 查询班上小于等于55岁的学生 ?
	select *  from student where age <= 55;
      需求: 查询班上不等于55岁的学生 ? 
	select *  from student where age != 55;

  2.逻辑运算符 
 且   and
 或   or  
 非   not 
      
 需求: 查询班上大于55岁且小于70岁的学生 ?
       select * from student where age>55 and age < 70;
 需求: 查询班上除了北京的学生
   select * from student where not address = '北京';
   3.模糊运算符
     格式:  ..... where 字段名  like  '字符串'
 任意字符: % 任意多个任意字符
           _ 一个任意字符
      
   需求:  查询姓习的同学?
select * from student where name  like '习%';
   需求: 查询姓习的同学,且只有2个字?    	
       select * from student where name  like '习_';
   需求:  查询名字中包含  胖  字的同学
     insert into student values(0,"大胖胖",66,"北京",1),(0,"三胖子",55,"北京",1),(0,"胖胖胖",67,"中南海",1),(0,"胖娃娃",78,"北京",1);
       select * from student where name like '%胖%';
   
   4.范围运算符
       成员运算符 
      格式1:  .... where 字段名  in  (值1,值2,值3)
        是否等于 () 中的某一个值

     格式2:   ... where  字段名  between 值1 and 值2   

      需求: 查询出住在北京或者 中南海或者.. 的同学 ?
       select * from student where address in  ('北京','中南海');
   
   5.空值判断
     格式1:   .... where  字段名 is null;
 需求:将address为空的所有数据筛选出来?
      .. where  address is null;

     格式2:   .... where  字段名 is not null;
 需求:将address不为空的所有数据筛选出来? 
          ...where  address is not null;
     
   6.运算符的优先级
      1.以上运算符可以综合使用
  2. 加上() 

3.聚合函数
count(*) 统计查询结果的数量
max(字段名) 统计某个字段的最大值
min(字段名) 统计某个字段的最小值
avg(字段名) 统计某个字段的平均值
sum(字段名) 统计某个字段的总和

格式: select 聚合函数  from  表名 where 条件
需求: 查询该表总共有多少学生
      select count(*)  from student;
需求: 获取学生的最大年龄?
      select max(age)  from student;
需求: 统计年龄的平均值 ?
      select avg(age) from student;

4.分组 group by
格式: select … from 表名 where 条件 group by 字段名

 **统计某个字段有多少种值
需求: 查看有多少种地址 ? 
    select address  from student group by address;
需求:查询每个地址有多少人?   
      select address,count(*)  from student group by address;

需求: 再查询出中南海有多少人 ?
    select address,count(*)  from student  group by address having address = '中南海';
   


    having  条件   表示在某个结果集上继续筛选

注意: where 与having 后面都是跟一个条件表示查询,
      但是where是先筛选, having是在where的结果后再筛选

5.排序 order by
格式: select … from 表名 where 条件 order by 字段名 排序规则;
需求:查询所有学生,并按年龄排序(升序,默认的) ?
select * from student order by age desc;
降序: desc
升序asc 默认的

  需求: 多个字段排序
    格式: select ..... from 表名  where 条件  order by 字段名 排序规则,字段名2 排序规则;

6.分页:
格式: select … from 表名 where 条件 limit 值1,值2;
例: select * from student limit 1,4;

  值1 表示的是分页的起始位置,  注意: 从 0 开始  
  值2 表示的是每一页的结果数量 
   
 获取第n页数据,  每一页4条数据
  select * from student limit  4(n-1),4;

pycharm链接MySQL

#pip 安装 pymysql模块
#导入pymysql模块,用来帮助操作mysql数据库
import  pymysql

连接mysql数据库
用户名, 密码, 主机名,端口号
host = None, con 指定主机名, ip地址, 127.0.0.1和localhost代表主机
user = None, 指定用户名
password = “”, 指定密码
database = None, 指定连接的数据库
port = 0, 指定数据库端口号 默认是3306
charset = ‘’, 指定编码

connect = pymysql.Connect(host="10.31.160.135",user="hz1805",password = "123456",database="school",charset='utf8')

# 执行sql语句
# 获得cursor对象,cursor对象是用来执行sql语句的
cursor = connect.cursor()
# 执行sql语句 获得MySQL版本
cursor.execute("select version();")

# 获得执行后的结果
res = cursor.fetchone()
print(res)

1、创建一个表

# 如果该表已经存在,则删除该用户
sqlFood = "drop table if EXISTS  food"

cursor.execute(sqlFood)

# 创建一个表
createSql = "CREATE TABLE food(id INT PRIMARY  KEY  auto_increment,name VARCHAR(20),price INT)"

cursor.execute(createSql)

2、 数据添加

# 增 删 改 需要用到回滚 --------查
try:
    sqlInsert = "insert into food values(0,'麻辣香锅',99);"
    sqlInsert2 = "insert into food values(0,'6块钱的麻辣烫',6);"
    # sqlInsert2 = "insert into food values(0,'xxxxxxxx','6块钱的麻辣烫',6);"
    sqlInsert = "insert into food values(0,'传奇冒菜',9);"
    sqlInsert2 = "insert into food values(0,'麻辣小龙虾',64);"
    sqlInsert = "insert into food values(0,'红烧大肘子',91);"
    sqlInsert2 = "insert into food values(0,'麻辣火锅',66);"
    cursor.execute(sqlInsert)
    cursor.execute(sqlInsert2)
#     commit提交
    connect.commit()

except:  #只要出现异常就回滚sql
    # 用sql进行数据操作的时候,通常需要保证数据的完整性,当需要同时执行多条sql语句时, 其中的任意一条执行出错,就可能导致所有数据出错,
    #   所以,当其他中的某一条sql出错时,则回滚到执行这些sql语句之前的状态
    connect.rollback()

3、数据删除

# 增 删 改 需要用到回滚 --------查
try:
    # 删除sql
    sqlDel  = "delete from food where id = '1'"
    cursor.execute(sqlDel)

    connect.commit()

except:
    connect.rollback()

4、数据修改

# 增 删 改 需要用到回滚 --------查
try:
    # 删除sql
    sqlDel  = "update food set name='传奇冒菜' where id = 2;"
    cursor.execute(sqlDel)

    connect.commit()

except:
    connect.rollback()

5、数据查询

selectSql = "select * from food"
cursor.execute(selectSql)

关闭连接

cursor.close()
connect.close()

猜你喜欢

转载自blog.csdn.net/wujialaoer/article/details/83013499