---
title: "常用SQL语句（在mysql上测验通过）"
author: "wandering192"
date: "2010-09-11T15:59:20Z"
tags: []
categories: ["数据库"]
slug: "sql-mysql"
draft: false
lastmod: "2010-09-11T15:59:20Z"
id: 760926
url: "https://wandering192.iteye.com/blog/760926"
---

本文讲解《数据库处理——基础、设计与实现》第30页中3个表的创建。

1. 创建table
   
   ```
   mysql> create table ORDER_ITEM
       -> (
       -> OrderNumber integer references retail_order(OrderNumber),
       -> SKU integer references SKU_DATA (SKU),
       -> Quantity integer ,
       -> Price decimal(8,2),
       -> ExtendedPrice decimal(9,2)
       -> );
   Query OK, 0 rows affected (0.16 sec)
   ```
2. 创建了表后，要往表里添加数据，在CMD下用insert语句打入数据是相对麻烦的，这里介绍使用引入外部数据的方法。
3. 新建一个.txt文件，文件里直接打入行数据，每一行的所有列数据都要对应到数据库table中的列中，每一行的不同列数据直接用TAB键隔开，不同行之间用换行隔开。

假如表的列数据类型如下

```
+-----------------+----------+------+-----+---------+-------+
| Field           | Type     | Null | Key | Default | Extra |
+-----------------+----------+------+-----+---------+-------+
| SKU             | int(11)  | YES  |     | NULL    |       |
| SKU_Description | char(35) | YES  |     | NULL    |       |
| Department      | char(30) | YES  |     | NULL    |       |
| Buyer           | char(30) | YES  |     | NULL    |       |
+-----------------+----------+------+-----+---------+-------+
```

 对应的txt文件如下，其存放的绝对位置为：C:\\Users\\Gavin\\Desktop\\mysqlDigitalbook\\data\_sku.txt

```
100100	Std,Scuba Tank,Yellow	Water Sports	Pete
100200	Std,Scuba Tank,Magenta	Water Sports	Pete H
101100	DriveMask,Small Clear	Water Sports	Nancy M
101200	DriveMask,Med Clear	Water Sports	Nancy M
```

 那么，执行语句，将“data\_sku.txt”文件里四行的数据写入表“sku\_data”，特别注意在cmd下，路径分隔符是“/”：

```
mysql> load  data local infile 'C:/Users/Gavin/Desktop/mysqlDigitalbook/data_sku
.txt'
    -> into table sku_data;
Query OK, 2 rows affected (0.02 sec)
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0
```