2-14-1 MySQL基础语句,查询语句

摘要:
1、 SQL概述结构化查询语言(简称SQL)1.它是一种特殊用途的编程语言2.它也是一种数据库查询和编程语言(用于访问数据、查询、更新和管理关系数据库系统;它也是数据库脚本文件的扩展)。从上面,我们可以看到大约有两个工作与我们的数据库有关:DBD和DBASQL是由美国国家标准局(ANSI)于1986年10月创建的。采用的数据库语言是美国标准,其次是国际标准化组织(is

一. SQL概述

结构化查询语言(Structured Query Language)简称SQL

1. 它是一种特殊目的的编程语言

2. 它还是一种数据库查询和程序设计语言

(用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名)

从上可以看出我们数据库相关工作职位大概两种:DBD和DBA

SQL 是1986年10 月由美国国家标准局(ANSI)通过的数据库语言美国标准,接着,国际标准化组织(ISO)颁布了SQL正式国际标准.1989年4月,ISO提出了具有完整性特征的SQL89标准,1992年11月又公布了SQL92标准,在此标准中,把数据库分为三个级别:基本集、标准集和完全集.

二. SQL语句结构

结构化查询语言包含6个部分:

1. 数据查询语言(DQL:Data Query Language):

其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出.

保留字SELECT是DQL(也是所有SQL)用得最多的动词,

其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING.

这些DQL保留字常与其他类型的SQL语句一起使用.

2. 数据操作语言(DML:Data Manipulation Language)0:

其语句包括动词INSERT,UPDATE和DELETE.

它们分别用于添加(insert),修改(update)和删除表(delete)中的行.

也称为动作查询语言.

3. 事务处理语言(TPL):

它的语句能确保被DML语句影响的表的所有行及时得以更新.

TPL语句包括BEGIN TRANSACTION,COMMIT和ROLLBACK.

4. 数据控制语言(DCL):

它的语句通过GRANT或REVOKE获得许可,确定单个用户和用户组对数据库对象的访问.

某些RDBMS可用GRANT或REVOKE控制对表单个列的访问.

5. 数据定义语言(DDL):

其语句包括动词CREATE和DROP.

在数据库中创建新表或删除表(CREAT TABLE 或 DROP TABLE);

为表加入索引等.

DDL包括许多从数据库目录中获得数据有关的保留字.它也是动作查询的一部分.

6. 指针控制语言(CCL):

它的语句,像DECLARE CURSOR,FETCH INTO和UPDATE WHERE CURRENT用于对一个或多个表单独行的操作.

三. MySQL语句

For other Help :

http://dev.mysql.com/doc/refman/5.5/en/

关于数据库的操作

#########################

3.1 查看数据库:

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

注:

1. information_schema这数据库保存了MySQL服务器所有数据库的信息.

如数据库名,数据库的表,表栏的数据类型,访问权限等.

2. performance_schema 这是MySQL5.5新增的一个性能优化的引擎

命名PERFORMANCE_SCHEMA

主要用于收集数据库服务器性能参数.

MySQL用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表

详细介绍可参考如下URL:

http://www.cnblogs.com/hzhida/archive/2012/08/08/2628833.html

3. 元数据是关于数据信息的数据.

如数据库名或表名,列的数据类型,或访问权限等.

4. mysql库是系统库,里面保存有账户信息,权限信息等.

mysql> show databases G#以行的方式显示

*************************** 1. row ***************************

Database: information_schema

*************************** 2. row ***************************

Database: mysql

*************************** 3. row ***************************

Database: performance_schema

[root@xiaogan64 ~]#mysql -e 'show databases' -uroot -p123456

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

mysql –e  后面我们接SQL语句,直接终端运行.

后面写sql 相关shell可以用到.

[root@xiaogan64 ~]# mysqlshow -uroot -p123456

+--------------------+

|     Databases      |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

#########################

3.2 创建数据库:

Usage:

create database 数据库名;

创建数据库注意事项:

*1. 在文件系统中,MySQL的数据存储区将以目录方式表示MySQL数据库.

因此,上面命令中的数据库名字必须与操作系统的约束的目录名字一致.

例如: 不允许文件和目录名中有\,/,:,*,?,”,<,>,|这些符号,

在MySQL数据库名字中这些字母会被自动删除.<遵从目录的约束>

*2. 数据库的名字不能超过64个字符

*3. 包含特殊字符的名字或者是全部由数字或保留字组成的名字必须用反斜杠``包起来.

*4. 数据库不能重名.

mysql> create database HA; #创建一个名为HA的数据库

mysql> create database `HA-test`;

[root@xiaogan64 ~]# ls /usr/local/mysql/data/  #查看数据库存放目录

#########################

3.3 选择要操作的数据库:

使用USE语句将会选择一个数据库成为当前数据库.后面的操作默认都在被选择的数据库中操作.

mysql> use HA-test;

Database changed

查看自己所处的位置及默认所在的位置

mysql> select database();

+------------+

| database() |

+------------+

| HA-test    |

+------------+

1 row in set (0.00 sec)

默认

mysql> select database();

+------------+

| database() |

+------------+

| NULL       |

+------------+

1 row in set (0.00 sec)

Null意味着没有选择数据库

Null在数据库中表示 不知道的数据,主要有3种意思:

1) 知道数据存在,但不知道具体值.

2) 不知道数据是否存在.

3) 数据不存在.

在命令行选择默认的数据库

mysql -uroot -p123456 HA

mysql> select now(),user(),database();

+---------------------+------------------+------------+

| now()               | user()           | database() |

+---------------------+------------------+------------+

| 2015-10-06 10:52:48 | root@localhost | HA         |

+---------------------+------------------+------------+

now() --> 在数据库系统中,指当前时间

user() --> 在数据库系统中,显示当前用户

database() --> 在数据库系统中,显示当前数据库

#########################

删除数据库:

Way 1:

mysql> drop database `HA-test`;

删除没有任何提示,要慎重操作

Way 2: 直接到数据库存放目录移出就行

cd /usr/local/mysql/data/

mv HA@002dtest /tmp

mysql> show databases;

if exists / if not exists

使用IF EXISTS 子句以避免删除不存在的数据库时出现的MySQL错误信息

mysql> drop database if exists `HA-test`;

同理我们创建数据库时也可以使用

mysql> create database if not exists HA;

#########################

关于表的操作:

创建表:

Usage:

create table 表名 (字段名 类型, 字段名 类型, 字段名 类型);

mysql> create table student(id int(20),name char(40),age int);

注: 为了提高sql语句的可读性,可书写如下:

create table student (

id int(20),

name char(40),

age int);

查看表相关信息:

查看表:

要进入到数据库再查看

mysql> use HA;

Database changed

mysql> show tables;

+--------------+

| Tables_in_HA |

+--------------+

| student      |

查看表的结构:

Describe

mysql> desc student;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(20)  | YES  |     | NULL    |       |

| name  | char(40) | YES  |     | NULL    |       |

| age   | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> explain mysql.user;

mysql> show columns from mysql.user;

mysql> show fields from mysql.user;

mysql> show columns from mysql.user like '%user';

会一种常用的就行

查看创建表执行了哪些命令:

mysql> show create table student G

*************************** 1. row ***************************

       Table: student

Create Table: CREATE TABLE `student` (

  `id` int(20) DEFAULT NULL,

  `name` char(40) DEFAULT NULL,

  `age` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8

## ---> 可在创建表时,指定存储引擎和字符集

mysql> create table student2(

id int(20),

name char(40),

age int)ENGINE=MyISAM DEFAULT CHARSET=utf8;

# 可以指定表的默认存储引擎和字符集

这两个是默认存储引擎和默认字符集

删除表:

mysql> drop table student2;

禁止预读表信息:

没有禁止前的提示

mysql> use performance_schema;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

登录的时候加上-A参数

mysql -uroot –p123456 -A

修改表名称alter:

Usage:

altertable 表名 rename 新表名;

mysql> alter table student rename students;

# student表名修改为students

mysql> show tables;

+--------------+

| Tables_in_HA |

+--------------+

| students     |

+--------------+

修改表中的字段类型:

Usage:

altertable 表名 modify 要修改的字段名 要修改的类型;

mysql> desc students;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(20)  | YES  |     | NULL    |       |

| name  | char(40) | YES  |     | NULL    |       |

| age   | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

mysql> alter table students modify id int(10);

wps6FD2.tmp

修改表中的字段类型和字段名称:

Usage:

alter table 表名 change 原字段名 新字段名 新字段类型;

查了一下官方文档,发现mysql还真的不支持同时修改多个字段,

MODIFY [COLUMN] col_name column_definition

        [FIRST | AFTER col_name]

来源:http://dev.mysql.com/doc/refman/5.5/en/alter-table.html

mysql> desc students;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(10)  | YES  |     | NULL    |       |

| name  | char(40) | YES  |     | NULL    |       |

| age   | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

mysql> alter table students change name stname char(20);

wps6FD3.tmp

注:CHANGE 和MODIFY的区别:

CHANGE 对列进行重命名和更改列的类型,需给定旧的列名称和新的列名称、当前的类型. MODIFY 可以改变列的类型,此时不需要重命名(不需给定新的列名称)

在表中添加字段:

语法:alter table 表名 add 字段名 字段类型;

mysql> alter table students add sex enum('M','W');

wps6FD4.tmp

指定位置添加字段:

在第一列添加一个字段:

mysql> alter table students add uid int(10) first;

wps6FD5.tmp

在age后面添加一个address字段:

mysql> alter table students add address char(40) after age;

wps6FD6.tmp

删除表中字段:

语法:alter table 表名 drop 字段名 ;

mysql> alter table students drop address;

wps6FE6.tmp

#########################

关于记录的操作:

插入字段<记录>insert:

语法:insert into 表名values (字段值1,字段值2, 字段值3);

mysql> insert into student values(1,'zhangs',21);

插入记录时要对应相对的类型

mysql> insert into student values(2,'lis',24),(3,'wange',26);

同时插入多条,使用,分开

mysql> insert into student (id,name)values(4,'hangl');

指定字段插入

查询表中记录:

语法:select * from 表名称;

mysql> select * from student;  *表示所有

+------+--------+------+

| id   | name   | age  |

+------+--------+------+

|    1 | zhangs |   21 |

|    2 | lis    |   24 |

|    3 | wange  |   26 |

|    4 | hangl  | NULL |

+------+--------+------+

当字段比较多的时候我们也可以使用G

mysql> select * from studentG

只查询表中某个字段的内容:

mysql> select name from student;

+--------+

| name   |

+--------+

| zhangs |

| lis    |

| wange  |

| hangl  |

 

mysql> select id,name from student;

+------+--------+

| id   | name   |

+------+--------+

|    1 | zhangs |

|    2 | lis    |

|    3 | wange  |

|    4 | hangl  |

+------+--------+

查看别的数据库的表或者不在本数据库上进行查看:

语法:SELECT 字段 FROM 数据库名.表名;

mysql> select *from HA.student; 查看某个数据库下指定的表内容,数据库名.表名

+------+--------+------+

| id   | name   | age  |

+------+--------+------+

|    1 | zhangs |   21 |

|    2 | lis    |   24 |

|    3 | wange  |   26 |

|    4 | hangl  | NULL |

+------+--------+------+

这样等效于先use 数据库,然后查看

删除记录:

删除id为3的行

mysql> delete from students where id=3;

wps6FE7.tmp

删除age为空的行

mysql> delete from students where age is null;

wps6FE8.tmp

更新记录

mysql> update students set sex='M' where id=2;

wps6FE9.tmp

mysql> update students set id=2;  所有的都变为2

wps6FEA.tmp

update students set stname='zhangsan',age=21 where uid=1;

同时更新多个字段时候用,号隔开

四. SQL基础条件查询语句

语法:select 字段名1,字段名2 from 表名 [where 条件];

1;查询students表中的name,age

mysql> select name,age from students;

+--------+------+

| name   | age  |

+--------+------+

| zhangs |   21 |

| lis    |   24 |

| jk     |   24 |

| lo     |   26 |

| io     |   25 |

| jk     |   24 |

+--------+------+

2:去重复查询distinct

mysql> select distinct name,age from students;

+--------+------+

| name   | age  |

+--------+------+

| zhangs |   21 |

| lis    |   24 |

| jk     |   24 |

| lo     |   26 |

| io     |   25 |

mysql> select  distinct id,name,age from students where id=3;

+------+------+------+

| id   | name | age  |

+------+------+------+

|    3 | jk   |   24 |

select distinct * from students; mysql的distinct可以对*使用

3:使用and和or进行多条件查询

or和and 同时存在时,先算and的两边值,逻辑与先执行

mysql> select id,name,age from students where id>3 and age>25;

+------+------+------+

| id   | name | age  |

+------+------+------+

|    5 | lo   |   26 |

+------+------+------+

mysql> select id,name,age from students where id>3 or age>25;

+------+------+------+

| id   | name | age  |

+------+------+------+

|    5 | lo   |   26 |

|    6 | io   |   25 |

+------+------+------+

select * from students where stname='zhangsan' and (age=21 or age=24);

注意and和or都是用的时候的逻辑关系

4:MySQL区分大小写查询:

Mysql查询默认是不区分大小写的

mysql> select name from students where name='jk';

+------+

| name |

+------+

| jk   |

| jk   |

| JK   |

+------+

解决

mysql> select * from students where binary name='jk';

+------+------+------+------+

| id   | name | age  | sex  |

+------+------+------+------+

|    3 | jk   |   24 | W    |

|    3 | jk   |   24 | W    |

+------+------+------+------+

BINARY是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写.

5:MySQL查询排序:

语法:select distinct 字段1,字段2 from 表名order by 字段名;

默认为升序  asc

mysql> select distinct id from students order by id;

+------+

| id   |

+------+

|    2 |

|    3 |

|    5 |

|    6 |

|    8 |

+------+

mysql> select distinct id from students order by id desc;

+------+

| id   |

+------+

|    8 |

|    6 |

|    5 |

|    3 |

|    2 |

+------+

关于MySQL命令帮助

help

mysql> help show;

wps6FEB.tmp

会告诉我们很多使用方法和信息

mysql> help select;

wps6FFC.tmp

免责声明:文章转载自《2-14-1 MySQL基础语句,查询语句》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇select下拉框默认不能选择第一个选项的问题centos7 firewall指定IP与端口、端段访问(常用)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

安装PL/SQL Developer 遇到的问题及解决方法

在用PL/SQL Developer安装Oracle客户端时,报错误,初始化失败,一直找不到原因,换Oracle版本也解决不了问题,之后才发现,是Oracle的环境变量配置错了,之前用户配了Oracle全局环境变量在windows 开始菜单-->计算机--->右键属性--->高级系统设置-->最下边点环境变量-->系统变量--...

PetShop 4.0 详解之四(PetShop之ASP.NET缓存)

继续第四讲,转自[ http://www.cnblogs.com/YoungPeng/archive/2008/10/13/1177622.html#1339706 ] 如果对微型计算机硬件系统有足够的了解,那么我们对于Cache这个名词一定是耳熟能详的。在CPU以及主板的芯片中,都引入了这种名为高速缓冲存储器(Cache)的技术。因为Cache的存取速度比...

关于table中使用了colspan后导致列宽度失效问题

因为一般表格都会定义一个table-layout: fixed;防止td被连串字符撑开,但是如果首行有合并单元格的话 TD宽度定义就会失效。最好的办法,在最上边加以下代码来控制td宽度<colgroup> <col width=60%></col> <col width=20%></col> &l...

还在用分库分表?看TiDB如何解决海量数据无感扩容难题

近日,一家国产企业级数据库公司 PingCAP 刚刚获得了 2.7 亿美元 D 轮融资,在数据库行业引起了很大反响。在新基建和国产自主化浪潮的推动下,PingCAP 的主打产品 TiDB 数据库热度持续上升。而随着云原生技术的迅速普及,各类大中小企业和技术团队为了更好地节省成本、深入体验云上数据库的灵活性和弹性,也纷纷将目光投向了 TiDB 的云上数据库...

【ETL】Kettle字符编码问题

0x01:Kettle保存文件时提示错误? 一般这种问题是因为数据库连接与数据库字符编码不一致导致的,需指定数据库连接字符编码与数据库一致【Repository Manager】->【数据库连接】->【选项】->【命名参数:characterEncoding;值:数据库编码(如:gbk) 0x02:打开转换时提示【一个数据库错误发生在从...

文本超过长度后隐藏,显示省略号

table{   table-layout:fixed; } td{    white-space:nowrap;   overflow:hidden;   text-overflow:ellipsis; }   在table和td分别新增这几个样式就可以了,效果如下: 本方法用于解决表格单元格内容过多时的美观问题,主要涉及到4句CSS样式: 1....