存储过程和用户权限,创建和修改数据表

一、联结表

  数据仍使用前文中的数据。

一.CREATE语句(创建)

假如现在有表结构:

  1、子查询

  作为子查询的SELECT语句只能查询单个列。企图检索多个列将返回错误。

-- 作为查询条件使用
-- 查看TNT2订单对应的客户ip(order表)
SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM orderitems WHERE prod_id='TNT2');
-- 根据TNT2订单的客户ip查看客户信息(cust表)
SELECT cust_name, cust_contact FROM customers WHERE cust_id IN (SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM orderitems WHERE prod_id='TNT2'));
-- 作为计算字段使用
-- 计算每个客户的订单数据
SELECT cust_name, cust_state, (SELECT COUNT(*) FROM orders WHERE orders.cust_id = customers.cust_id) AS orders FROM customers ORDER BY cust_name;

1.创建DataBase

图片 1

CREATE TABLE `msg_detail` (
    `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
    `title` VARCHAR(200) NOT NULL DEFAULT '' COMMENT '标题',
    `content` TEXT NOT NULL COMMENT '内容',
    `audio_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '音频地址',
    `type_id` TINYINT(4) NOT NULL DEFAULT '0',
     PRIMARY KEY (`id`),
    INDEX `cid` (`cid`) USING BTREE,
    INDEX `column_id` (`column_id`) USING BTREE
)
COMMENT='内参表'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1;

  2、创建链接

 图片示例  链接方式 关键字 语句示例
  内连接 INNER JOIN … ON SELECT <select_list> FROM A INNER JOIN B ON A.key = B.key
左外连接 LEFT JOIN … ON SELECT <select_list> FROM A LEFT JOIN B ON A.key = B.key
左外连接 LEFT JOIN … ON … WHERE B.key IS NULL SELECT <select_list> FROM A LEFT JOIN B ON A.key = B.key WHERE B.key IS NULL
右外连接 RIGHT JOIN … ON SELECT <select_list> FROM A RIGHT JOIN B ON A.key = B.key
右外连接 RIGHT JOIN … ON … WHERE A.key IS NULL SELECT <select_list> FROM A RIGHT JOIN B ON A.key = B.key WHERE A.key IS NULL
全外连接 UNION

SELECT <select_list> FROM A LEFT JOIN B ON A.key = B.key

UNION

SELECT <select_list> FROM A RIGHT JOIN B ON A.key = B.key

全外连接 (WHER IS NULL) UNION … (WHER IS NULL)

SELECT <select_list> FROM A LEFT JOIN B ON A.key = B.key WHERE B.key IS NULL

UNION

SELECT <select_list> FROM A RIGHT JOIN B ON A.key = B.key WHERE A.key IS NULL

   用例:

-- 笛卡尔积:n * m
-- SELECT * FROM vendors, products;
SELECT orders.cust_id, cust_name FROM orders CROSS JOIN customers;-- 没有WHERE子句
-- 内连接
-- SELECT * FROM vendors, products WHERE vendors.vend_id = products.vend_id;
SELECT * FROM vendors INNER JOIN products on vendors.vend_id = products.vend_id;
-- 左外连接(a),根据A表相应字段取列
SELECT orders.cust_id, cust_name FROM orders LEFT JOIN customers on customers.cust_id = orders.cust_id;
-- 左外连接b,只取A表中无B表相应字段的列
SELECT vendors.vend_id, vend_name, vend_country  FROM vendors LEFT JOIN products on vendors.vend_id = products.vend_id where products.vend_id IS NULL ORDER BY vend_id;
-- 右外连接(a),同左外连接(a)
SELECT orders.cust_id, cust_name FROM orders RIGHT JOIN customers on customers.cust_id = orders.cust_id;
-- 右外连接(b),同左外连接(b)
SELECT customers.cust_id, cust_name FROM orders RIGHT JOIN customers on customers.cust_id = orders.cust_id WHERE orders.cust_id IS NULL;

-- 全外连接,两者id相等的行和不相等的行都保留
SELECT customers.cust_id, orders.order_num FROM orders LEFT JOIN customers on customers.cust_id = orders.cust_id
UNION
SELECT customers.cust_id, orders.order_num FROM orders RIGHT JOIN customers on customers.cust_id = orders.cust_id;
-- 全外连接,只保留两者id不相等的行,这里两个WHERE子句一样是因为customers的id字段完全包含orders中的字段
SELECT customers.cust_id, orders.order_num FROM orders LEFT JOIN customers on customers.cust_id = orders.cust_id WHERE orders.cust_id IS NULL
UNION
SELECT customers.cust_id, orders.order_num FROM orders RIGHT JOIN customers on customers.cust_id = orders.cust_id WHERE orders.cust_id IS NULL

1.CONTAINMENT

  SQL Server 2012 新功能 , 默认值是OFF 。(太高级
书上也没有详细介绍)。

 

  3、视图

  视图是虚拟的表。它只包含使用时动态检索数据的查询,换言之,视图存储查询语句。保存查询语句可以使用视图。视图的一些规则:

  - 与表名一样,视图必须唯一命名。

  - 视图数目没有限制。

  - 为了创建视图,必须具有足够的访问权限。

  - 视图可以嵌套。

  - 视图查询中不要使用GROUP BY。

  - 视图不能索引,也不能有关联的触发器或默认值。

-- 创建视图
-- CREATE VIEW viewname AS SELECT *
CREATE VIEW productcustomers AS SELECT cust_name, cust_contact, prod_id FROM customers, orders, orderitems WHERE customers.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num;

CREATE VIEW orderitemsexpanded AS SELECT order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM orderitems;

-- 执行视图查询时会首先执行视图
SELECT cust_name, cust_contact FROM productcustomers WHERE prod_id ='FB';
SELECT * FROM orderitemsexpanded WHERE order_num = 20009;
-- 删除视图
-- DROP VIEW viewname;
DROP VIEW productcustomers;

2.ON

  ON用于两个地方,第一个是存储数据的文件的位置,第二个是存储日志的文件的位置。
ON 后面的 PRIMARY的概念:希望将所有的内容存放在一个文件里。

  查询所有类型内参最大id,可用如下sql: 

二、存储过程

3.NAME

  一个逻辑名称,即SQL
Server在内部使用该名称引用该文件。当需要修改数据库大小时,需要使用这个名称

 select
max(id),type_id from msg_detail group by type_id; 

  1、存储过程

4.FILENAME

  实际的操作系统文件在磁盘的名字,如果不写默认放在安装SQL
Server的文件夹中,默认的数据库文件是.mdf后缀,日志是.ldf文件。

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

标签:
网站地图xml地图