您的位置:首页 > 数据库 > MySQL

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 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: