CREATE DATABASE beifen;
use beifen;
CREATE TABLE books
(
bk_id INT NOT NULL PRIMARY KEY,
bk_title VARCHAR(50) NOT NULL,
copyright YEAR NOT NULL
);
INSERT INTO books
VALUES (11078, 'Learning MySQL', 2010),
(11033, 'Study Html', 2011),
(11035, 'How to use php', 2003),
(11072, 'Teach youself javascript', 2005),
(11028, 'Learing C++', 2005),
(11069, 'MySQL professional', 2009),
(11026, 'Guide to MySQL 5.5', 2008),
(11041, 'Inside VC++', 2011);
CREATE TABLE authors
(
auth_id INT NOT NULL PRIMARY KEY,
auth_name VARCHAR(20),
auth_gender CHAR(1)
);
INSERT INTO authors
VALUES (1001, 'WriterX' ,'f'),
(1002, 'WriterA' ,'f'),
(1003, 'WriterB' ,'m'),
(1004, 'WriterC' ,'f'),
(1011, 'WriterD' ,'f'),
(1012, 'WriterE' ,'m'),
(1013, 'WriterF' ,'m'),
(1014, 'WriterG' ,'f'),
(1015, 'WriterH' ,'f');
CREATE TABLE authorbook
(
auth_id INT NOT NULL,
bk_id INT NOT NULL,
PRIMARY KEY (auth_id, bk_id),
FOREIGN KEY (auth_id) REFERENCES authors (auth_id),
FOREIGN KEY (bk_id) REFERENCES books (bk_id)
);
INSERT INTO authorbook
VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);
1、使用mysqldump命令备份数据库中的所有表
[root@coco ~]# mkdir -p /aa/bb 创建随机目录进行选定备份地址
[root@coco ~]# mysqldump -uroot -p beifen > /aa/bb/cc.sql
Enter password: 引用备份语法语句进行对该数据库的三个表进行全局备份
[root@coco ~]# cd /aa/bb
[root@coco bb]#
[root@coco bb]# cat cc.sql
-- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost Database: beifen
-- ------------------------------------------------------
-- Server version 5.7.18
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `authorbook`
--
以下省略
2、备份beifen数据库中的books表
[root@coco ~]# mkdir /aa/cc
[root@coco ~]# mysqldump -uroot -p beifen books > /aa/cc/beifen[books].sql
Enter password:
[root@coco ~]# mkdir /aa/cc
[root@coco ~]# mysqldump -uroot -p beifen books > /aa/cc/beifen[books].sql
Enter password:
[root@coco ~]#
[root@coco ~]# cd /aa/cc
[root@coco cc]# cat beifen[books].sql
-- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost Database: beifen
-- ------------------------------------------------------
-- Server version 5.7.18
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `books`
--
DROP TABLE IF EXISTS `books`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `books` (
`bk_id` int(11) NOT NULL,
`bk_title` varchar(50) NOT NULL,
`copyright` year(4) NOT NULL,
PRIMARY KEY (`bk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `books`
--
3、使用mysqldump备份booksDB和test数据库
[root@coco ~]# mkdir /aa/dd
[root@coco ~]# mysqldump -uroot -p --databases beifen haha > /aa/dd/beifen[haha].sql
Enter password:
[root@coco ~]# cd /aa/dd
[root@coco dd]# cat beifen[haha].sql
-- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost Database: beifen
-- ------------------------------------------------------
-- Server version 5.7.18
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `beifen`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `beifen` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `beifen`;
--
-- Table structure for table `authorbook`
4、使用mysqldump备份服务器中的所有数据库
[root@coco ~]# mysqldump -uroot -p -A > /aa/cc/sql[all].sql
Enter password:
[root@coco ~]#
[root@coco ~]#
[root@coco ~]# cd /aa/cc
[root@coco cc]#
[root@coco cc]# ls
beifen[books].sql sql[all].sql
[root@coco cc]# cat sql[all].sql
-- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 5.7.18
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `beifen`
--
5、使用mysql命令还原第二题导出的book表
恢复备份的语句为:
语法(Syntax):
mysql -u -p < /opt/mytest_bak.sql #库必须保留,空库也可
说明:指定dbname,相当于use
[root@coco ~]# mysql -uroot -p beifen < /aa/cc/beifen[books].sql
Enter password:
[root@coco ~]#
6、进入数据库使用source命令还原第二题导出的book表
首先:删除其数据库的源代码
[root@coco ~]# mysql -uroot -p -e "drop database beifen;"
Enter password:
其次:进入MySQL系统中创建其一个数据库,任意都行
mysql> create database beifen;
Query OK, 1 row affected (0.00 sec)
然后:进行代码导出到新创建的数据库中
[root@coco cc]# mysql -uroot -p beifen < /aa/cc/beifen[books].sql
Enter password:
最后,在进入数据库中,进行source语句恢复备份
mysql> source /aa/cc/beifen[books].sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
代码如下(示例):
二、索引
1、建立一个utf8编码的数据库test1
2、建立商品表goods和栏目表category
按如下表结构创建表:存储引擎engine myisam 字符集charset utf8
mysql> desc goods;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| goods_id | int(11) | NO | PRI | NULL | auto_increment |
| goods_name | varchar(20) | NO | | | |
| cat_id | int(11) | NO | | 0 | |
| brand_id | int(11) | NO | | 0 | |
| goods_sn | char(12) | NO | | | |
| shop_price | float(6,2) | NO | | 0.00 | |
| goods_desc | text | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc category;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| cat_id | int(11) | NO | PRI | NULL | auto_increment |
| cate_name | varchar(20) | NO | | | |
| parent_id | int(11) | NO | | 0 | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段
1、删除goods中的goods_desc和货号字段
mysql> alter table goods drop goods_desc;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table goods drop goods_id;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
2、增加click_count字段
mysql> alter table goods add click_count int not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
4、在 goods_name 列上加唯一性索引(用alter table方式)
1、用alter table命令增加唯一索引
mysql> alter table goods add unique index goodsname (goods_name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
2、输入show index from goods \G查看其表的索引
5、在 shop_price 列上加普通索引(用create index方式)
mysql> create index shopprice on goods(shop_price);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
1、先创建普通索引
mysql> create index clickcount on goods(click_count);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
2、其次查看表的索引状态
Table: goods
Non_unique: 1
Key_name: clickcount
Seq_in_index: 1
Column_name: click_count
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (0.00 sec)
3、成功添加以后,删除索引
①mysql> alter table goods drop index clickcount;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
②mysql> drop index clickcount on goods;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
4、再输入 show index from goods \G查看click_count的索引状态
被删除了....
视图
学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno,Cno为主键
1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
mysql> create view stu_info as select student.sname,student.ssex,course.cno,sc.score from student,sc,course where student.sno=sc.sno and sc.cno==course.cno;
Query OK, 0 rows affected (0.00 sec)
2、删除视图 stu_info。
mysql> drop view stu_info;
Query OK, 0 rows affected (0.00 sec)
mysql> desc stu_info;
ERROR 1146 (42S02): Table 'Student.stu_info' doesn't exist
mysql>