Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3041911
  • 博文数量: 167
  • 博客积分: 613
  • 博客等级: 中士
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-13 21:35
个人简介

人, 既无虎狼之爪牙,亦无狮象之力量,却能擒狼缚虎,驯狮猎象,无他,唯智慧耳。

文章分类
文章存档

2015年(19)

2014年(70)

2013年(54)

2012年(14)

2011年(10)

分类: Mysql/postgreSQL

2014-01-02 11:01:08

     告别2013,进入崭新的2014,自己决定将数据库作为自己新的一年的工作学习的开始。工作中不可避免地要和数据库打交道,自己之前参加青鸟培训的时候曾经接触过微软的VS中自带的SqlServer2005,那个时候也简单地写过一些C#的数据库程序,但是之后没有继续使用,因而很快就都忘记了。所以现在需要重新来复习一下,为下一步的工作做好准备。今天先来从基本的sql语句开始吧,不针对特定平台,也不会详细地列出语句用法,主要针对自己之前遗忘或者没有接触掌握的内容做些梳理。

一、构建测试用数据库
     这里自己学习的材料是经典教材《Sql必知必会》,对于自己熟悉常用的sql命令足够了,计划用两到三天的时间来完成这个部分的学习。为了随时验证所学的东西,这里采用书中提供的数据库作为测试用例。官方提供的是mdb格式的文件,默认应当使用Access来打开,但是自己安装了Ubuntu的虚拟环境,配置了MySql,因此决定利用脚本在MySql中搭建数据库,方法比较简单,执行创建初始化脚本即可:
Create脚本:

点击(此处)折叠或打开

  1. --------------------------------------------
  2. -- Sams Teach Yourself SQL in 10 Minutes
  3. --
  4. -- Example table creation scripts for MySQL.
  5. --------------------------------------------


  6. -------------------------
  7. -- Create Customers table
  8. -------------------------
  9. CREATE TABLE Customers
  10. (
  11.   cust_id char(10) NOT NULL ,
  12.   cust_name char(50) NOT NULL ,
  13.   cust_address char(50) NULL ,
  14.   cust_city char(50) NULL ,
  15.   cust_state char(5) NULL ,
  16.   cust_zip char(10) NULL ,
  17.   cust_country char(50) NULL ,
  18.   cust_contact char(50) NULL ,
  19.   cust_email char(255) NULL
  20. );

  21. --------------------------
  22. -- Create OrderItems table
  23. --------------------------
  24. CREATE TABLE OrderItems
  25. (
  26.   order_num int NOT NULL ,
  27.   order_item int NOT NULL ,
  28.   prod_id char(10) NOT NULL ,
  29.   quantity int NOT NULL ,
  30.   item_price decimal(8,2) NOT NULL
  31. );


  32. ----------------------
  33. -- Create Orders table
  34. ----------------------
  35. CREATE TABLE Orders
  36. (
  37.   order_num int NOT NULL ,
  38.   order_date datetime NOT NULL ,
  39.   cust_id char(10) NOT NULL
  40. );

  41. ------------------------
  42. -- Create Products table
  43. ------------------------
  44. CREATE TABLE Products
  45. (
  46.   prod_id char(10) NOT NULL ,
  47.   vend_id char(10) NOT NULL ,
  48.   prod_name char(255) NOT NULL ,
  49.   prod_price decimal(8,2) NOT NULL ,
  50.   prod_desc text NULL
  51. );

  52. -----------------------
  53. -- Create Vendors table
  54. -----------------------
  55. CREATE TABLE Vendors
  56. (
  57.   vend_id char(10) NOT NULL ,
  58.   vend_name char(50) NOT NULL ,
  59.   vend_address char(50) NULL ,
  60.   vend_city char(50) NULL ,
  61.   vend_state char(5) NULL ,
  62.   vend_zip char(10) NULL ,
  63.   vend_country char(50) NULL
  64. );


  65. ----------------------
  66. -- Define primary keys
  67. ----------------------
  68. ALTER TABLE Customers ADD PRIMARY KEY (cust_id);
  69. ALTER TABLE OrderItems ADD PRIMARY KEY (order_num, order_item);
  70. ALTER TABLE Orders ADD PRIMARY KEY (order_num);
  71. ALTER TABLE Products ADD PRIMARY KEY (prod_id);
  72. ALTER TABLE Vendors ADD PRIMARY KEY (vend_id);


  73. ----------------------
  74. -- Define foreign keys
  75. ----------------------
  76. ALTER TABLE OrderItems ADD CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (order_num) REFERENCES Orders (order_num);
  77. ALTER TABLE OrderItems ADD CONSTRAINT FK_OrderItems_Products FOREIGN KEY (prod_id) REFERENCES Products (prod_id);
  78. ALTER TABLE Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
  79. ALTER TABLE Products ADD CONSTRAINT FK_Products_Vendors FOREIGN KEY (vend_id) REFERENCES Vendors (vend_id);
之后配置Populate脚本:

点击(此处)折叠或打开

  1. ----------------------------------------------
  2. -- Sams Teach Yourself SQL in 10 Minutes
  3. --
  4. -- Example table population scripts for MySQL.
  5. ----------------------------------------------


  6. ---------------------------
  7. -- Populate Customers table
  8. ---------------------------
  9. INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
  10. VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
  11. INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
  12. VALUES('1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green');
  13. INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
  14. VALUES('1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'jjones@fun4all.com');
  15. INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
  16. VALUES('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@fun4all.com');
  17. INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
  18. VALUES('1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard');

  19. -------------------------
  20. -- Populate Vendors table
  21. -------------------------
  22. INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
  23. VALUES('BRS01','Bears R Us','123 Main Street','Bear Town','MI','44444', 'USA');
  24. INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
  25. VALUES('BRE02','Bear Emporium','500 Park Street','Anytown','OH','44333', 'USA');
  26. INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
  27. VALUES('DLL01','Doll House Inc.','555 High Street','Dollsville','CA','99999', 'USA');
  28. INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
  29. VALUES('FRB01','Furball Inc.','1000 5th Avenue','New York','NY','11111', 'USA');
  30. INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
  31. VALUES('FNG01','Fun and Games','42 Galaxy Road','London', NULL,'N16 6PS', 'England');
  32. INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
  33. VALUES('JTS01','Jouets et ours','1 Rue Amusement','Paris', NULL,'45678', 'France');

  34. --------------------------
  35. -- Populate Products table
  36. --------------------------
  37. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  38. VALUES('BR01', 'BRS01', '8 inch teddy bear', 5.99, '8 inch teddy bear, comes with cap and jacket');
  39. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  40. VALUES('BR02', 'BRS01', '12 inch teddy bear', 8.99, '12 inch teddy bear, comes with cap and jacket');
  41. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  42. VALUES('BR03', 'BRS01', '18 inch teddy bear', 11.99, '18 inch teddy bear, comes with cap and jacket');
  43. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  44. VALUES('BNBG01', 'DLL01', 'Fish bean bag toy', 3.49, 'Fish bean bag toy, complete with bean bag worms with which to feed it');
  45. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  46. VALUES('BNBG02', 'DLL01', 'Bird bean bag toy', 3.49, 'Bird bean bag toy, eggs are not included');
  47. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  48. VALUES('BNBG03', 'DLL01', 'Rabbit bean bag toy', 3.49, 'Rabbit bean bag toy, comes with bean bag carrots');
  49. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  50. VALUES('RGAN01', 'DLL01', 'Raggedy Ann', 4.99, '18 inch Raggedy Ann doll');
  51. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  52. VALUES('RYL01', 'FNG01', 'King doll', 9.49, '12 inch king doll with royal garments and crown');
  53. INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
  54. VALUES('RYL02', 'FNG01', 'Queen doll', 9.49, '12 inch queen doll with royal garments and crown');

  55. ------------------------
  56. -- Populate Orders table
  57. ------------------------
  58. INSERT INTO Orders(order_num, order_date, cust_id)
  59. VALUES(20005, '2004-05-01', '1000000001');
  60. INSERT INTO Orders(order_num, order_date, cust_id)
  61. VALUES(20006, '2004-01-12', '1000000003');
  62. INSERT INTO Orders(order_num, order_date, cust_id)
  63. VALUES(20007, '2004-01-30', '1000000004');
  64. INSERT INTO Orders(order_num, order_date, cust_id)
  65. VALUES(20008, '2004-02-03', '1000000005');
  66. INSERT INTO Orders(order_num, order_date, cust_id)
  67. VALUES(20009, '2004-02-08', '1000000001');

  68. ----------------------------
  69. -- Populate OrderItems table
  70. ----------------------------
  71. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  72. VALUES(20005, 1, 'BR01', 100, 5.49);
  73. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  74. VALUES(20005, 2, 'BR03', 100, 10.99);
  75. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  76. VALUES(20006, 1, 'BR01', 20, 5.99);
  77. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  78. VALUES(20006, 2, 'BR02', 10, 8.99);
  79. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  80. VALUES(20006, 3, 'BR03', 10, 11.99);
  81. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  82. VALUES(20007, 1, 'BR03', 50, 11.49);
  83. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  84. VALUES(20007, 2, 'BNBG01', 100, 2.99);
  85. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  86. VALUES(20007, 3, 'BNBG02', 100, 2.99);
  87. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  88. VALUES(20007, 4, 'BNBG03', 100, 2.99);
  89. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  90. VALUES(20007, 5, 'RGAN01', 50, 4.49);
  91. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  92. VALUES(20008, 1, 'RGAN01', 5, 4.99);
  93. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  94. VALUES(20008, 2, 'BR03', 5, 11.99);
  95. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  96. VALUES(20008, 3, 'BNBG01', 10, 3.49);
  97. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  98. VALUES(20008, 4, 'BNBG02', 10, 3.49);
  99. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  100. VALUES(20008, 5, 'BNBG03', 10, 3.49);
  101. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  102. VALUES(20009, 1, 'BNBG01', 250, 2.49);
  103. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  104. VALUES(20009, 2, 'BNBG02', 250, 2.49);
  105. INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
  106. VALUES(20009, 3, 'BNBG03', 250, 2.49);
      完成之后我们可以进入到mysql中去查看已经创建好的数据库和表:
     mysql -u username -p > password;
     show databases;
     use database_name;       //这里创建了example数据库作为测试
     show tables;
     desc(ribe) table_name;    //此命令可以查看表结构,如各列的名称和类型等


二、基本的select语句
     在继续介绍基本语句之前,先来列出自己感觉重要的几个要点:
1. DBMS会管理着多个数据库,每个数据库由表组成,表由一条一条的记录构成,每条记录则是不同数据类型的集合;
2. 每个表中都有一个主键,用于唯一标识每条记录,因此要求主键列具有以下属性:
   -‘1-1’对应:每条记录都有一个唯一主键,不同的记录主键不同;
   -   不能使用update命令对记录的主键更新修改;
   -   可以delete记录,但是删掉的记录的主键永远不可在表中复用,即新纪录不能使用用过的主键,不管该主键对应的记录是否还存在;
3. 书写规则:
   -   Sql语句不区分大小写,因此select和SELECT效果是一样的,一般为了便于阅读,建议关键词(如SELECT/UPDATE等)大写,其余小写;
   -   Sql语句不区分空格,因此column = 1与column=1是相同的;
   -   Sql语句一般要求使用';'作为每条语句的结束,有些DBMS可能不要求,但是为了统一考虑,一般建议都使用';'作为命令结束;
4. 排序语句:
   -   Order by一般放置在SELECT语句的末尾,对于已经进行过各种条件过滤之后的语句再排序;
   -   Order by默认是升序排列,即数字大小或字母表顺序,可以使用DESC指定为降序,仅仅对向前修饰的最近Order by有效;
   -   Order by可以同时使用多列排序,依次排序,如Order by A, B,C 则先按A排序,A相同的再按B排序,依次类推;
5. 条件过滤-WHERE:
   -   WHERE使用时,如果与字符串关键词比较,必须使用''标识字符串;数字关键字不需要,''仅仅用来表示字符串;
   -   Between A and B范围语句包含起点和终点A和B;
   -   IS NULL用来筛选列值为空的记录;
   -   WHERE column IN (SET);
【基本的语句结构:SELECT   查什么:列名1,列名2,列名...   FROM   从哪查:table_name   WHERE IS NULL/NOT 条件   ORDER BY 指标列集合 DESC】



阅读(2757) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~