MySQL之存储过程、存储函数、备份还原、索引及视图

-- 创建统计记录条数的存储函数count_sch()

DELIMITER //
CREATE FUNCTION count_sch()
  RETURNS INT
  BEGIN
    DECLARE count INT;
    SELECT COUNT(*) INTO count FROM sch;
    RETURN count;
  END //
DELIMITER ;

-- 创建存储过程avg_sai()

DELIMITER //
CREATE PROCEDURE avg_sai(IN deptno INT, IN job VARCHAR(50), OUT avg_salary INT)
  BEGIN
    SELECT AVG(salary) INTO avg_salary FROM emp WHERE d.no = deptno AND j.ob = job;
  END //
DELIMITER ;

一、备份与还原

/***************************样例表***************************/
CREATE DATABASE booksDB;
use booksDB;

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);
boo    
/***************************样例表***************************/

1、使用mysqldump命令备份数据库中的所有表

[root@node2 mysql_databases]#mysqldump -uroot -p123456 booksDB books > /backup/mysql_databases/booksDB_books.sql

2、备份booksDB数据库中的books表

[root@node2 mysql_databases]#mysqldump -uroot -p123456 booksDB books > /backup/mysql_databases/booksDB_books.sql

3、使用mysqldump备份booksDB和test数据库

[root@node2 mysql_databases]#mysqldump -uroot -p123456 --databases  booksDB test > /backup/mysql_databases/db-booksDB-test.sql

4、使用mysqldump备份服务器中的所有数据库

[root@node2 mysql_databases]#mysqldump -uroot -p123456 -A --routines > /backup/mysql_databases/all_databasesA.sql

5、使用mysql命令还原第二题导出的book表

[root@node2 mysql_databases]# mysql -uroot -p'123456' booksDB books < booksDB_books.sql 

6、进入数据库使用source命令还原第二题导出的book表

mysql> create database booksDB;
mysql> use booksDB;
mysql> source /backup/mysql_databases/booksDB_books.sql

二、索引

1、建立一个utf8编码的数据库test1

mysql> CREATE DATABASE test1 CHARACTER SET utf8 COLLATE utf8_general_ci;

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)
mysql> USE test1;
Database changed
mysql> CREATE TABLE goods
    -> (
    ->   goods_id   INT(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->   goods_name VARCHAR(20)  NOT NULL,
    ->   cat_id     INT(11)      NOT NULL DEFAULT 0,
    ->   brand_id   INT(11)      NOT NULL DEFAULT 0,
    ->   goods_sn   CHAR(12)     NOT NULL,
    ->   shop_price FLOAT(6,2)   NOT NULL DEFAULT 0.00,
    ->   goods_desc TEXT         DEFAULT NULL
    -> ) ENGINE=MyISAM DEFAULT CHARACTER SET=utf8;

  parent_id INT(11)      NOT NULL DEFAULT 0
) ENGINE=MyISAM DEFAULT CHARACTER SET=utf8;Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> CREATE TABLE category
    -> (
    ->   cat_id    INT(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->   cate_name VARCHAR(20)  NOT NULL,
    ->   parent_id INT(11)      NOT NULL DEFAULT 0
    -> ) ENGINE=MyISAM DEFAULT CHARACTER SET=utf8;
Query OK, 0 rows affected (0.00 sec)

3、删除 goods 表中的 goods_desc 和 goods_sn 字段,并增加 click_count 字段:

mysql> ALTER TABLE goods
    -> DROP COLUMN goods_desc,
    -> DROP COLUMN goods_sn,
    -> ADD COLUMN click_count INT AFTER brand_id;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

4、在 goods_name 列上加唯一性索引(用 alter table 方式):

mysql> ALTER TABLE goods
    -> ADD UNIQUE INDEX idx_goods_name (goods_name);

5、在 shop_price 列上加普通索引(用 create index 方式):

mysql>  CREATE INDEX idx_shop_price ON goods(shop_price);

6、在 click_count 上增加普通索引,然后再删除(分别使用 drop index 和 alter table 删除):

mysql> ALTER TABLE goods
    -> ADD INDEX idx_click_count (click_count);

mysql> ALTER TABLE goods
    -> DROP INDEX idx_click_count;

三、视图 学生表:Student (Sno, Sname, Ssex , Sage, Sdept) 学号,姓名,性别,年龄,所在系 Sno为主键 课程表:Course (Cno, Cname,) 课程号,课程名 Cno为主键 学生选课表:SC (Sno, Cno, Score) 学号,课程号,成绩 Sno,Cno为主键

1、创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。

CREATE VIEW stu_info AS SELECT S.Sname, S.Ssex, C.Cname, SC.Score FROM Student S JOIN SC ON S.Sno = SC.Sno JOIN Course C ON C.Cno = SC.Cno;

2、删除视图 stu_info。

DROP VIEW stu_info;

你可能感兴趣的:(sql,数据库,mysql)