Centos7源码安装-SQL语言

Posted goonxiaoqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7源码安装-SQL语言相关的知识,希望对你有一定的参考价值。

?安装

依赖包安装:

 yum install -y gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel

 创建mysql用户,软件存放目录,安装目录

[root@localhost tools]# groupadd mysql    #创建用户
[root@localhost tools]# useradd -r -g mysql mysql
[root@localhost tools]# mkdir /usr/local/mysql  #安装目录
[root@localhost tools]# mkdir -p /data/mysql      #数据目录

 小插曲:CRT上传下载插件 yum -y install lrzsz

 编译

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DDEFAULT_CHARSET=utf8 -DDEFALUT_CHARSETS=all -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DMYSQL_TCP_PORT=3306 -DWITH_DEBUG=0
tar zxvf mysql-5.5.13.tar.gz
cd mysql-5.5.13
cmake . \                                      #开始编译 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \      #安装到/usr/local/mysql目录 
-DMYSQL_DATADIR=/data/mysql \                  #数据存放到/data/mysql目录 
-DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock \    #sock存放到/data/mysql目录 
-DWITH_INNOBASE_STORAGE_ENGINE=1 \             #innoDB引擎
-DENABLED_LOCAL_INFILE=1 \ 
-DDEFAULT_CHARSET=utf8 \                       #字符集
-DDEFALUT_CHARSETS=all \                       #支持所有字符集
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 
-DWITH_PARTITION_STORAGE_ENGINE=1 \ 
-DDEFAULT_COLLATION=utf8_general_ci \          #字符集校验
-DMYSQL_USER=mysql \ 
-DEXTRA_CHARSETS=all \ 
-DMYSQL_TCP_PORT=3306 \ 
-DWITH_DEBUG=0\
make
make install  
到此处源码包安装完毕。

更改目录权限
[root@localhost mysql-5.5.32]# chown -R mysql /usr/local/mysql
[root@localhost mysql-5.5.32]# chgrp -R mysql /usr/local/mysql
[root@localhost mysql-5.5.32]# chown -R mysql /data/mysql
[root@localhost mysql-5.5.32]# chgrp -R mysql /data/mysql
[root@localhost scripts]# chown -R root /usr/local/mysql

 初始化数据库

在这个目录下有初始化数据库命令/opt/mysql-5.5.32/scripts

sh mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --ldata=/data/mysql

 启动文件,配置文件

在包内有目录upport-files 里面有配置文件和启动文件 拷贝到相应目录下
启动文件:
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
[root@localhost support-files]# chmod 755 /etc/init.d/mysqld 
配置文件:   
[root@localhost support-files]# cp my-medium.cnf /etc/my.cnf

 开机自启动相关设置

[root@localhost support-files]# chkconfig --add mysqld
[root@localhost support-files]# chkconfig mysqld on
[root@localhost support-files]# chkconfig --levels 245 mysqld off  
添加环境变量
[root@localhost support-files]# export PATH=$PATH:/usr/local/mysql/bin 
[root@localhost support-files]# echo ‘PATH=$PATH:/usr/local/mysql/bin‘ >> /etc/profile 

  启动

service mysqld start/stop 

 数据库小优化,安全

mysql> select user,host from mysql.user;  #mysql.user 授权表
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
|      | localhost             |
| root | localhost             |
|      | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
6 rows in set (0.01 sec)

mysql> delete from mysql.user where user=‘‘;    #删除user为空的
Query OK, 2 rows affected (0.00 sec)

mysql> select user,host from mysql.user;      #查看
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
4 rows in set (0.00 sec)

mysql> delete from mysql.user where host=‘localhost.localdomain‘;    #删除host
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
4 rows in set (0.00 sec)

mysql> delete from mysql.user where host=‘::1‘;    #删除host
Query OK, 1 row affected (0.00 sec)

mysql> select user,host from mysql.user;        #查看
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
3 rows in set (0.00 sec)
mysql> drop databaes test; #删除test库 #安全性

?额外授权管理员

mysql> delete from mysql.user;

mysql> grant all privileges on *.* to ststem@‘localhost‘ idetified by ‘oldboy123‘ with grant option;


 

?结构化查询语言:

  SQL 英文全程structured Query Language 中文意思是结构化查询语句。

  它是一种对关系数据库中的数据进行定义和操作的语言方法。

  一、数据查询语言(DQL)

    从表中获取数据 select 伴随 where ,order by(排序),group by(分组),having。

mysql> select user,host from mysql.user; 查看
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
|      | localhost             |
| root | localhost             |
|      | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
6 rows in set (0.00 sec)
mysql> select user,host from mysql.user order by host asc; #升序
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
|      | localhost             |
| root | localhost             |
|      | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
6 rows in set (0.00 sec)

mysql> select user,host from mysql.user order by host desc; #倒叙
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | localhost.localdomain |
|      | localhost.localdomain |
| root | localhost             |
|      | localhost             |
| root | ::1                   |
| root | 127.0.0.1             |
+------+-----------------------+
6 rows in set (0.00 sec)

  二 数据库操作语言(DML)

    insert(插入)  update(更新) delect(删除)

    处理表中的数据

mysql> select user,host from mysql.user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
|      | localhost             |
| root | localhost             |
|      | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
6 rows in set (0.00 sec)

mysql> delete from mysql.user where host=‘localhost.localdomain‘;
Query OK, 2 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| root | localhost |
+------+-----------+
4 rows in set (0.00 sec)

  三、事务处理语言(TPL)

    begin transaction(开始事务),commit(提交) ,rollback(回滚)

    它的语句能确保被DML(数据库操作语句)语句影响的表的所有行及时得以更新。

  四、数据控制语句(DCL)

    grant(授权)  revoke(撤销)

    确定单个用户和用户组对数据库对象的访问

  五、数据定义语言(DDL)

    create(创建) drop(删除) create table (创建表) drop table(删除表)

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

  六、指针控制语言(CCL)    

    declare (声明),cursor(光标),fetch into update where current(获取当前位置的更新)

    用于对一个或多个表单独行的操作  

重点小结:
    DDL 数据定义语言 (create(创建),alter(添加),drop(删除))
    DML 数据操作语言(select(查询),insert(插入),delete(删除),update(更新))
    DCL 数据控制语言 (grant(授权),revoke(撤销),commit(提交),rollback(回滚))

 

?数据库常见管理应用 

  (创建数据库)

  命令语法 : create database <数据库名称>    #注意能数字开头

第一种:
mysql> create database wahaha; #创建数据库 Query OK, 1 row affected (0.01 sec) mysql> show databases; #查询数据库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | wahaha | +--------------------+ 5 rows in set (0.00 sec) mysql> show create database wahaha; #查询建库语句 +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | wahaha | CREATE DATABASE `wahaha` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec)

 建立一个名为wahaha_gbk的GBK字符集数据库

第二种
mysql> create database wahaha_gbk default character set gbk collate gbk_chinese_ci; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | wahaha | | wahaha_gbk | +--------------------+ 6 rows in set (0.00 sec) mysql> show create database wahaha_gbk; +------------+--------------------------------------------------------------------+ | Database | Create Database | +------------+--------------------------------------------------------------------+ | wahaha_gbk | CREATE DATABASE `wahaha_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ | +------------+--------------------------------------------------------------------+ 1 row in set (0.00 sec)
# character 指定一个字符集
# collate gbk_chinese_ci 校对的规则 (校对规则是比对的方式)
第三种
mysql> create database wahaha_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci; #创建一个utf8的 有没有default一样
小结:
    create database wahaha; #默认数据库配置字符集
    create database wahaha_gbk default character set gbk collate gbk_chinese_ci;  #gbk字符集
    create database wahaha_utf8 default CHARACTER SET utf8 COLLATE utf8_general_ci;  #utf8字符集
提示:字符集的不一致是数据库乱码的罪魁祸首,如果编译的时候指定了特定字符集,则 以后创建对应的数据库就不需要指定字符集了

(显示数据库)

  命令:show databases;

mysql> show databases;  #查看所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wahaha             |
| wahaha_gbk         |
| wahaha_utf8        |
+--------------------+
7 rows in set (0.00 sec)

mysql> show databases like ‘%waha%‘; #匹配
+-------------------+
| Database (%waha%) |
+-------------------+
| wahaha            |
| wahaha_gbk        |
| wahaha_utf8       |
+-------------------+
3 rows in set (0.01 sec)

mysql> use wahaha;  # 进入数据库
Database changed
mysql> select database();    #查看当前数据库
+------------+
| database() |
+------------+
| wahaha     |
+------------+
1 row in set (0.00 sec)

 (删除数据库)

  命令:drop databaes <数据库名称>

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wahaha             |
| wahaha_gbk         |
| wahaha_utf8        |
+--------------------+
7 rows in set (0.00 sec)
mysql> 
mysql> drop database wahaha;  #删除数据库
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wahaha_gbk         |
| wahaha_utf8        |
+--------------------+
6 rows in set (0.00 sec)

  (链接数据库)

  命令:use <数据库名称>

mysql> use wahaha;  # 进入数据库
Database changed
mysql> select database();    #查看当前数据库
+------------+
| database() |
+------------+
| wahaha     |
+------------+
1 row in set (0.00 sec)
插曲:select user();    #查看用户
         select now();    #时间
         select version();#版本

  (查看所有的表)

  命令:show tables;

show tables like ‘user‘;
show tables from wahaha;
show tables from wahaha_utf8;
show tables in wahaha_gbk;

  (删除MySQL系统多余账户)

  命令:drop  user "user"@"主机域"  

drop user ‘root‘@‘localhost‘;

 生效 flush privieges;


 

?创建MySQL用户及赋予用户权限

create user ‘jeffrey‘@‘localhost‘ identified by ‘mypass‘;
grant all on db1.* to ‘jeffrey‘@‘localhost‘;
grant select on db2.invoice to ‘jeffrey‘@‘localhost‘;
grant usage on *.* to ‘jeffrey‘@‘localhost‘; with max_queries_per_hour 90;

  一定要背下来 

grant  all on db1.* to ‘jeffrey‘@‘localhost‘  identified by ‘mypass‘;
grant  all on            #权限
db1.*                    #库 。表
‘jeffrey‘@‘localhost‘    #用户  主机
 identified by ‘mypass‘; #密码是什么                  

 查看用户的权限

mysql> show grants for xiaoqi@localhost; #查看赋予的权限
+---------------------------------------------------------------------------------------------------------------+
| Grants for xiaoqi@localhost                                                                                   |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘xiaoqi‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1‘ |
| GRANT ALL PRIVILEGES ON `wahaha`.* TO ‘xiaoqi‘@‘localhost‘                                                    |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

看看里面是什么  ALL PRIVILEGES(privileges)18个

select,  #查找
insert,  #插入
update,  #更新
delete,  #删除
create,  #创建库 表
drop,    #删除库 表
references,  #参考
index,    #索引
alter,    #修改
create temporary tables,  #创建临时表
lock tables,        #锁表
execute,          #执行
create view,         #创建视图
show view,         #显示视图
create routine,      #创建存储过程
alter routine,      #修改存储过程
event,           #事件
trigger          #事件触发器

收回权限

mysql> revoke insert on wahaha.* from ‘xiaoqi‘@‘localhost‘ ; #收回insert权限
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for xiaoqi@‘localhost‘; #查看赋予的权限
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for xiaoqi@localhost                                                                                                                                                                                                      |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘xiaoqi‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1‘                                                                                                                    |
| GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `wahaha`.* TO ‘xiaoqi‘@‘localhost‘ |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

 小技巧

 mysql -uroot -p123123 -e "show databases;" -S mysql.sock  #可以在shell环境下执行sql

 

?表的操作  

基本语法:create table <表名称>(<字段名1><类型1>,.....<字段名n><类型n>);

mysql> create table student( id int(4) not null, name char(20) not null, age tinyint(2) not null default ‘0‘, dept varchar(16) default null); 
Query OK, 0 rows affected (0.01 sec)
 
mysql> show tables;
+------------------+
| Tables_in_xiaoqi |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)
mysql> show create table student\G    #查看建表语句
*************************** 1. row ***************************
       Table: student
Create Table: CREATE TABLE `student` (
  `id` int(4) NOT NULL,      #数字类型 常用
  `name` char(20) NOT NULL,    #字符串  常用
  `age` tinyint(2) NOT NULL DEFAULT ‘0‘,  #很小的整数
  `dept` varchar(16) DEFAULT NULL      #变长字符类型  常用
) ENGINE=InnoDB DEFAULT CHARSET=utf8    #引擎和字符集 引擎默认InnoDB 字符集 继承库utf8

查看表结构

mysql> desc student; 查看表结构
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | NO   |     | NULL    |       |
| name  | char(20)    | NO   |     | NULL    |       |
| age   | tinyint(2)  | NO   |     | 0       |       |
| dept  | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) 

 表中创建索引

  首先,无论建立主键索引还是普通索引,都要在表的对应列上创建,可以对单列创建索引,也可以对多列创建索引!

第一种方法:

  create table student(

  id int(4) not null auto_increment, #自增

   name char(20) not null,

   age tinyint(2) not null default ‘0‘,

  dept varchar(16) default null,

  primary key(id),  #id 创建主键

  key index_name(name)  #name 创建普通索引

);

mysql> create table student( id int(4) not null auto_increment, name char(20) not null, age tinyint(2) not null default ‘0‘, dept varchar(16) default null, primary key(id), key index_name(name) );

Query OK, 0 rows affected (0.00 sec) mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
主键索引 PRI
普通索引 MUL

 2、建表后利用alter增加普通索引

  删除建表时创建的index_name索引

mysql> alter table student drop index index_name;   #删除索引index_name
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;   #查看结构
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(4)      | NO   | PRI | NULL    | auto_increment |
| name  | char(20)    | NO   |     | NULL    |                |
| age   | tinyint(2)  | NO   |     | 0       |                |
| dept  | varchar(16) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

 添加索引

mysql> desc student; #查看表结构
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(4)      | NO   | PRI | NULL    | auto_increment |
| name  | char(20)    | NO   |     | NULL    |                |
| age   | tinyint(2)  | NO   |     | 0       |                |
| dept  | varchar(16) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table student add index index_name(name); #添加一个普通索引 在name行 索引名称是index_name
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;  #查看表结构
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(4)      | NO   | PRI | NULL    | auto_increment |
| name  | char(20)    | NO   | MUL | NULL    |                |
| age   | tinyint(2)  | NO   |     | 0       |                |
| dept  | varchar(16) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

插入表中数据

mysql> create table test( id int(4) not null auto_increment, name char(20) not null, primary key(id));  
#创建表 test id为索引自增模式
Query OK, 0 rows affected (0.00 sec) mysql> show tables; #看表 +------------------+ | Tables_in_xiaoqi | +------------------+ | student | | test | +------------------+ 2 rows in set (0.00 sec) mysql> desc test;  #看结构 +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | | NULL | | +-------+----------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)

 插入

mysql>  insert into test(id,name) values(1,‘oldboy‘); #插入
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | oldboy |
+----+--------+
1 row in set (0.00 sec)
mysql> insert into test(name) values(‘oldgirl‘); #插入 因为建表时候 auto_increment 参数 Query OK, 1 row affected (0.01 sec) mysql> select * from test; +----+---------+ | id | name | +----+---------+ | 1 | oldboy | | 2 | oldgirl | +----+---------+ 2 rows in set (0.00 sec)

mysql> insert into test values(3,‘inca‘); #不指定列的时候要对应的去插入值 类型一定要对应

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
| 3 | inca   | +----+---------+
mysql > insert into test values(4,‘zuma‘),(5,‘kaka‘); #多条数据
mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
| 3 | inca  |
| 4 | zuma |
| 5 | kaka | 
+----+---------+

删除所有表数据

mysql> delete from  test; #删除所有表数据
Query OK, 8 rows affected (0.01 sec)

mysql> select * from test; #查看
Empty set (0.00 sec)

 我们用一条命令还原回来

 也算是优化:insert into test(id,name) values(1,‘oldboy‘),(2,‘oldgirl‘),(3,‘inca‘),(4,‘zuma‘),(5,‘kaka‘);

 备份 mysqldump(逻辑备份 已sql语句的形式倒出来

[root@localhost opt]# mysqldump -uroot -p123123 -B xiaoqi > xiaoqi.sql

   检查 :都是我们mysqldump东西  

[root@localhost opt]# grep -E -v "#|\/|^$|--" xiaoqi.sql

 ?查询数据

  语法: select <字段1,字段2,......> from <表名字> where <表达式>;

mysql> show tables;
+------------------+
| Tables_in_xiaoqi |
+------------------+
| student          |
| test             |
+------------------+
2 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> select * from test; #查看所有表中的数据
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
|  4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

mysql> select id,name from test; #查看id,name列所有内容
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
|  4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

mysql>  select id,name from test limit 2; #显示2行
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
+----+---------+
2 rows in set (0.00 sec)

mysql> select id,name from test where id=1; #id查询为1的数据
+----+--------+
| id | name   |
+----+--------+
|  1 | oldboy |
+----+--------+
1 row in set (0.01 sec)

mysql> select id,name from test where name=‘oldgirl‘; #字符串查询需要带引号
+----+---------+
| id | name    |
+----+---------+
|  2 | oldgirl |
+----+---------+
1 row in set (0.01 sec)

mysql> select id ,name from test where name=‘oldgirl‘ and id=5;  #既要满足name 也要满足id 咱们表中没有
Empty set (0.00 sec)

mysql> select id ,name from test where name=‘oldgirl‘ or id=5;    #只要出现name=oldgirl  或者 id=5的
+----+---------+
| id | name    |
+----+---------+
|  2 | oldgirl |
|  5 | kaka    |
+----+---------+
2 rows in set (0.00 sec)

mysql> select id ,name from test where id>2 and id<4; #范围查询 大于2小于4
+----+------+
| id | name |
+----+------+
|  3 | inca |
+----+------+
1 row in set (0.01 sec)

mysql> select id ,name from test where id>2 or id<4;   #大于2出来,小于4的出来
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
|  4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

mysql> select id ,name from test order by id;  # 排序 默认是升序
+----+---------+
| id | name    |
+----+---------+
|  1 | oldboy  |
|  2 | oldgirl |
|  3 | inca    |
|  4 | zuma    |
|  5 | kaka    |
+----+---------+
5 rows in set (0.00 sec)

mysql> select id ,name from test order by id desc; #倒叙
+----+---------+
| id | name    |
+----+---------+
|  5 | kaka    |
|  4 | zuma    |
|  3 | inca    |
|  2 | oldgirl |
|  1 | oldboy  |
+----+---------+
5 rows in set (0.01 sec)

多表查询

创建一推数据如下:

ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 #如有特别要求 指定引擎 跟编码 auto_increment 自增
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
ENGINE=InnoDB DEFAULT CHARSET=latin1

create table student(Sno int(10) NOT NULL COMMENT ‘学号‘,Sname varchar(16) NOT NULL COMMENT ‘姓名‘,Ssex char(2) NOT NULL COMMENT ‘性别‘,Sage tinyint(2) NOT NULL COMMENT ‘年龄‘,Sdept varchar(16) default NULL COMMENT ‘学生所在系别‘,  PRIMARY KEY (Sno),key index_Sname(Sname)) AUTO_INCREMENT=1;

Create table course(Con int(10) NOT NULL COMMENT ‘课程号‘,Cname varchar(64) NOT NULL COMMENT ‘课程名称‘,Credit tinyint(2) NOT NULL COMMENT ‘学分‘,PRIMARY KEY (Con)) AUTO_INCREMENT=1;

Create table SC(SCid int(12) NOT NULL auto_increment COMMENT ‘主键‘, Con int(10) NOT NULL COMMENT ‘课程号‘,Sno int(10) NOT NULL COMMENT ‘学号‘,Grade tinyint(2) NOT NULL COMMENT ‘学生成绩‘, PRIMARY KEY (SCid));

学生表插入(student)
Insert into student values(0001,‘宏志‘,‘男‘,‘30‘,‘计算机网络‘);
Insert into student values(0002,‘王硕‘,‘男‘,‘30‘,‘computer application‘);
Insert into student values(0003,‘oldboy‘,‘男‘,‘28‘,‘物流管理‘);
Insert into student values(0004,‘脉动‘,‘男‘,‘29‘,‘computer application‘);
Insert into student values(0005,‘oldgirl‘,‘女‘,‘26‘,‘计算机科学与技术‘);
Insert into student values(0006,‘莹莹‘,‘女‘,‘22‘,‘护士‘);
课程表插入(course)
Insert into course values(1001,‘linux中高级运维‘,3);
Insert into course values(1002,‘linux高级运维‘,5);
Insert into course values(1003,‘MySQL高级Dda‘,4);
Insert into course values(1004,‘python运维开发‘,4);
Insert into course values(1005,‘java web开发‘,3);
选课表(sc)
Insert into SC(Sno,Con,Grade) values(0001,1001,4);
Insert into SC(Sno,Con,Grade) values(0001,1002,3);
Insert into SC(Sno,Con,Grade) values(0001,1003,1);
Insert into SC(Sno,Con,Grade) values(0001,1004,6);

Insert into SC(Sno,Con,Grade) values(0002,1001,3);
Insert into SC(Sno,Con,Grade) values(0002,1002,2);
Insert into SC(Sno,Con,Grade) values(0002,1003,2);
Insert into SC(Sno,Con,Grade) values(0002,1004,8);

Insert into SC(Sno,Con,Grade) values(0003,1001,4);
Insert into SC(Sno,Con,Grade) values(0003,1002,4);
Insert into SC(Sno,Con,Grade) values(0003,1003,2);
Insert into SC(Sno,Con,Grade) values(0003,1004,8);

Insert into SC(Sno,Con,Grade) values(0004,1001,1);
Insert into SC(Sno,Con,Grade) values(0004,1002,1);
Insert into SC(Sno,Con,Grade) values(0004,1003,2);
Insert into SC(Sno,Con,Grade) values(0004,1004,3);

Insert into SC(Sno,Con,Grade) values(0005,1001,5);
Insert into SC(Sno,Con,Grade) values(0005,1002,3);
Insert into SC(Sno,Con,Grade) values(0005,1003,2);
Insert into SC(Sno,Con,Grade) values(0005,1004,9);

结果:

mysql> show tables;
+----------------------+
| Tables_in_xiaoxiaoqi |
+----------------------+
| SC                   |
| course               |
| student              |
+----------------------+
3 rows in set (0.00 sec)

mysql> select * from student;
+-----+---------+------+------+--------------------------+
| Sno | Sname   | Ssex | Sage | Sdept                    |
+-----+---------+------+------+--------------------------+
|   1 | 宏志    | 男   |   30 | 计算机网络               |
|   2 | 王硕    | 男   |   30 | computer applica         |
|   3 | oldboy  | 男   |   28 | 物流管理                 |
|   4 | 脉动    | 男   |   29 | computer applica         |
|   5 | oldgirl | 女   |   26 | 计算机科学与技术         |
|   6 | 莹莹    | 女   |   22 | 护士                     |
+-----+---------+------+------+--------------------------+
6 rows in set (0.01 sec)

mysql> select * from SC;
+------+------+-----+-------+
| SCid | Con  | Sno | Grade |
+------+------+-----+-------+
|    1 | 1001 |   1 |     4 |
|    2 | 1002 |   1 |     3 |
|    3 | 1003 |   1 |     1 |
|    4 | 1004 |   1 |     6 |
|    5 | 1001 |   2 |     3 |
|    6 | 1002 |   2 |     2 |
|    7 | 1003 |   2 |     2 |
|    8 | 1004 |   2 |     8 |
|    9 | 1001 |   3 |     4 |
|   10 | 1002 |   3 |     4 |
|   11 | 1003 |   3 |     2 |
|   12 | 1004 |   3 |     8 |
|   13 | 1001 |   4 |     1 |
|   14 | 1002 |   4 |     1 |
|   15 | 1003 |   4 |     2 |
|   16 | 1004 |   4 |     3 |
|   17 | 1001 |   5 |     5 |
|   18 | 1002 |   5 |     3 |
|   19 | 1003 |   5 |     2 |
|   20 | 1004 |   5 |     9 |
+------+------+-----+-------+
20 rows in set (0.00 sec)

mysql> select * from course;
+------+----------------------+--------+
| Con  | Cname                | Credit |
+------+----------------------+--------+
| 1001 | linux中高级运维      |      3 |
| 1002 | linux高级运维        |      5 |
| 1003 | MySQL高级Dda         |      4 |
| 1004 | python运维开发       |      4 |
| 1005 | java web开发         |      3 |
+------+----------------------+--------+
5 rows in set (0.00 sec)

mysql> desc SC;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| SCid  | int(12)    | NO   | PRI | NULL    | auto_increment |
| Con   | int(10)    | NO   |     | NULL    |                |
| Sno   | int(10)    | NO   |     | NULL    |                |
| Grade | tinyint(2) | NO   |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc course;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| Con    | int(10)     | NO   | PRI | NULL    |       |
| Cname  | varchar(64) | NO   |     | NULL    |       |
| Credit | tinyint(2)  | NO   |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Sno   | int(10)     | NO   | PRI | NULL    |       |
| Sname | varchar(16) | NO   | MUL | NULL    |       |
| Ssex  | char(2)     | NO   |     | NULL    |       |
| Sage  | tinyint(2)  | NO   |     | NULL    |       |
| Sdept | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> show create table student \G
*************************** 1. row ***************************
       Table: student
Create Table: CREATE TABLE `student` (
  `Sno` int(10) NOT NULL COMMENT ‘学号‘,
  `Sname` varchar(16) NOT NULL COMMENT ‘姓名‘,
  `Ssex` char(2) NOT NULL COMMENT ‘性别‘,
  `Sage` tinyint(2) NOT NULL COMMENT ‘年龄‘,
  `Sdept` varchar(16) DEFAULT NULL COMMENT ‘学生所在系别‘,
  PRIMARY KEY (`Sno`),
  KEY `index_Sname` (`Sname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table course \G
*************************** 1. row ***************************
       Table: course
Create Table: CREATE TABLE `course` (
  `Con` int(10) NOT NULL COMMENT ‘课程号‘,
  `Cname` varchar(64) NOT NULL COMMENT ‘课程名称‘,
  `Credit` tinyint(2) NOT NULL COMMENT ‘学分‘,
  PRIMARY KEY (`Con`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table SC \G
*************************** 1. row ***************************
       Table: SC
Create Table: CREATE TABLE `SC` (
  `SCid` int(12) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘,
  `Con` int(10) NOT NULL COMMENT ‘课程号‘,
  `Sno` int(10) NOT NULL COMMENT ‘学号‘,
  `Grade` tinyint(2) NOT NULL COMMENT ‘学生成绩‘,
  PRIMARY KEY (`SCid`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

查询:(3张表一起查询)

学生成绩查询

mysql> select student.Sno,student.Sname,course.Cname,SC.Grade from student,SC,course where student.Sno=SC.Sno and course.Con=SC.Con;
select 表名.列,表名.列 ..... from 表1,表2,表3 ...... where 条件1 and|or 条件2;
+-----+---------+----------------------+-------+ | Sno | Sname | Cname | Grade | +-----+---------+----------------------+-------+ | 1 | 宏志 | linux中高级运维 | 4 | | 2 | 王硕 | linux中高级运维 | 3 | | 3 | oldboy | linux中高级运维 | 4 | | 4 | 脉动 | linux中高级运维 | 1 | | 5 | oldgirl | linux中高级运维 | 5 | | 1 | 宏志 | linux高级运维 | 3 | | 2 | 王硕 | linux高级运维 | 2 | | 3 | oldboy | linux高级运维 | 4 | | 4 | 脉动 | linux高级运维 | 1 | | 5 | oldgirl | linux高级运维 | 3 | | 1 | 宏志 | MySQL高级Dda | 1 | | 2 | 王硕 | MySQL高级Dda | 2 | | 3 | oldboy | MySQL高级Dda | 2 | | 4 | 脉动 | MySQL高级Dda | 2 | | 5 | oldgirl | MySQL高级Dda | 2 | | 1 | 宏志 | python运维开发 | 6 | | 2 | 王硕 | python运维开发 | 8 | | 3 | oldboy | python运维开发 | 8 | | 4 | 脉动 | python运维开发 | 3 | | 5 | oldgirl | python运维开发 | 9 | +-----+---------+----------------------+-------+ 20 rows in set (0.00 sec) 

以上是关于Centos7源码安装-SQL语言的主要内容,如果未能解决你的问题,请参考以下文章

Centos7源码安装MongoDB-3.6

CentOS7系统上的LAPACK源码安装

centos7安装go语言环境

Mongodb源码安装for centos7

centos7源码安装ffmpeg

Erlang源码安装