三、安装Mysql
(1)实验环境
系统 | 主机名 | ip地址 | 使用的源码包 | 硬件 |
Centos7.4 | rzy | 192.168.100.202 | mysql-5.7.12.tar.gz,boost_1_59_0.tar.gz | 4G内存,双核心 |
(2)实验步骤
首先先配置虚拟机硬件
******(1)先做基础配置 [root@Centos7 ~]# hostnamectl set-hostname rzy [root@Centos7 ~]# su [root@rzy ~]# systemctl stop firewalld [root@rzy ~]# setenforce 0 setenforce: SELinux is disabled [root@rzy ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载 mount: /dev/sr0 已经挂载或 /mnt 忙 /dev/sr0 已经挂载到 /mnt 上 ******(2)上传两个源码包 [root@rzy ~]# yum -y remove mysql-* boost-* #先删除系统内Mysql和Boost 。。。。。。 完毕! [root@rzy ~]# yum -y install gcc gcc-c++ ncurses bison libgcrypt perl cmake ncurses-devel #安装依赖包 。。。。。。 完毕! [root@rzy ~]# rz z waiting to receive.**B0100000023be50 [root@rzy ~]# ll 总用量 118132 -rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg -rw-r--r-- 1 root root 70389425 5月 11 14:35 boost_1_59_0.tar.bz2 -rw-r--r-- 1 root root 50571897 5月 11 14:30 mysql-5.7.12.tar.gz ******(3)先安装boost再安装mysql [root@rzy ~]# tar xf boost_1_59_0.tar.bz2 [root@rzy ~]# mv boost_1_59_0 /usr/local/boost [root@rzy ~]# groupadd mysql #创建mysql组 [root@rzy ~]# useradd -r -g mysql mysql #创建用户mysql并且指定组 [root@rzy ~]# tar xf mysql-5.7.12.tar.gz -C /usr/src [root@rzy ~]# cd /usr/src/mysql-5.7.12 [root@rzy mysql-5.7.12]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #使用cmake进行编译 > -DMYSQL_DATADIR=/usr/local/mysql/data \ > -DDEFAULT_CHARSET=utf8 \ > -DDEFAULT_COLLATION=utf8_general_ci \ > -DMYSQL_TCP_PORT=3306 \ > -DMYSQL_USER=mysql \ > -DWITH_MYISAM_STORAGE_ENGINE=1 \ > -DWITH_INNOBASE_STORAGE_ENGINE=1 \ > -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ > -DWITH_MEMORY_STORAGE_ENGINE=1 \ > -DENABLE_DOWNLOADS=1 \ > -DDOWNLOAD_BOOST=1 \ > -DWITH_BOOST=/usr/local/boost \ > -DSYSCONFDIR=/etc #等待编译完成 [root@rzy mysql-5.7.12]# make -j `cat /proc/cpuinfo | grep processor| wc -l` #根据系统内核来进行编译,后面的命令就是查看有几个系统内核 [root@rzy mysql-5.7.12]# make install [root@rzy mysql-5.7.12]# ls /usr/local/mysql #检查是否安装成功 bin COPYING docs include lib man mysql-test README share support-files ******(4)优化mysql程序 [root@rzy mysql-5.7.12]# chown -R mysql:mysql /usr/local/mysql #修改mysql目录下的属组和属主 [root@rzy mysql-5.7.12]# cp /usr/src/mysql-5.7.12/support-files/my-default.cnf /etc/my.cnf cp:是否覆盖"/etc/my.cnf"? y [root@rzy mysql-5.7.12]# cp /usr/src/mysql-5.7.12/support-files/mysql.server /etc/init.d/ [root@rzy mysql-5.7.12]# chmod +x /etc/init.d/mysql.server #增加可执行权限 [root@rzy mysql-5.7.12]# vim /usr/lib/systemd/system/mysql.service #编写mysql的控制脚本 [Unit] Description=mysqldapi After=network.target [Service] Type=forking PIDFile=/usr/local/mysql/logs/mysqld.pid ExecStart=/etc/init.d/mysql.server start ExecReload=/etc/init.d/mysql.server restart ExecStop=/etc/init.d/mysql.server stop PrivateTmp=Flase [Install] WantedBy=multi-user.target #保存退出 ———————————————————————————————————————————————————————————————— 注意!!!!: PrivateTmp=Flase #此配置必须关闭,不然mysql连接文件mysql.sock文件会默认生成在以下位置/tmp/systemd-private-83bba738e8ff4837b5ae657eff983821-mysqld.service-BPxWpJ/tmp/mysql.sock,导致数据库无法连接,将此配置项关闭后,则文件正常生成在/tmp/mysql.sock ———————————————————————————————————————————————————————————————— [root@rzy mysql-5.7.12]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile #配置全局变量优化mysql命令执行路径 [root@rzy mysql-5.7.12]# source /etc/profile #运行脚本使配置生效 [root@rzy mysql-5.7.12]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data #初始化数据库 ———————————————————————————————————————————————————————————————————————— 注解: --initialize-insecure #禁用mysql的密码策略(密码复杂性等) --initializeaize #开启密码策略,自动生成密码在mysqld.log文件中 --user=mysql #运行的账户 --basedir=/usr/local/mysql #mysql的安装位置 --datadir=/usr/local/mysql/data #mysql数据库服务数据的物理存放路径 ———————————————————————————————————————————————————————————————————————— 。。。。。。 [root@rzy mysql-5.7.12]# > /etc/my.cnf [root@rzy mysql-5.7.12]# vim /etc/my.cnf #编写mysql配置文件 [mysqld] basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES character_set_server=utf8 init_connect='SET NAMES utf8' log-error=/usr/local/mysql/logs/mysqld.log pid-file=/usr/local/mysql/logs/mysqld.pid skip-name-resolve ———————————————————————————————————————————————————————————————————————— 注解: [mysqld] #声明区域 basedir = /usr/local/mysql #mysql的安装位置 datadir = /usr/local/mysql/data #mysql的物理文件存放位置 port = 3306 #mysql服务监听的端口 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #mysql的模式 character_set_server=utf8 #字符集 init_connect='SET NAMES utf8' log-error=/usr/local/mysql/logs/mysqld.log #指定日志文件位置 pid-file=/usr/local/mysql/logs/mysqld.pid #指定运行服务所产生的pid文件位置 skip-name-resolve #跳过mysql的域名反向解析,和tomcat屏蔽域名查询一个道理 ———————————————————————————————————————————————————————————————————————— [root@rzy mysql-5.7.12]# mkdir /usr/local/mysql/logs #创建日志存放目录 [root@rzy mysql-5.7.12]# chown mysql:mysql /usr/local/mysql/logs/ #修改属主和属组 [root@rzy mysql-5.7.12]# systemctl start mysqld #开启mysql [root@rzy mysql-5.7.12]# systemctl enable mysqld #配置为开机自启 Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service. [root@rzy mysql-5.7.12]# netstat -anpt | grep mysql #检查端口 tcp6 0 0 :::3306 :::* LISTEN 18642/mysqld ******(5)进入mysql,给root设置密码 [root@rzy mysql-5.7.12]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.12 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> exit Bye [root@rzy mysql-5.7.12]# mysqladmin -u root -p password 123123 Enter password: mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. [root@rzy mysql-5.7.12]# mysql -u root -p123123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.12 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> exit Bye
做完后,推荐保存虚拟机快照
四、SQL语句概述
(1)什么是SQL
SQL全称:Structured Query Language,即结构化查询语言
所有的关系型数据库都可以使用SQL语句,SQL是执行在客户端 (windows在命令行下执行,Linux在终端执行)
下或者通过Java代码执行在JDBC
(2)SQL语句分类
- DDL: 数据定义语言,用来建立数据库、数据对象和定义其列的项,命令有:create、alter、drop等
- DML: 数据操纵语言,用来查询、插入、删除、修改数据库中的数据,命令有:select、insert、update、delete等
- DCL: 数据控制语言,用来控制数据库组件的存取许可、存取权限等,赋权用户,命令有:commit、rollback、grant、revoke等
(3)数据类型
- 日期:
类型 | 大小(字节) | 范围 | 格式 | 用途 |
date | 3 | 1000-01-01到9999-12-31 | YYYY-MM-DD | 日期值 |
time | 3 | -838:59:59到838:59:59 | HIH:MM:SS | 时间值或持续时间 |
year | 1 | 1901到2155 | YYYY | 年份值 |
datetime | 8 | 1000-01-01 00:00:00到9999-12-31 23:59:59 | YYYY-MM-DD HIH:MM:SS | 混合日期和时间值 |
timestamp | 4 | 1970-01-01 00:00:00到2037 年某时 | YYYY-MM-DD HIH:MM:SS | 混合日期和时间值,时间戳 |
- 小数类型:
- 字符串:
类型 | 大小(可自行定义最大字节) | 用途 |
char | 0-255字节 | 定长字符串 |
varchar | 0-65535字节 | 变长字符串 |
tinyblob | 0-255字节 | 不超过255个字符的二进制字符串 |
tinytext | 0-255字节 | 短文本字符串 |
blob | 0-65535字节 | 二进制形式的长文本数据 |
text | 0-65535字节 | 长文本数据 |
mediumblob | 0-16777215字节 | 二进制形式的中等长度文本数据 |
mediumtext | 0-16777215字节 | 中等长度文本数据 |
longblob | 0-4294967295字节 | 二进制形式的极大文本数据 |
longtext | 0-4294967295字节 | 极大文本数据 |
(4)使用SQL语句
- 使用SQL语句的规格
- 每个命令要以;结尾
- 关键字之间要有空格,通常是一个空格,但是多个空格也不会报错
- SQL语句中可以换行,只要不输入;号,按回车就可以换行,直到有;后就会执行
- SQL语句不区分大小写
- 在编写表的值的时
- ,除了整数,基本上都要加"“,所以在写值时,直接加”"就行了
- 在mysql数据库中使用SQL
-查看所有数据库
[root@rzy ~]# mysql -u root -p123123 #进入数据库 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.12 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; #查看所有数据库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.13 sec)
-创建库,查看库信息
mysql> create database aaa; #创建数据库aaa Query OK, 1 row affected (0.00 sec) #ok表示命令执行成功 mysql> show databases; #查看所有数据库 +--------------------+ | Database | +--------------------+ | information_schema | | aaa | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> create database bbb character set utf8; #创建数据库bbb并且指定字符值为utf8 Query OK, 1 row affected (0.00 sec) mysql> show create database aaa; #查看数据库的详细信息,其实就是查看创建库时SQL语句的详细信息 +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | aaa | CREATE DATABASE `aaa` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+--------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> use aaa; #选中(进入)aaa库 Database changed #表示选中(进入)成功
-查看表,创建表
mysql> show tables; #查看当前aaa库下的所有表,因为是新创建的库,所以是没有表的 Empty set (0.00 sec) mysql> create table aaa(id int,name char(10),sex char(10)); #在当前库下创建一个新的表 Query OK, 0 rows affected (0.07 sec) mysql> show tables; #查看所有表,发现了刚刚新创建的表 +---------------+ | Tables_in_aaa | +---------------+ | aaa | +---------------+ 1 row in set (0.00 sec) mysql> show create table aaa; #查看表的详细信息,其实就是创建表时所使用的SQL语句的详细信息 +-------+------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------+ | aaa | CREATE TABLE `aaa` ( `id` int(11) DEFAULT NULL, `name` char(10) DEFAULT NULL, `sex` char(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) mysql> desc aaa; #查看表的项,可以看到创建表时的三个项id、name、sex都显示出来了 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> create table bbb(id int,name char(10),sex char(10)) engine=myisam charset=utf8; #创建表,指定存储引擎是myisam,使用字符是utf8,要注意的是,mysql创建表的默认存储引擎是innoDB Query OK, 0 rows affected (0.36 sec) mysql> show tables; +---------------+ | Tables_in_aaa | +---------------+ | aaa | | bbb | +---------------+ 2 rows in set (0.00 sec)
-表的重命名
mysql> rename table aaa to ccc; #把aaa表重命名为ccc Query OK, 0 rows affected (0.00 sec) mysql> show tables; #查看所有表,发现aaa没有了,多了一个ccc +---------------+ | Tables_in_aaa | +---------------+ | bbb | | ccc | +---------------+ 2 rows in set (0.00 sec)
-修改表的属性、格式
mysql> alter table bbb engine=innoDB charset=gbk; #修改bbb表的存储引擎为innoDB,字符为gbk Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table bbb; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | bbb | CREATE TABLE `bbb` ( `id` int(11) DEFAULT NULL, `name` char(10) CHARACTER SET utf8 DEFAULT NULL, `sex` char(10) CHARACTER SET utf8 DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=gbk | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
-添加表的字段(项)
mysql> desc bbb; #先查看bbb表的信息 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> alter table bbb add age int; #在bbb表的最后添加项,age类型是int形式的 Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbb; #查看bbb表的信息,发现成功添加 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table bbb add aaa char(10) after id; #在bbb表中的id项后面添加aaa项,类型是char Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbb; #查看bbb表的信息,发现成功在id后面添加aaa项 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | aaa | char(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> alter table bbb add bbb char(10) first; #添加bbb项类型为char,添加在bbb表中的第一个 Query OK, 0 rows affected (14.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbb; #查看bbb表,发现bbb项成功插入到第一个 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | bbb | char(10) | YES | | NULL | | | id | int(11) | YES | | NULL | | | aaa | char(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
-删除表的字段(项)
mysql> desc bbb; #先查看bbb表的信息 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | bbb | char(10) | YES | | NULL | | | id | int(11) | YES | | NULL | | | aaa | char(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> alter table bbb drop bbb; #删除bbb表中的bbb项 Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbb; #再次查看bbb表,发现成功删除 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | aaa | char(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
-修改表的字段和类型
mysql> desc bbb; #先查看bbb表的信息 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | aaa | char(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> alter table bbb change aaa abc varchar(10); #修改bbb表中的aaa项为abc类型为varchar Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbb; #再次查看bbb表的信息,发现成功修改 +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | abc | varchar(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
-修改表的字段类型和顺序
mysql> desc bbb; #先查看bbb表的信息 +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | abc | varchar(10) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> alter table bbb modify abc char(10) after sex; #修改bbb表中的abc项的类型为char并且移动项到sex项的后面,这里也可以使用first移动到第一个 Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbb; #再次查看bbb表,发现成功修改abc的类型并且成功移动到sex的后面 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | abc | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
-删除表和库
mysql> show tables ; #先查看当前库下的所有表 +---------------+ | Tables_in_aaa | +---------------+ | bbb | | ccc | +---------------+ 2 rows in set (0.00 sec) mysql> drop table ccc; #删除ccc表 Query OK, 0 rows affected (0.00 sec) mysql> show tables; #再次查看所有表发现ccc表没有了 +---------------+ | Tables_in_aaa | +---------------+ | bbb | +---------------+ 1 row in set (0.00 sec) mysql> show databases; #查看所有库 +--------------------+ | Database | +--------------------+ | information_schema | | aaa | | bbb | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> drop database bbb; #删除bbb库 Query OK, 0 rows affected (0.09 sec) mysql> show databases; #再次查看所有库,发现bbb库没有了 +--------------------+ | Database | +--------------------+ | information_schema | | aaa | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
-给表中插入数据
mysql> show tables; #查看当前库下的所有表 +---------------+ | Tables_in_aaa | +---------------+ | bbb | +---------------+ 1 row in set (0.00 sec) mysql> desc bbb; #查看bbb表的信息 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | abc | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> insert into bbb values(1,"rzy","man","aaa","20"); #插入数据,直接使用values的话,输入的值的顺序必须和项的顺序相同 Query OK, 1 row affected (0.00 sec) mysql> insert into bbb (id,name) values(2,"zhangsan"); #插入数据,这样插入可以指定想要插入值的项,后面的values输入值要和前面指定项的顺序相同 Query OK, 1 row affected (0.00 sec) mysql> insert into bbb values(3,"lisi","girl","bbb","18"),(4,"wangwu","man","ccc","22"); #批量插入数据,中间使用,隔开 Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into bbb (id,name) values(5,"renliu"),(6,"zhangqi"); #批量插入数据,中间使用,隔开 Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from bbb; #查看表是否成功添加,NULL表示为空 +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 1 | rzy | man | aaa | 20 | | 2 | zhangsan | NULL | NULL | NULL | | 3 | lisi | girl | bbb | 18 | | 4 | wangwu | man | ccc | 22 | | 5 | renliu | NULL | NULL | NULL | | 6 | zhangqi | NULL | NULL | NULL | +------+----------+------+------+------+ 6 rows in set (0.00 sec)
-查看表中数据
mysql> select * from bbb; #查看表中的所有数据 +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 1 | rzy | man | aaa | 20 | | 2 | zhangsan | NULL | NULL | NULL | | 3 | lisi | girl | bbb | 18 | | 4 | wangwu | man | ccc | 22 | | 5 | renliu | NULL | NULL | NULL | | 6 | zhangqi | NULL | NULL | NULL | +------+----------+------+------+------+ 6 rows in set (0.00 sec) mysql> select abc from bbb; #只查看表中的abc项的数据 +------+ | abc | +------+ | aaa | | NULL | | bbb | | ccc | | NULL | | NULL | +------+ 6 rows in set (0.00 sec) mysql> select * from bbb where name="rzy"; #只查看表中name为rzy的数据 +------+------+------+------+------+ | id | name | sex | abc | age | +------+------+------+------+------+ | 1 | rzy | man | aaa | 20 | +------+------+------+------+------+ 1 row in set (0.00 sec) mysql> select sex from bbb where name="zhangsan"; #只查看表中的sex项,并且只查看name为zhangsan的数据 +------+ | sex | +------+ | NULL | +------+ 1 row in set (0.00 sec)
-更新(修改)表中的数据
mysql> select * from bbb; #查看bbb表中的所有数据 +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 1 | rzy | man | aaa | 20 | | 2 | zhangsan | NULL | NULL | NULL | | 3 | lisi | girl | bbb | 18 | | 4 | wangwu | man | ccc | 22 | | 5 | renliu | NULL | NULL | NULL | | 6 | zhangqi | NULL | NULL | NULL | +------+----------+------+------+------+ 6 rows in set (0.00 sec) mysql> update bbb set age=100; #修改bbb表中的age项全部为100 Query OK, 6 rows affected (0.01 sec) Rows matched: 6 Changed: 6 Warnings: 0 mysql> select * from bbb; #再次查看bbb表的所有信息,发现成功修改 +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 1 | rzy | man | aaa | 100 | | 2 | zhangsan | NULL | NULL | 100 | | 3 | lisi | girl | bbb | 100 | | 4 | wangwu | man | ccc | 100 | | 5 | renliu | NULL | NULL | 100 | | 6 | zhangqi | NULL | NULL | 100 | +------+----------+------+------+------+ 6 rows in set (0.00 sec) mysql> update bbb set age=10 where name="rzy"; #修改bbb表中的age项为10并且只限于name为rzy的行 Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from bbb; #再次查看bbb表的所有信息,发现成功修改 +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 1 | rzy | man | aaa | 10 | | 2 | zhangsan | NULL | NULL | 100 | | 3 | lisi | girl | bbb | 100 | | 4 | wangwu | man | ccc | 100 | | 5 | renliu | NULL | NULL | 100 | | 6 | zhangqi | NULL | NULL | 100 | +------+----------+------+------+------+ 6 rows in set (0.00 sec)
-删除表中的数据
mysql> select * from bbb; #查看表中的所有数据 +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 1 | rzy | man | aaa | 10 | | 2 | zhangsan | NULL | NULL | 100 | | 3 | lisi | girl | bbb | 100 | | 4 | wangwu | man | ccc | 100 | | 5 | renliu | NULL | NULL | 100 | | 6 | zhangqi | NULL | NULL | 100 | +------+----------+------+------+------+ 6 rows in set (0.00 sec) mysql> delete from bbb where id="1"; #删除bbb表中id项为1的行 Query OK, 1 row affected (0.00 sec) mysql> select * from bbb; +------+----------+------+------+------+ | id | name | sex | abc | age | +------+----------+------+------+------+ | 2 | zhangsan | NULL | NULL | 100 | | 3 | lisi | girl | bbb | 100 | | 4 | wangwu | man | ccc | 100 | | 5 | renliu | NULL | NULL | 100 | | 6 | zhangqi | NULL | NULL | 100 | +------+----------+------+------+------+ 5 rows in set (0.00 sec) mysql> delete from bbb where abc is NULL; #删除bbb表中abc项为null空的行 Query OK, 3 rows affected (0.00 sec) mysql> select * from bbb; +------+--------+------+------+------+ | id | name | sex | abc | age | +------+--------+------+------+------+ | 3 | lisi | girl | bbb | 100 | | 4 | wangwu | man | ccc | 100 | +------+--------+------+------+------+ 2 rows in set (0.00 sec) mysql> delete from bbb; #删除bbb表中的所有数据 Query OK, 2 rows affected (0.00 sec) mysql> select * from bbb; #查看bbb表发现已经没有数据了 Empty set (0.00 sec) mysql> desc bbb; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(10) | YES | | NULL | | | sex | char(10) | YES | | NULL | | | abc | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 5 rows in set (0.00 sec)