CMD操作数据库

Dos窗口下使用Mysql

一.安装配置mysql

  1. 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-win32.zip
  2. 下载完成后解压自己指定的目录如 E:mysql
  3. 配置环境变量,在path后加入 ;E:mysqlin
  4. 在刚刚解压的根目录(即d:mysql目录下)下,新建一个文本文件my.ini,将如下内容放入my.ini(格式为ANSI文本)文件中:

    [mysql]

    # 设置mysql客户端默认字符集

    default-character-set=utf8

    [mysqld]

    #设置3306端口

    port = 3306

    # 设置mysql的安装目录

    basedir=E:mysql 

    # 这里地址记得加引号,不然不能正常启动。

    # 设置mysql数据库的数据的存放目录

    datadir=E:mysqldata

    # 允许最大连接数

    max_connections=200

    # 服务端使用的字符集默认为8比特编码的latin1字符集

    character-set-server=utf8

    # 创建新表时将使用的默认存储引擎

    default-storage-engine=INNODB

    如下图:

   5.清除密码:mysqld --initialize-insecure

 二.启动mysql,设置root密码

  1. 安装MySQL服务并启动

     

  2.  继续输入“mysql -uroot -p”,回车。然后会提示“Enter password:”,不要输入,此时不需要密码,再按一次回车跳过 

  3. 设置root用户密码:

     ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';

     

     

   4.退出当前登录状态,重新登录:q或exit

    

   5.修改root密码:mysql>set password for root@localhost ='11111111' 

    

 三、创建数据库

   1.创建学生库 :create database student

   2.创建学生表:

    create table students

    (

    stu_name varchar(30) not null,

    stu_id char(4) ,

    stu_specialty varchar(30) not null,

    stu_sex char(2) check( stu_sex in ('','')),

    stu_age int (3),

    primary key (stu_id)

    );

    (1)修改表的名称可以使用:

      alter table students rename to student;

    (2)添加表的列名:

      alter table student add column classs varchar(10);

    (3)修改表的列名:

      alter table student change column classs class varchar(10);

    (4)删除表的列表:

      alter table student drop column class;

   3.创建课程表

    create table studentclass

    (

    stu_classid varchar(30) not null,

    stu_classname varchar(30) not null,

    primary key (stu_classid)

    );

   4.创建成绩表

    create table studentcore

    (

    stu_id char(4) ,

    stu_classid varchar(30) not null,

    stu_score int(3) not null

    );

   5.创建完成之后,查看表内无数据:select *from student,可以向表内插入数据

    (1)插入学生表数据,插入多条数据:

      insert into student values ('王二','01','计算机专业','','10'), ('张四','02','计算机专业','','11'),('王五','03','计算机专业','','12');      

    (2)插入课程表数据,每次只插入一条数据

      insert into studentclass values ('001','java');

      insert into studentclass values ('002','c');

      insert into studentclass values ('003','c++');

    (3)插入成绩表数据

      insert into studentcore values('01','001','60'),('01','002','65'),('01','003','70');

      insert into studentcore values('02','002','70');

      insert into studentcore values('03','003','80');

      

    

  6.创建完成后,可以通过select语句查询自己想要的信息,比如查询学号为01的平均分,最高分,最低分:

    select avg(stu_score) 平均分,max(stu_score) 最高分,min(stu_score) 最低分

    from studentcore

    where stu_id='01'

   7.也可以通过创建视图,来查看自己想要的信息,使用视图的优点:    

    (1)提高了重用性,就像一个函数

    (2)对数据库重构,却不影响程序的运行

    (3)提高了安全性能。可以对不同的用户设定不同的视图

    (4)让数据更加清晰,想要什么样的数据,就创建什么样的视图

   8.创建视图的例子:  

    (1)单表创建视图:

      CREATE VIEW stu_id01  AS select avg(stu_score) 平均分,max(stu_score) 最高分,min(stu_score) 最低分from studentcore where stu_id='01'; 

      select * from stu_id01r;

       

    (2)多表创建视图:           

       比如:查询成绩单为70分的学生的姓名和课程的名称

       1)首先通过select语句写出自己想要查询的数据:

          select stu_name 学生姓名,stu_classname 课程名称 from student,studentclass,studentcore where student.stu_id=studentcore.stu_id and                                                       studentclass.stu_classid=studentcore.stu_classid and stu_score = 70;

      (2)创建视图:

          CREATE VIEW score_70 AS select stu_name 学生姓名,stu_classname 课程名称 from student,studentclass,studentcore where           student.stu_id=studentcore.stu_id and studentclass.stu_classid=studentcore.stu_classid and stu_score = 70;

       (3)查看视图:

          select stu_name from score_70;

          

       (4)当数据发生变化时,如果想查询自己想要的内容,不用重复去写select查询语句,直接查看视图即可。

          insert into studentcore values('03','001','70');

          select * from score_70;

          

     9.也可以批量生成数据,准备好数据,然后用source命令:source C:UserswzfDesktop1.sql

四.连接测试环境的数据库

     1. Ping 地址

   2.ping通之后,可以连接

    

原文地址:https://www.cnblogs.com/tengyun99/p/12496620.html