美文网首页
MySQL基础

MySQL基础

作者: Koelre | 来源:发表于2017-07-22 12:10 被阅读16次

    看了很多MySQL的基础,现在把自己的学习笔记记录下来,方便自己以后的复习和巩固,除了官网和chm,还有一些博文,感觉也写的挺好的 侵删 21分钟 MySQL 入门教程MySQL的初次见面礼基础实战篇

    什么是数据库?
    数据库 是按照数据结构来组织、存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据。
    RDBMS即关系数据库管理系统 的特点:
    1.数据以表格的形式出现
    2.每行为各种记录名称
    3.每列记录名称所对应的数据域
    4.许多的行和列组成一张表单
    5.若干的表单组成database

    一、环境准备

    开始基础学习,MySQL肯定得要安装好了,除了用workbench(MySQL Workbench官方介绍),也可以用Navicat for MySQL(http://www.cr173.com/ 也可以自己去下载破解版) 操作数据库

    image.png

    二、MySQL中的数据类型

    《MySQL数据类型》

    三、数据库与表的创建以及SQL增删改查

    1.mysql服务的启动、停止和卸载
        启动—— net start mysql
        停止—— net stop mysql
        卸载—— sc delete mysql


    2.登录到mysql数据库
        
    语法:
        mysql -h 主机名 -u 用户名 -p
        (输入密码-----)
        eg:
        mysql -u root -p
        回车确认,如果安装正确且mysql正常运行,会提示输入密码,再正确输入即可


    3.创建、删除、查看数据库
        
    语法:
         create database 数据库名 character set gbk;创建数据库(utf-8,gbk都可以)
        drop database 数据库名 删除数据库
        show databases 数据库名 查询所有数据库
        eg:
        create database samp_db character set utf-8;
        为了便于在命令提示符下显示中文,在创建时通过character set gbk 将数据库字符编码指定为gbk


    4.选择所要操作的数据库
        
    语法:
         use 数据库名;
        eg:
        use samp_db


    5.创建数据库表
        
    语法:
        create table 表名称(列声明)
        eg:
        create table students(
        id int unsigned not null auto_increment primary key,
        name char(8) not null,
        sex char(4) not null,
        age tinyint unsigned not null,
        tel char(13) null default "-");
        user 表:
        CREATE TABLE user (
        id int(11) NOT NULL AUTO_INCREMENT,
        username varchar(32) NOT NULL COMMENT 用户名称,
         birthday date DEFAULT NULL COMMENT 生日,
        sex char(1) DEFAULT NULL COMMENT 性别,
         address varchar(256) DEFAULT NULL COMMENT 地址,
        PRIMARY KEY (id)
        ) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;
        对于有的较长的语句,在命令提示符下,我们可能容易输错,因此,我们通过文本编辑器保存sql文件,然后通过命令提示符下的文件重定向执行该脚本。
        mysql -D 数据库名称 -u root -p < sql文件所在完整的目录
        eg:
        mysql -D samp_db -u root -p <D:\createtable.sql需要关闭cmd,然后重新打开
        外键,teacher表id,,外键需要先定义,再说明引用来自哪一张表:
        create table student(
        id int not null primary key,
        name varchar(20),
        teacher_id int,
        FOREIGN KEY(teacher_id) REFERENCES teacher(id));


    6.向表中插入数据
        
    语法:
        insert into 表名 [(列名)] values (值...)
        eg:
        insert into students values(NULL,"王刚","男",20,"13458695211")
        insert into students(name,sex,age) values("孙刘慧","女",21);
        INSERT into teacher values(1,'teacher1'),(2,'teacher2'),(3,'teacher3')


    7.查询表中的数据
        
    语法:
        select 列名称 from 表名称 [查询条件];
        eg:
        select name,age from students;
        也可以使用通配符*查询表中所有的内容,但是实际工作中,数据量太大,一般都不建议使用*
        语法:
        select 列名称 from 表名称 where 条件;
        eg:
        select name,age from students where sex="女";
        按特定的条件查询,where 子句不仅仅支持where 列名=值,对一般的比较运算符也是支持的,例如=,>,<,>=,<=,!=以及is [not] null,in,like 等等,还可以对查询条件使用or和and进行组合查询


    8.更新表中的数据
        
    语法:
        update 表名称 set 列名称=新值 where 更新条件;
        eg:
        update students set tel=default where id =5;


    9.删除表中的数据
        
    语法:
        delete from 表名称 where 删除条件;
        truncate table 表名称/*清空表*/
        eg:
        delete from students where id=2;/*删除id为2的行*/
        delete from students where age<20;/*删除所有年龄小于21岁的数据*/
        delete from student;/*删除表中的所有数据*/
        truncate table student/*清空表中的所有数据*/


    10.添加列
        
    语法:
        alter table 表名 add 列名 列数据类型 [after 插入位置];
        eg:
        /*在表的最后追加列:*/ alter table students add address char(60);
        /*在age列后插入列birthday:*/ alter table students add birthday date after age;


    11.修改列
        
    语法:
        alter table 表名 change 列名称 列新名称 新数据类型;
        eg:
        /*将表tel列改名为telphone:*/
         alter table students change tel telphone char(13) default "-";
        /*将name列的数据类型改为char(16): */
        alter table students change name char(16) not null;


    12.删除列
        
    语法:
        alter table 表名 drop 列名称;
        eg:
        /*删除birthday列:*/ alter table students drop birthday;


    13.重命名表
        
    语法:
        alter table 表名 rename 新表名;
        eg:
        alter table students rename workmates;


    14.删除整张表
        
    语法:
        drop table 表名;
        eg:
        drop table workmates;


    15.查看表结构
        
    语法:
        desc 表名;
        eg:
        desc user


    16.连接查询
        
    *内连接*语法:
        select 列名1... from 表1 inner join 表2 on 表1.外键=表2.外键 [where / order by 语句]
        eg:
        select tm.name,tm.price,(tm.price*od.tiems_num) as price from orderdetail as od inner join items as tm on od.item_id=tm.id;

    **左外连接*:
        -- 无论右边是否有数据对应左边的表,左边的表都将显示左表查询该列的所有信息
        语法:
        select 列名1... from 表1 left [outer] join 表2 on 表1.外键 = 表2.外键 [where / order by 语句等]
        eg:
        select u.username,o.number,o.createtime from user as u left join orders as o on u.id=o.user_id;

    **右外连接*:
        -- 和左外连接恰恰相反,无论左表是否与右边的表与之对应,右边的表都将显示右表查询该列的所有信息,下面的这个例子就是 与左连接的查询结果一样。
        语法:
        select 列名1... from 表1 right [outer] join 表2 on 表1.外键=表2.外键 [where / order by 语句等]
        eg:
        select u.username,o.number,o.createtime form orders as o right join user as u on u.id=o.userid;


    *17.查询eg
        select count(*) from student -- 查询student表中有多少数据
        SELECT COUNT(*) from test where age=21-- 查询test表中age为21的数据一共有多少条
        select count(*) from test where age>21;-- 查询test表中age大于21的数据一共有多少条,数据库中的int类型支持大于小于等比较符
        select count(*) from test where age>21 and age<30;-- 查询test表中age大于21并且小于30的数据一共有多少条
        select * from test order by id desc;-- 查询test表按id降序排序


    18.复制表
        
    语法:
        create table 新表名 select * from 旧表名;
        eg:
        create table new_user select * from
        user;select * from new_user


    19.复制表结构
        
    语法:
        create table 新表名 like 旧表名;
        eg:
        create table new_u_d like user;
        select * from new_u_d;
        desc new_u_d;


    20.复制表数据
        
    语法:
        insert into 表名 select * from 带数据的表;
        eg:
        insert into new_u_d select * from user;
        select * from new_u_d

    create database 数据库名 character set bgk
    *在创建时character set gbk 将数据库字符编码指定为gbk
    *可以用 show database 查看已经创建了哪些数据库

    选择所要操作
    在登录数据库时候 MySQL -D 所选择的数据库名 -h 主机名 -u 用户名 -p

    create table 表名(列声明);
    *eg:
    create table students(
    id int unsigned not null auto_increment primary key,
    name char(8) not null,
    sex cahr(4) not null,
    age tinyint unsigned not null,
    tel cahr(13) null default "-"
    )

    外键.png 指定执行sql文件01.png

    可视化工具:
    Navicat 在线手册
    mysq数据库管理工具navicat基本使用方法
    Navicat For Mysql快捷键

    相关文章

      网友评论

          本文标题:MySQL基础

          本文链接:https://www.haomeiwen.com/subject/rpyfcxtx.html