mysql相关语法和通过pycharm进行数据库管理

1、基础知识

大小写不敏感
以分号结束一句sql语句

单行注释:
1.-- (后面有一个空格)
2.#
多行注释:
/*  */

2、基础语法

查看数据库:
show databases;


使用数据库:
use 数据库名字;


创建数据库:
create database 数据库名 charset UTF8;


删除数据库:
drop 数据库名


查看当前使用的数据库:
select database();


查看有哪些表:
use world;
show tables;


创建表:
use world;
create table student(
id int,
name varchar(30),
age int
);


删除表:
drop table student;

数据插入
use world;
create table student(
id int,
name vachar(10),
age int
);
insert into student(id) values(1),(2),(3);
insert into student(id,name,age) values(4,'肖',19),(5,'钧',20),(6,'文',21);


条件删除(不带条件默认为全部删除)
delete from student where id>5;

更新内容(不带条件默认为全部更新)
update student set age=98 where age=11;


数据查询
select Name,id from city;
*代表全部的列
select * from city where CountryCode ='CHN';

计算语法:
select 聚合函数 from 表 [where 条件] group by 列
select District, sum(population) from city where CountryCode='chn' group by District;
sum求和,avg求平均值,min,max,count(列)求数量

排序:
select * from city where Population >1000000 and CountryCode ='chn' order by Population  asc;
desc降序,asc升序(默认)

结果分页限制:
select * from student limit 5;
#显示五条
select *from student limet 10,5;
#跳过前十条,从第十一条开始显示五条。

3、通过pycharm管理数据库

from pymysql import Connection
conn=Connection(
    host='localhost',
    port=3306,
    user='root',
    password='123456'
)
# mysql数据库服务器的版本
# print(conn.get_server_info())
cursor=conn.cursor()
conn.select_db('test') #选择数据库
cursor.execute("create table test_pymysql(id int);")
conn.close()
from pymysql import Connection
conn=Connection(
    host='localhost',
    port=3306,
    user='root',
    password='123456'
)
# mysql数据库服务器的版本
# print(conn.get_server_info())
cursor=conn.cursor()
conn.select_db('world') #选择数据库
cursor.execute("select * from city where countrycode='chn';")
result=cursor.fetchall()
for r in result:
    print(r)
conn.close()
from pymysql import Connection
conn=Connection(
    host='localhost',
    port=3306,
    user='root',
    password='123456',
    autocommit=True
)
# mysql数据库服务器的版本
# print(conn.get_server_info())
cursor=conn.cursor()
conn.select_db('test') #选择数据库
# cursor.execute("select * from city where countrycode='chn';")
# result=cursor.fetchall()
# for r in result:
#     print(r)
cursor.execute('insert into test_pymysql values(3),(2);')
conn.commit()
# 确认后修改才生效,或者用autocommit=True
conn.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coleak

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值