MySQL基本语法
2018-04-19 19:34
225 查看
一、基本命令:
1、启动服务
以管理员身份运行cmd
格式:net start 服务名
格式:net start mysql57
2、停止服务
说明:管理员身份运行cmd
格式:net stop 服务名
格式:net stop mysql57
3、链接数据库
格式:mysql -u 用户名 -p
格式:mysql -u root -p
输入密码:tyz950829
4、退出登录
quit或者exit
5、远程链接
格式:mysql -h ip地址 -u 用户名 -p
输入密码
二、数据库操作
1、创建数据库
格式:create database 数据库名 charest=utf8
格式:create database poseidon charset=utf8
2、删除数据库
格式:drop database 数据库名
3、切换数据库
格式:use 数据库名
4、查看当前选择的数据库
select database();
三、表操作
1、查看当前数据库中所有表
show tables;
2、创建表
create table 表名(列及类型)
create table student(id int autu_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, idDelete bit default 0)
说明:autu_increment自增长
primary key主键
not null不为空
3、删除表
drop table 表名
4、查看表结构
格式:desc 表名
5、重命名表
格式:rename table 原表名 to 新表名
6、修改表
格式:alter table 表名 add/change/drop
四、数据操作
1、增
全列插入
格式:insert into 表名 values()
insert into student values(0, "tom", 19, 1, "beijing", 0);
缺省插入
格式:insert into 表名(列1, 列2, ... ...);
同时插入多条数据
格式:insert into 表名(...), (...), ... ...;
2、删
格式:delete from 表名 where 条件;
delete from student where id=4;
注意:没有条件是全部删除
3、改
格式:update 表名 set 列1=值1, 列2=值2, ...where 条件;
4、查
格式:select * from 表名;
五、查
1、基本语法
格式:select * from 表名
select name, age from student
2、消除重复行
在select后面,列前面使用distinct可以消除重复的行
3、条件查询
语法
select * from 表名 where 条件
比较运算符(where后条件里的内容)
等于=, 大于>, 小于<, 大于等于>=, 小于等于<=, 不等于!=
逻辑运算符
and并且, or或者, not非
模糊查询
like
格式:select * from 表名 where name like "田%"
查询“田xx”的名字
范围查询
in 在一个非连续的范围内
between ... and ... 在一个连续的范围内
where id in(8, 10, 12);
where id between 6 and 8
空判断
is null 判断空
is not null 判断非空
4、聚合
count(*) 表示计算总行数
max(列) 最大值
min(列) 最小值
sum(列) 和
avg(列) 求平均值
格式:select count(*) from student;
5、分组
格式:select 列1, 列2, 聚合...from 表名 group by gender;
6、排序
格式:select * from 表名 order by 列1 asc/desc, 列2, asc/desc 说明:asc降序, desc升序
7、分页
格式:select * from 表名 limit start, count;
格式:select * from start limit 0, 3;
六、关联
建表语句:
1、create table student(id int auto_increment primary key, name varchar(20) not null, stuNum int not null);
2、create table class(id int auto_increment primary key, name varchar(20) not null, foreign key(classid) references class(id));
## python中实现MySQL
import pymysql
# 链接数据库
# 参数1:mysql服务所在主机IP
# 参数2:用户名
# 参数3:密码
# 参数4:要链接数据库名
db = pymysql.connect("localhost", "root", "tyz950829", "poseidon")
# 创建cursor对象
cursor = db.cursor()
# 执行语句,查看当前版本号
sql = "select version()"
# 在sql中输入MySQL语句进行操作
# 输出信息
cursor.execute(sql)
data = cursor.fetchone()
print(data)
# 断开
cursor.close()
db.close()
## python下使用数据库
import pymysql
class poseidonSQL():
def __init__(self, host, user, passwd, bdName):
self.host = host
self.user = user
self.passwd = passwd
self.bdName = bdName
def connet(self):
self.db = pymysql.connet(self.host, self.user, self.passwd, self.bdName)
self.cursor = self.db.cursor()
def close(self):
self.cursor.close()
self.db.close()
def get_one(self, sql):
res = None
try:
self.connet()
self.cursor.execute(sql)
res = self.cursor.fetchone()
self.close()
except:
print("查询失败")
return res 阅读更多
1、启动服务
以管理员身份运行cmd
格式:net start 服务名
格式:net start mysql57
2、停止服务
说明:管理员身份运行cmd
格式:net stop 服务名
格式:net stop mysql57
3、链接数据库
格式:mysql -u 用户名 -p
格式:mysql -u root -p
输入密码:tyz950829
4、退出登录
quit或者exit
5、远程链接
格式:mysql -h ip地址 -u 用户名 -p
输入密码
二、数据库操作
1、创建数据库
格式:create database 数据库名 charest=utf8
格式:create database poseidon charset=utf8
2、删除数据库
格式:drop database 数据库名
3、切换数据库
格式:use 数据库名
4、查看当前选择的数据库
select database();
三、表操作
1、查看当前数据库中所有表
show tables;
2、创建表
create table 表名(列及类型)
create table student(id int autu_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, idDelete bit default 0)
说明:autu_increment自增长
primary key主键
not null不为空
3、删除表
drop table 表名
4、查看表结构
格式:desc 表名
5、重命名表
格式:rename table 原表名 to 新表名
6、修改表
格式:alter table 表名 add/change/drop
四、数据操作
1、增
全列插入
格式:insert into 表名 values()
insert into student values(0, "tom", 19, 1, "beijing", 0);
缺省插入
格式:insert into 表名(列1, 列2, ... ...);
同时插入多条数据
格式:insert into 表名(...), (...), ... ...;
2、删
格式:delete from 表名 where 条件;
delete from student where id=4;
注意:没有条件是全部删除
3、改
格式:update 表名 set 列1=值1, 列2=值2, ...where 条件;
4、查
格式:select * from 表名;
五、查
1、基本语法
格式:select * from 表名
select name, age from student
2、消除重复行
在select后面,列前面使用distinct可以消除重复的行
3、条件查询
语法
select * from 表名 where 条件
比较运算符(where后条件里的内容)
等于=, 大于>, 小于<, 大于等于>=, 小于等于<=, 不等于!=
逻辑运算符
and并且, or或者, not非
模糊查询
like
格式:select * from 表名 where name like "田%"
查询“田xx”的名字
范围查询
in 在一个非连续的范围内
between ... and ... 在一个连续的范围内
where id in(8, 10, 12);
where id between 6 and 8
空判断
is null 判断空
is not null 判断非空
4、聚合
count(*) 表示计算总行数
max(列) 最大值
min(列) 最小值
sum(列) 和
avg(列) 求平均值
格式:select count(*) from student;
5、分组
格式:select 列1, 列2, 聚合...from 表名 group by gender;
6、排序
格式:select * from 表名 order by 列1 asc/desc, 列2, asc/desc 说明:asc降序, desc升序
7、分页
格式:select * from 表名 limit start, count;
格式:select * from start limit 0, 3;
六、关联
建表语句:
1、create table student(id int auto_increment primary key, name varchar(20) not null, stuNum int not null);
2、create table class(id int auto_increment primary key, name varchar(20) not null, foreign key(classid) references class(id));
## python中实现MySQL
import pymysql
# 链接数据库
# 参数1:mysql服务所在主机IP
# 参数2:用户名
# 参数3:密码
# 参数4:要链接数据库名
db = pymysql.connect("localhost", "root", "tyz950829", "poseidon")
# 创建cursor对象
cursor = db.cursor()
# 执行语句,查看当前版本号
sql = "select version()"
# 在sql中输入MySQL语句进行操作
# 输出信息
cursor.execute(sql)
data = cursor.fetchone()
print(data)
# 断开
cursor.close()
db.close()
## python下使用数据库
import pymysql
class poseidonSQL():
def __init__(self, host, user, passwd, bdName):
self.host = host
self.user = user
self.passwd = passwd
self.bdName = bdName
def connet(self):
self.db = pymysql.connet(self.host, self.user, self.passwd, self.bdName)
self.cursor = self.db.cursor()
def close(self):
self.cursor.close()
self.db.close()
def get_one(self, sql):
res = None
try:
self.connet()
self.cursor.execute(sql)
res = self.cursor.fetchone()
self.close()
except:
print("查询失败")
return res 阅读更多
相关文章推荐
- MySQL基本语法
- mysql基本语法
- MYSQL的学习笔记------MYSQL的基本语法
- SQL分类”及“Mysql基本语法
- 最新sql基本语法大全(mysql)
- Mysql基本语法
- 讲述MySQL基本语法
- PHP+MYSQL+AJAX - 第四章 认识PHP基本语法
- MySQL基本语法
- MySQL与Oracle 差异比较之二基本语法
- mysql基本语法
- mysql 基本语法
- MySQL 安装 启动 基本语法概述
- MySQL的一些基本语法(7)-查询数据
- mysql优化的一些基本语法
- [转]MySQL 最基本的SQL语法/语句
- MYSQL基本语法命令
- PHP与MySQL连接及SQL语言基本语法
- mysql存储过程 基本语法
- Mysql 存储过程基本语法