Mysql(一) 基本操作

一、介绍

   1、数据库

     数据库,通俗的讲,即为存储数据的“仓库”。不过,数据库不仅只是存储,还对所存储的数据做相应的管理,例如,访问权限,安全性,并发操作,数据的备份与恢复,日志等。实际
上,我们所提及的数据库,就是数据库管理系统。( DBMS,Database Management System

    2、数据表

      数据表是存储数据的基本单元。数据表是二维的,由多行与多列组成,我们称每一列为一个字段,称每一行为一条记录。
      数据在数据表中就是以多行多列的形式来存储的。
      数据表在表现形式上类似于excel的一张sheet,大家可以通过excelsheet来辅助理解。 

    3、sql

       SQL,结构化查询语言( Structured Query Language),是一种查询与操作关系型数据库的语言。使用SQL语言可以完成在数据库中查询相关信息,更新信息,权限变更,修改数据库结
构等操作。
       说明: SQL语言是大小写不敏感的。(不区分大小写)

   4、安装MySQL绿色版 5.7.x

        默认不在含有data数据库文件夹,我们需要进行初始化操作。
        初始化命令为:
        mysqld –-initialize-insecure
        mysqld --initialize

   5、配置my.ini文件

      解压文件后,我们需要修改my.ini文件内容,如下:
      mysql服务器安装目录
              basedir=E:/mysql-5.6.19-win32
     #mysql数据存储目录
              datadir=E:/mysql-5.6.19-win32/data
其中E:/mysql-5.6.19-win32MySQL数据库的根目录,根据实际情况进行修改。
说明:如果没有my.ini文件,则一切取默认配置。

     6、数据库服务端字符集

MySQL在默认情况下,使用LatinISO8859-1)字符集,该字符集不支持中文等字符,我们可以将其修改为更通用的UTF8字符集。
      修改my.ini文件,在[mysqld]中加入:
       character-set-server=utf8
     7、启动与停止MySQL服务

我们要访问MySQL数据库,首先要启动MySQL服务。
    启动服务    start mysqld
    停止服务    mysqladmin –u用户名 -p shutdown
输入密码后,停止服务。
     8、登录与退出MySQL数据库

     登录  mysql –u用户名 –p
     登出 quit / exit
说明: MySQL默认存在一个用户名为root,密码为空的管理员用户。 
二、简单操作

查看所有数据库
show databases
使用某数据库
use 数据库名;
查看指定数据库下的所有表
show tables
查看指定表的表结构
desc / describe 表名;
说明: MySQL以“;”作为一条命令的结束。

创建数据库
create database 数据库名
删除数据库
drop database 数据库名
说明:使用show查看数据库或表时,使用的是英文单词为复数,而使用create创建数据库或表时,使用的英文单词为单数。 
三、

    1、注释

MySQL中可以使用以下三种注释:
       # 从“ #”开始的位置一直到该行的结束。
       -- 从“ --” 开始的位置一直到该行的结束,使用这种注释时需要在“ --”与注释内容之间至少存在一个空格。
       /* */从“ /*”开始,到“ */”之间的内容。

   2、数据类型

MySQL中数据类型分为以下几种:数值类型、字符类型、日期与时间类型

    (1)数值类型

tinyint1个字节)      带符号的范围是-128127。无符号的范围是0255
smallint2个字节)     带符号的范围是-3276832767。无符号的范围是065535
mediumint3个字节)  带符号的范围是-83886088388607。无符号的范围是0到 16777215。 
int / integer4个字节)  带符号的范围是-21474836482147483647。无符号的范围是0 到4294967295
bigint8个字节)   带符号的范围是-9223372036854775808到 9223372036854775807。无符号的范围是018446744073709551615。 
说明: 默认为有符号类型,无符号类型则在相应类型后面加上 unsigned。 
float[(M,D)] 单精度浮点类型。
double [(M,D)] 双精度浮点类型
M是浮点类型总位数, D是小数点后面的位数,如果MD被省略,根据硬件允许的限制来保存值。 float大约7位小数位,double大约15位小数位。

      (2)日期与时间

date日期类型,支持的范围为'1000-01-01''9999-12-31'
time时间类型,支持的范围是'-838:59:59''838:59:59'
datetime日期时间类型,支持的范围是'1000-01-01 00:00:00'到 '9999-12-31 23:59:59‘
timestamp时间戳,范围是'1970-01-01 00:00:00'2037年。
year[(2|4)],两位或四位格式的年。默认是四位格式。

     (3)字符串类型

char(M)固定长度字符串,当实际长度不足M时,在右侧填充空格以达到指定的长度。 M表示列长度。 M的范围是0255 个字符。
varchar(M)可变长字符串。当实际长度不足M时,不进行填 充。 M表示最大长度。 M的范围是065,535
           此外,字符串类型还有binaryvarbinarytinyblobtinytext,blob, text, mediumblob, mediumtext, longblob, longtext,enum, set
四、表的操作

      1、创建表

create table student2 (
    name varchar(30) comment '名字',
    age int,
    height float(5, 2) default 10.2,
    weight float(5, 2)
)

增加字段(add后面小括号可以省略)
alter table student2 add temp int;

同时增加多个字段(add后面的小括号不能省略)
alter table student2 add (
temp2 int,
temp3 char(5)
)

查看表结构: desc student2

类型说明包括如下定义:
[not null | null] 不允许为空 | 允许为空
[default default_value] 默认值
[auto_increment] 自动增长
[unique [key] | [primary] key] 唯一 | 主键
[comment ‘string’] 字段注释

  2、修改字段(modify不能改变字段的名字)

alter table student2 modify temp3 int;

修改字段(change可以改变字段的名字)
alter table student2 change temp3 temp4 varchar(20);

 3、删除字段

alter table student2 drop temp4;

  4、修改表名

alter table student2 rename to student;

  5、删除表

drop table student;

6、   

create table student(
	id INT primary key,	#主键,
    name varchar(10),
	age int
);

#查询表中的数据
select id, name from student;
#查询所有列
select * from student;

#有条件的查询,where指定查询条件,条件表达式为真的记录
#才会在结果集中显示。
select * from student where age > 12
#not 对条件表达式取反。true->false, false->true
select * from student where not age > 12

#and 并且
select * from student where age > 12 and age < 18
#or 或者
select * from student where age < 12 or age > 18

#in 是否在指定的集合中(跟集合中的任意一个值相等)
select * from student where age in (1, 10, 20, 30, 50);

#between and 区间(闭)
select * from student where age between 12 and 18;
select * from student where age >= 12 and age <= 18;

#MySQL中,如果没有给一个字段指定具体的值,并且该字段
#没有使用default指定默认值,则字段的默认值为null。(default null)

#错误的写法
#select * from student where age = null;
#select * from student where age <> null;

  

#MySQL中,null是一个特殊值,其不等于任何值,包括其自身。
#正确的写法
select * from student where age is null;
select * from student where age <=> null; #能够比较null值的等于号
select * from student where age is not null;
select * from student where not age <=> null;

#使用distinct可以去掉结果集中重复的记录。当查询多个字段时,
#只有多个字段全部相同,才认为是重复的记录。部分字段相同,不
#认为是重复的。

select distinct name, age from student;

#limit限制结果集中的记录
#取结果集中前count条记录。
#limit count
#从结果集中start位置开始,获取count条记录。(start从0开始)
#limit start, count
#第一种方式也可以使用第二种方式来表示。limit n 等价于 limit 0, n
select * from student limit 3;
select * from student limit 2, 3;

#模糊查询
#两个通配符 % _
#%匹配0个或任意多个字符
#_匹配任意一个字符
select * from student where name like '%三%';
select * from student where name like '张%';
select * from student where name like '%三';
select * from student where name like '张_'

#含有某关键字X like '%X%'
#以关键字X开头 like 'X%'
#以关键字X结尾 like '%X'
#如果关键字含有通配符,则需要对通配符进行转义。
#默认的转义字符是。
select * from student where name like '%\_%';
#我们也可以自己来指定转义字符,使用escape。
select * from student where name like '%$_%' eacape '$';

#向表中插入数据
#insert into 表名(字段列表) values (值列表)
insert into student(id) values (20);
insert into student(id, name, age) values (21, 'xxx', 30);
#如果要向表中所有的字段插入值,可以省略字段列表。
#此时,按照字段在表声明(创建)时指定的顺序与值列表中
#的值进行对应。
#这种写法不推荐。
insert into student values (22, 'kcs', 33);

#修改数据
update student set name='修改', age=30 where id=1
#删除数据
delete from student where id=1;
#table 可以省略 truncate student;
truncate table student;

/*
delete与truncate
二者都可以删除表中的记录。
delete可以指定where条件,进行有选择的删除。
truncate就是删除表中所有的记录,因此,truncate
不能指定where条件。
当二者都删除表中所有记录时,truncate在性能上要优于
delete from。实际上,truncate就是删除表结构,然后
再重新创建表结构。
*/

#别名 使用as
select student.id as '学号' , student.name as '姓名' from student
#别名也可以省略as
select id '学号', name '姓名' from student
#也可以去掉单引号
select id 学号, name 姓名 from student

#表也可以取别名
select a.id, a.name from student as a
select a.id, a.name from student a

select id from (
select id, name from student
) x


 








 

原文地址:https://www.cnblogs.com/liuwei6/p/6734859.html