Create 创建数据库

示例数据

到目前为止,在本教程中,我们已经在 PostgreSQL 数据库中使用了一个非常小而简单的表。

现在,我们希望创建更多具有更多内容的表,以便能够展示更多数据库功能。

我们将在 PostgreSQL 数据库中创建以下6个表:

下面我们列出了创建这些表所需的所有 SQL 语句和内容。

您不需要在自己的系统上创建表来继续本教程,但您可能会更好地了解 PostgreSQL 和 SQL 语句是如何工作的。

请确保已连接到数据库。如果没有,请按照本教程 SQL Shell 一章中的步骤进行操作。

连接后,就可以编写 SQL 语句了!


CATEGORIES

下面的语句将创建一个名为 categories 的表:

创建表 categories
  1. CREATE TABLE categories (
  2. category_id SERIAL NOT NULL PRIMARY KEY,
  3. category_name VARCHAR(255),
  4. description VARCHAR(255)
  5. );
结果
  1. CREATE TABLE

以下 SQL 语句将用内容填充 categoriescategories:

插入 categories
  1. INSERT INTO categories (category_name, description)
  2. VALUES
  3. ('Beverages', 'Soft drinks, coffees, teas, beers, and ales'),
  4. ('Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'),
  5. ('Confections', 'Desserts, candies, and sweet breads'),
  6. ('Dairy Products', 'Cheeses'),
  7. ('Grains/Cereals', 'Breads, crackers, pasta, and cereal'),
  8. ('Meat/Poultry', 'Prepared meats'),
  9. ('Produce', 'Dried fruit and bean curd'),
  10. ('Seafood', 'Seaweed and fish');
结果
  1. INSERT 0 8

结果可以在这里查看: 示例数据库 - categories


CUSTOMERS

下面的语句将创建一个名为 customers 的表:

创建表 customers
  1. CREATE TABLE customers (
  2. customer_id SERIAL NOT NULL PRIMARY KEY,
  3. customer_name VARCHAR(255),
  4. contact_name VARCHAR(255),
  5. address VARCHAR(255),
  6. city VARCHAR(255),
  7. postal_code VARCHAR(255),
  8. country VARCHAR(255)
  9. );
结果
  1. CREATE TABLE

以下 SQL 语句将用内容填充 customers 表:

插入 customers
  1. INSERT INTO customers (customer_name, contact_name, address, city, postal_code, country)
  2. VALUES
  3. ('Alfreds Futterkiste', 'Maria Anders', 'Obere Str. 57', 'Berlin', '12209', 'Germany'),
  4. ('Ana Trujillo Emparedados y helados', 'Ana Trujillo', 'Avda. de la Constitucion 2222', 'Mexico D.F.', '05021', 'Mexico'),
  5. ('Antonio Moreno Taquera', 'Antonio Moreno', 'Mataderos 2312', 'Mexico D.F.', '05023', 'Mexico'),
  6. ('Around the Horn', 'Thomas Hardy', '120 Hanover Sq.', 'London', 'WA1 1DP', 'UK'),
  7. ('Berglunds snabbkoep', 'Christina Berglund', 'Berguvsvegen 8', 'Lulea', 'S-958 22', 'Sweden'),
  8. ('Blauer See Delikatessen', 'Hanna Moos', 'Forsterstr. 57', 'Mannheim', '68306', 'Germany'),
  9. ('Blondel pere et fils', 'Frederique Citeaux', '24, place Kleber', 'Strasbourg', '67000', 'France'),
  10. ('Bolido Comidas preparadas', 'Martin Sommer', 'C/ Araquil, 67', 'Madrid', '28023', 'Spain'),
  11. ('Bon app', 'Laurence Lebihans', '12, rue des Bouchers', 'Marseille', '13008', 'France'),
  12. ('Bottom-Dollar Marketse', 'Elizabeth Lincoln', '23 Tsawassen Blvd.', 'Tsawassen', 'T2F 8M4', 'Canada'),
  13. ('Bs Beverages', 'Victoria Ashworth', 'Fauntleroy Circus', 'London', 'EC2 5NT', 'UK'),
  14. ('Cactus Comidas para llevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', '1010', 'Argentina'),
  15. ('Centro comercial Moctezuma', 'Francisco Chang', 'Sierras de Granada 9993', 'Mexico D.F.', '05022', 'Mexico'),
  16. ('Chop-suey Chinese', 'Yang Wang', 'Hauptstr. 29', 'Bern', '3012', 'Switzerland'),
  17. ('Comercio Mineiro', 'Pedro Afonso', 'Av. dos Lusiadas, 23', 'Sao Paulo', '05432-043', 'Brazil'),
  18. ('Consolidated Holdings', 'Elizabeth Brown', 'Berkeley Gardens 12 Brewery ', 'London', 'WX1 6LT', 'UK'),
  19. ('Drachenblut Delikatessend', 'Sven Ottlieb', 'Walserweg 21', 'Aachen', '52066', 'Germany'),
  20. ('Du monde entier', 'Janine Labrune', '67, rue des Cinquante Otages', 'Nantes', '44000', 'France'),
  21. ('Eastern Connection', 'Ann Devon', '35 King George', 'London', 'WX3 6FW', 'UK'),
  22. ('Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', '8010', 'Austria'),
  23. ('Familia Arquibaldo', 'Aria Cruz', 'Rua Oros, 92', 'Sao Paulo', '05442-030', 'Brazil'),
  24. ('FISSA Fabrica Inter. Salchichas S.A.', 'Diego Roel', 'C/ Moralzarzal, 86', 'Madrid', '28034', 'Spain'),
  25. ('Folies gourmandes', 'Martine Rance', '184, chaussee de Tournai', 'Lille', '59000', 'France'),
  26. ('Folk och fe HB', 'Maria Larsson', 'Akergatan 24', 'Brecke', 'S-844 67', 'Sweden'),
  27. ('Frankenversand', 'Peter Franken', 'Berliner Platz 43', 'Munchen', '80805', 'Germany'),
  28. ('France restauration', 'Carine Schmitt', '54, rue Royale', 'Nantes', '44000', 'France'),
  29. ('Franchi S.p.A.', 'Paolo Accorti', 'Via Monte Bianco 34', 'Torino', '10100', 'Italy'),
  30. ('Furia Bacalhau e Frutos do Mar', 'Lino Rodriguez ', 'Jardim das rosas n. 32', 'Lisboa', '1675', 'Portugal'),
  31. ('Galeria del gastronomo', 'Eduardo Saavedra', 'Rambla de Cataluna, 23', 'Barcelona', '08022', 'Spain'),
  32. ('Godos Cocina Tipica', 'Jose Pedro Freyre', 'C/ Romero, 33', 'Sevilla', '41101', 'Spain'),
  33. ('Gourmet Lanchonetes', 'Andre Fonseca', 'Av. Brasil, 442', 'Campinas', '04876-786', 'Brazil'),
  34. ('Great Lakes Food Market', 'Howard Snyder', '2732 Baker Blvd.', 'Eugene', '97403', 'USA'),
  35. ('GROSELLA-Restaurante', 'Manuel Pereira', '5th Ave. Los Palos Grandes', 'Caracas', '1081', 'Venezuela'),
  36. ('Hanari Carnes', 'Mario Pontes', 'Rua do Paco, 67', 'Rio de Janeiro', '05454-876', 'Brazil'),
  37. ('HILARION-Abastos', 'Carlos Hernandez', 'Carrera 22 con Ave. Carlos Soublette #8-35', 'San Cristobal', '5022', 'Venezuela'),
  38. ('Hungry Coyote Import Store', 'Yoshi Latimer', 'City Center Plaza 516 Main St.', 'Elgin', '97827', 'USA'),
  39. ('Hungry Owl All-Night Grocers', 'Patricia McKenna', '8 Johnstown Road', 'Cork', '', 'Ireland'),
  40. ('Island Trading', 'Helen Bennett', 'Garden House Crowther Way', 'Cowes', 'PO31 7PJ', 'UK'),
  41. ('Koniglich Essen', 'Philip Cramer', 'Maubelstr. 90', 'Brandenburg', '14776', 'Germany'),
  42. ('La corne d abondance', 'Daniel Tonini', '67, avenue de l Europe', 'Versailles', '78000', 'France'),
  43. ('La maison d Asie', 'Annette Roulet', '1 rue Alsace-Lorraine', 'Toulouse', '31000', 'France'),
  44. ('Laughing Bacchus Wine Cellars', 'Yoshi Tannamuri', '1900 Oak St.', 'Vancouver', 'V3F 2K1', 'Canada'),
  45. ('Lazy K Kountry Store', 'John Steel', '12 Orchestra Terrace', 'Walla Walla', '99362', 'USA'),
  46. ('Lehmanns Marktstand', 'Renate Messner', 'Magazinweg 7', 'Frankfurt a.M. ', '60528', 'Germany'),
  47. ('Lets Stop N Shop', 'Jaime Yorres', '87 Polk St. Suite 5', 'San Francisco', '94117', 'USA'),
  48. ('LILA-Supermercado', 'Carlos Gonzalez', 'Carrera 52 con Ave. Bolivar #65-98 Llano Largo', 'Barquisimeto', '3508', 'Venezuela'),
  49. ('LINO-Delicateses', 'Felipe Izquierdo', 'Ave. 5 de Mayo Porlamar', 'I. de Margarita', '4980', 'Venezuela'),
  50. ('Lonesome Pine Restaurant', 'Fran Wilson', '89 Chiaroscuro Rd.', 'Portland', '97219', 'USA'),
  51. ('Magazzini Alimentari Riuniti', 'Giovanni Rovelli', 'Via Ludovico il Moro 22', 'Bergamo', '24100', 'Italy'),
  52. ('Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', 'B-1180', 'Belgium'),
  53. ('Mere Paillarde', 'Jean Fresniere', '43 rue St. Laurent', 'Montreal', 'H1J 1C3', 'Canada'),
  54. ('Morgenstern Gesundkost', 'Alexander Feuer', 'Heerstr. 22', 'Leipzig', '04179', 'Germany'),
  55. ('North/South', 'Simon Crowther', 'South House 300 Queensbridge', 'London', 'SW7 1RZ', 'UK'),
  56. ('Oceano Atlantico Ltda.', 'Yvonne Moncada', 'Ing. Gustavo Moncada 8585 Piso 20-A', 'Buenos Aires', '1010', 'Argentina'),
  57. ('Old World Delicatessen', 'Rene Phillips', '2743 Bering St.', 'Anchorage', '99508', 'USA'),
  58. ('Ottilies Keseladen', 'Henriette Pfalzheim', 'Mehrheimerstr. 369', 'Koln', '50739', 'Germany'),
  59. ('Paris specialites', 'Marie Bertrand', '265, boulevard Charonne', 'Paris', '75012', 'France'),
  60. ('Pericles Comidas clasicas', 'Guillermo Fernandez', 'Calle Dr. Jorge Cash 321', 'Mexico D.F.', '05033', 'Mexico'),
  61. ('Piccolo und mehr', 'Georg Pipps', 'Geislweg 14', 'Salzburg', '5020', 'Austria'),
  62. ('Princesa Isabel Vinhoss', 'Isabel de Castro', 'Estrada da saude n. 58', 'Lisboa', '1756', 'Portugal'),
  63. ('Que Delicia', 'Bernardo Batista', 'Rua da Panificadora, 12', 'Rio de Janeiro', '02389-673', 'Brazil'),
  64. ('Queen Cozinha', 'Lucia Carvalho', 'Alameda dos Canarios, 891', 'Sao Paulo', '05487-020', 'Brazil'),
  65. ('QUICK-Stop', 'Horst Kloss', 'Taucherstrasse 10', 'Cunewalde', '01307', 'Germany'),
  66. ('Rancho grande', 'Sergio Gutiarrez', 'Av. del Libertador 900', 'Buenos Aires', '1010', 'Argentina'),
  67. ('Rattlesnake Canyon Grocery', 'Paula Wilson', '2817 Milton Dr.', 'Albuquerque', '87110', 'USA'),
  68. ('Reggiani Caseifici', 'Maurizio Moroni', 'Strada Provinciale 124', 'Reggio Emilia', '42100', 'Italy'),
  69. ('Ricardo Adocicados', 'Janete Limeira', 'Av. Copacabana, 267', 'Rio de Janeiro', '02389-890', 'Brazil'),
  70. ('Richter Supermarkt', 'Michael Holz', 'Grenzacherweg 237', 'Genève', '1203', 'Switzerland'),
  71. ('Romero y tomillo', 'Alejandra Camino', 'Gran Via, 1', 'Madrid', '28001', 'Spain'),
  72. ('Santa Gourmet', 'Jonas Bergulfsen', 'Erling Skakkes gate 78', 'Stavern', '4110', 'Norway'),
  73. ('Save-a-lot Markets', 'Jose Pavarotti', '187 Suffolk Ln.', 'Boise', '83720', 'USA'),
  74. ('Seven Seas Imports', 'Hari Kumar', '90 Wadhurst Rd.', 'London', 'OX15 4NB', 'UK'),
  75. ('Simons bistro', 'Jytte Petersen', 'Vinbeltet 34', 'Kobenhavn', '1734', 'Denmark'),
  76. ('Specialites du monde', 'Dominique Perrier', '25, rue Lauriston', 'Paris', '75016', 'France'),
  77. ('Split Rail Beer & Ale', 'Art Braunschweiger', 'P.O. Box 555', 'Lander', '82520', 'USA'),
  78. ('Supremes delices', 'Pascale Cartrain', 'Boulevard Tirou, 255', 'Charleroi', 'B-6000', 'Belgium'),
  79. ('The Big Cheese', 'Liz Nixon', '89 Jefferson Way Suite 2', 'Portland', '97201', 'USA'),
  80. ('The Cracker Box', 'Liu Wong', '55 Grizzly Peak Rd.', 'Butte', '59801', 'USA'),
  81. ('Toms Spezialiteten', 'Karin Josephs', 'Luisenstr. 48', 'Manster', '44087', 'Germany'),
  82. ('Tortuga Restaurante', 'Miguel Angel Paolino', 'Avda. Azteca 123', 'Mexico D.F.', '05033', 'Mexico'),
  83. ('Tradicao Hipermercados', 'Anabela Domingues', 'Av. Ines de Castro, 414', 'Sao Paulo', '05634-030', 'Brazil'),
  84. ('Trails Head Gourmet Provisioners', 'Helvetius Nagy', '722 DaVinci Blvd.', 'Kirkland', '98034', 'USA'),
  85. ('Vaffeljernet', 'Palle Ibsen', 'Smagsloget 45', 'Arhus', '8200', 'Denmark'),
  86. ('Victuailles en stock', 'Mary Saveley', '2, rue du Commerce', 'Lyon', '69004', 'France'),
  87. ('Vins et alcools Chevalier', 'Paul Henriot', '59 rue de l Abbaye', 'Reims', '51100', 'France'),
  88. ('Die Wandernde Kuh', 'Rita Moller', 'Adenauerallee 900', 'Stuttgart', '70563', 'Germany'),
  89. ('Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', '90110', 'Finland'),
  90. ('Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', '08737-363', 'Brazil'),
  91. ('White Clover Markets', 'Karl Jablonski', '305 - 14th Ave. S. Suite 3B', 'Seattle', '98128', 'USA'),
  92. ('Wilman Kala', 'Matti Karttunen', 'Keskuskatu 45', 'Helsinki', '21240', 'Finland'),
  93. ('Wolski', 'Zbyszek', 'ul. Filtrowa 68', 'Walla', '01-012', 'Poland');
结果
  1. INSERT 0 91

结果可以在这里查看: 示例数据库 - orders


PRODUCTS

下面的语句将创建一个名为 products 的表:

创建表 products
  1. CREATE TABLE products (
  2. product_id SERIAL NOT NULL PRIMARY KEY,
  3. product_name VARCHAR(255),
  4. category_id INT,
  5. unit VARCHAR(255),
  6. price DECIMAL(10, 2)
  7. );
结果
  1. CREATE TABLE

以下 SQL 语句将用内容填充 products 表:

插入 products
  1. INSERT INTO products (product_id, product_name, category_id, unit, price)
  2. VALUES
  3. (1, 'Chais', 1, '10 boxes x 20 bags', 18),
  4. (2, 'Chang', 1, '24 - 12 oz bottles', 19),
  5. (3, 'Aniseed Syrup', 2, '12 - 550 ml bottles', 10),
  6. (4, 'Chef Antons Cajun Seasoning', 2, '48 - 6 oz jars', 22),
  7. (5, 'Chef Antons Gumbo Mix', 2, '36 boxes', 21.35),
  8. (6, 'Grandmas Boysenberry Spread', 2, '12 - 8 oz jars', 25),
  9. (7, 'Uncle Bobs Organic Dried Pears', 7, '12 - 1 lb pkgs.', 30),
  10. (8, 'Northwoods Cranberry Sauce', 2, '12 - 12 oz jars', 40),
  11. (9, 'Mishi Kobe Niku', 6, '18 - 500 g pkgs.', 97),
  12. (10, 'Ikura', 8, '12 - 200 ml jars', 31),
  13. (11, 'Queso Cabrales', 4, '1 kg pkg.', 21),
  14. (12, 'Queso Manchego La Pastora', 4, '10 - 500 g pkgs.', 38),
  15. (13, 'Konbu', 8, '2 kg box', 6),
  16. (14, 'Tofu', 7, '40 - 100 g pkgs.', 23.25),
  17. (15, 'Genen Shouyu', 2, '24 - 250 ml bottles', 15.5),
  18. (16, 'Pavlova', 3, '32 - 500 g boxes', 17.45),
  19. (17, 'Alice Mutton', 6, '20 - 1 kg tins', 39),
  20. (18, 'Carnarvon Tigers', 8, '16 kg pkg.', 62.5),
  21. (19, 'Teatime Chocolate Biscuits', 3, '10 boxes x 12 pieces', 9.2),
  22. (20, 'Sir Rodneys Marmalade', 3, '30 gift boxes', 81),
  23. (21, 'Sir Rodneys Scones', 3, '24 pkgs. x 4 pieces', 10),
  24. (22, 'Gustafs Kneckebrod', 5, '24 - 500 g pkgs.', 21),
  25. (23, 'Tunnbrod', 5, '12 - 250 g pkgs.', 9),
  26. (24, 'Guarani Fantastica', 1, '12 - 355 ml cans', 4.5),
  27. (25, 'NuNuCa Nui-Nougat-Creme', 3, '20 - 450 g glasses', 14),
  28. (26, 'Gumber Gummiberchen', 3, '100 - 250 g bags', 31.23),
  29. (27, 'Schoggi Schokolade', 3, '100 - 100 g pieces', 43.9),
  30. (28, 'Rassle Sauerkraut', 7, '25 - 825 g cans', 45.6),
  31. (29, 'Thoringer Rostbratwurst', 6, '50 bags x 30 sausgs.', 123.79),
  32. (30, 'Nord-Ost Matjeshering', 8, '10 - 200 g glasses', 25.89),
  33. (31, 'Gorgonzola Telino', 4, '12 - 100 g pkgs', 12.5),
  34. (32, 'Mascarpone Fabioli', 4, '24 - 200 g pkgs.', 32),
  35. (33, 'Geitost', 4, '500 g', 2.5),
  36. (34, 'Sasquatch Ale', 1, '24 - 12 oz bottles', 14),
  37. (35, 'Steeleye Stout', 1, '24 - 12 oz bottles', 18),
  38. (36, 'Inlagd Sill', 8, '24 - 250 g jars', 19),
  39. (37, 'Gravad lax', 8, '12 - 500 g pkgs.', 26),
  40. (38, 'Cote de Blaye', 1, '12 - 75 cl bottles', 263.5),
  41. (39, 'Chartreuse verte', 1, '750 cc per bottle', 18),
  42. (40, 'Boston Crab Meat', 8, '24 - 4 oz tins', 18.4),
  43. (41, 'Jacks New England Clam Chowder', 8, '12 - 12 oz cans', 9.65),
  44. (42, 'Singaporean Hokkien Fried Mee', 5, '32 - 1 kg pkgs.', 14),
  45. (43, 'Ipoh Coffee', 1, '16 - 500 g tins', 46),
  46. (44, 'Gula Malacca', 2, '20 - 2 kg bags', 19.45),
  47. (45, 'Rogede sild', 8, '1k pkg.', 9.5),
  48. (46, 'Spegesild', 8, '4 - 450 g glasses', 12),
  49. (47, 'Zaanse koeken', 3, '10 - 4 oz boxes', 9.5),
  50. (48, 'Chocolade', 3, '10 pkgs.', 12.75),
  51. (49, 'Maxilaku', 3, '24 - 50 g pkgs.', 20),
  52. (50, 'Valkoinen suklaa', 3, '12 - 100 g bars', 16.25),
  53. (51, 'Manjimup Dried Apples', 7, '50 - 300 g pkgs.', 53),
  54. (52, 'Filo Mix', 5, '16 - 2 kg boxes', 7),
  55. (53, 'Perth Pasties', 6, '48 pieces', 32.8),
  56. (54, 'Tourtiare', 6, '16 pies', 7.45),
  57. (55, 'Pate chinois', 6, '24 boxes x 2 pies', 24),
  58. (56, 'Gnocchi di nonna Alice', 5, '24 - 250 g pkgs.', 38),
  59. (57, 'Ravioli Angelo', 5, '24 - 250 g pkgs.', 19.5),
  60. (58, 'Escargots de Bourgogne', 8, '24 pieces', 13.25),
  61. (59, 'Raclette Courdavault', 4, '5 kg pkg.', 55),
  62. (60, 'Camembert Pierrot', 4, '15 - 300 g rounds', 34),
  63. (61, 'Sirop d arable', 2, '24 - 500 ml bottles', 28.5),
  64. (62, 'Tarte au sucre', 3, '48 pies', 49.3),
  65. (63, 'Vegie-spread', 2, '15 - 625 g jars', 43.9),
  66. (64, 'Wimmers gute Semmelknadel', 5, '20 bags x 4 pieces', 33.25),
  67. (65, 'Louisiana Fiery Hot Pepper Sauce', 2, '32 - 8 oz bottles', 21.05),
  68. (66, 'Louisiana Hot Spiced Okra', 2, '24 - 8 oz jars', 17),
  69. (67, 'Laughing Lumberjack Lager', 1, '24 - 12 oz bottles', 14),
  70. (68, 'Scottish Longbreads', 3, '10 boxes x 8 pieces', 12.5),
  71. (69, 'Gudbrandsdalsost', 4, '10 kg pkg.', 36),
  72. (70, 'Outback Lager', 1, '24 - 355 ml bottles', 15),
  73. (71, 'Flotemysost', 4, '10 - 500 g pkgs.', 21.5),
  74. (72, 'Mozzarella di Giovanni', 4, '24 - 200 g pkgs.', 34.8),
  75. (73, 'Red Kaviar', 8, '24 - 150 g jars', 15),
  76. (74, 'Longlife Tofu', 7, '5 kg pkg.', 10),
  77. (75, 'Rhenbreu Klosterbier', 1, '24 - 0.5 l bottles', 7.75),
  78. (76, 'Lakkalikeeri', 1, '500 ml ', 18),
  79. (77, 'Original Frankfurter gr�ne Soae', 2, '12 boxes', 13);
结果
  1. INSERT 0 77

结果可以在这里查看: 示例数据库 - products


ORDERS

下面的语句将创建一个名为 orders 的表:

创建表 orders
  1. CREATE TABLE orders (
  2. order_id SERIAL NOT NULL PRIMARY KEY,
  3. customer_id INT,
  4. order_date DATE
  5. );
结果
  1. CREATE TABLE

以下 SQL 语句将用内容填充 orders 表:

插入 orders
  1. INSERT INTO orders (order_id, customer_id, order_date)
  2. VALUES
  3. (10248, 90, '2021-07-04'),
  4. (10249, 81, '2021-07-05'),
  5. (10250, 34, '2021-07-08'),
  6. (10251, 84, '2021-07-08'),
  7. (10252, 76, '2021-07-09'),
  8. (10253, 34, '2021-07-10'),
  9. (10254, 14, '2021-07-11'),
  10. (10255, 68, '2021-07-12'),
  11. (10256, 88, '2021-07-15'),
  12. (10257, 35, '2021-07-16'),
  13. (10258, 20, '2021-07-17'),
  14. (10259, 13, '2021-07-18'),
  15. (10260, 55, '2021-07-19'),
  16. (10261, 61, '2021-07-19'),
  17. ...
  18. ...
  19. ...
结果
  1. INSERT 0 830

结果可以在这里查看: 示例数据库 - orders


ORDER_DETAILS

下面的语句将创建一个名为 order_details 的表:

创建表 order_details
  1. CREATE TABLE order_details (
  2. order_detail_id SERIAL NOT NULL PRIMARY KEY,
  3. order_id INT,
  4. product_id INT,
  5. quantity INT
  6. );
结果
  1. CREATE TABLE

以下 SQL 语句将用内容填充 order_details 表:

插入 order_details
  1. INSERT INTO order_details (order_id, product_id, quantity)
  2. VALUES
  3. (10248, 11, 12),
  4. (10248, 42, 10),
  5. (10248, 72, 5),
  6. (10249, 14, 9),
  7. (10249, 51, 40),
  8. (10250, 41, 10),
  9. (10250, 51, 35),
  10. (10250, 65, 15),
  11. (10251, 22, 6),
  12. (10251, 57, 15),
  13. (10251, 65, 20),
  14. (10252, 20, 40),
  15. (10252, 33, 25),
  16. ...
  17. ...
  18. ...
结果
  1. INSERT 0 2155

结果可以在这里查看: 示例数据库 - order_details


TESTPRODUCTS

我们还将在本教程后面添加一个名为 testproducts 的表,用于演示。

下面的 SQL 语句将创建一个名为 testproducts 的表:

创建表 testproducts
  1. CREATE TABLE testproducts (
  2. testproduct_id SERIAL NOT NULL PRIMARY KEY,
  3. product_name VARCHAR(255),
  4. category_id INT
  5. );
结果
  1. CREATE TABLE

我们将在表格中填写 10 个虚拟产品:

插入 testproducts
  1. INSERT INTO testproducts (product_name, category_id)
  2. VALUES
  3. ('Johns Fruit Cake', 3),
  4. ('Marys Healthy Mix', 9),
  5. ('Peters Scary Stuff', 10),
  6. ('Jims Secret Recipe', 11),
  7. ('Elisabeths Best Apples', 12),
  8. ('Janes Favorite Cheese', 4),
  9. ('Billys Home Made Pizza', 13),
  10. ('Ellas Special Salmon', 8),
  11. ('Roberts Rich Spaghetti', 5),
  12. ('Mias Popular Ice', 14);
结果
  1. INSERT 0 10

结果可以在这里查看: 示例数据库 - testproducts