SQL必知必会(二)

来源:互联网 发布:java调用短信接口 编辑:程序博客网 时间:2024/04/30 05:29

39、数据库设计时,相同的数据出现多次绝不是一件好事,关系表的设计就是把信息分解成多个表,一类数据一个表,各个表通过某些共同的值互相关联。

40select vend_name,prod_name,prod_price from Vendors,Products where Vendors.vend_id=Products.vend_id;创建联结。不用where子句,结果返回的是两个表的笛卡尔积。这种基于两个表之间的相等测试的联结叫做内联结。再给出一个联结例子:select prod_name,vend_name,prod_price,quantity from OrderItems,Products,Vendors where Products.vend_id = Vendors.vend_id AND OrderItems.prod_id = Products.prod_id AND order_num = 20007;联结可能非常耗资源,应注意不要联结不必要的表。SQL不限制联结的数量,但是DBMS一般会有限制,应该注意查阅文档。

41select cust_name,cust_contact from Customers As C, Orders As O,OrderItems As OI where C.cust_id = O.cust_id AND OI.order_num = O.order_num AND prod_id = "RGAN01";这里使用的表别名,注意表别名只在查询执行中使用,与列别名不一样,表别名不返回到客户端。

42select c1.cust_id,c1.cust_name,c1.cust_contact from Customers As c1,Customers As c2 where c1.cust_name = c2.cust_name AND c2.cust_contact = 'Jim Jones';这是一个自联结的例子。处理联结比子查询要快的多。

43、标准的联结,即内联结,返回所有的数据,相同的列甚至多次出现(笛卡尔积),自然联结排除多次出现,使每一列只返回一次。这项工作需要自己完成。 

44、还有一个叫做外联结的东西,没看懂。

45、组合查询:执行多个查询,并将结果作为一个查询结果集返回。组合相同表的两个查询所完成的工作与具有多个where子句条件的一个查询所完成的工作相同。  例子:select cust_name,cust_contact,cust_email from Customers where cust_state IN('IL','IN','MI') UNION select cust_name,cust_contact ,cust_email from Customers where cust_name = 'Fun4All';实际上就是两条select组合到了一起。SQL没有限制UNION的条数,但是DBMS可能有限制。

46、使用组合查询的限制,每个查询必须包含相同的列,表达式或聚集函数。UNION会默认不返回重复的行。在UNION后加上ALL,可以返回所有匹配的行。

47、组合查询使用order by时,只能在最后一条select语句中使用,但是DBMS会按order by排序所有结果。

48、插入数据:insert into Customers values('1000000006','Toy Land','123 Any Street','New York','NY','11111','USA',NULL,NULL);INTO是可选的,但为了可移植,最好还是加上。另外,表的结构可能改变,所以,该例子是不安全的,更安全的办法是指定要插入的列名。

49、插入检索出的数据:insert into Customers(cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country) select cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country from CustNew;

50、复制一个新表:create table CustCopy As select * from  Customers;这条语句各数据库可能不太一样,比如有的是使用:select * into CustCopy from Customers;

51、更新数据:update Customers set cust_email = 'kim@thetoystore.com' where cust_id = '1000000005';别忘了set

52、更新多个列:update Customers set cust_contact = 'Sam Roberts',cust_email = 'kim@thetoystore.com' where cust_id = '1000000005';

53、删除Customers表中的一行:delete from Customers where cust_id = '1000000006';delete不需要列名或通配符,delete删除整行而不是阵列。要删除指定的列可以用 update 语句。应该非常小心滴使用updatedelete

54、创建新表时,表名必须不存在,否则会出错:create table ProductsNew (prod_id char(10) not null,vend_id char(10) not null,prod_name char(254) not null,prod_price decimal(8,2) not null,prod_desc varchar(1000) null );如果不指定not null,则默认为null,指定默认值可以用default ,如:create table OrderItemsNew(

order_num Integer not null,

order_item Integer not null,

prod_id char(10) not null,

quantity Integer not null default 1,

item_price decimal(8,2) not null

);默认值经常用于日期或时间戳列。MySQL中的系统日期函数为CUTTENT_DATE()

55、使用Alter table 更新表:alter table Vendors add vend_phone Char(20);alter table Vendors drop column vend_phone;

56、删除表:drop table CustCopy;重命名表:rename table custcopy to custcopy2;

57、创建视图:create view ProductCustomers as select cust_name,cust_contact,prod_id from Customers As C,Orders As O,OrderItems As OI where C.cust_id = O.cust_id AND OI.order_num = O.order_num;各大数据库对视图的支持基本一致。

58、创建存储过程:参考:http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html

DELIMITER //

create procedure proc1(OUT s int)

BEGIN

select count(*) into s from Customers;

END;

//

DELIMITER ;

 

调用存储过程:

set @s = 1;

call proc1(@s);

select @s;

其中DELIMITER用于声明分隔符。

存储过程的删除 drop precodure name1;

59、事务都应该具备ACID特征。所谓ACIDAtomic(原子性),Consistent(一致性),Isolated(隔离性),Durable(持续性)。事务处理是一种机制,用来管理必须成批执行的SQL操作,保证数据库不包含不完整的操作结果。其中,事物指一组SQL语句,回退指撤销指定SQL语句的过程,提交指将未存储的SQL语句结果写入数据库表,保留点指事物处理中设置的临时占位符,可以对它发布回退。可以回退的语句有INSERTUPDATE、和DELETE语句。不能回退SELECTCREATEDROP等语句。不同的DBMS处理事物的办法可能不一致,需要注意查看文档。

60、还有游标,索引,约束,触发器等内容需要详细了解。

0 0