这个专栏用来讲解 MySQL 数据的基本语法和用法。本教程的目的是方便查询 MySQL 的用法,因此对于原理的讲解会偏少一些,但特点就是会有很多实验操作图。
本专栏使用的源材料是《MySQL必知必会》这本书的源代码。
文章目录
- 1. 连接 MySQL 数据库
 - 2. 创建数据库
 - 3. 执行脚本创建表
 - 4. 查看某个表的结构:show命令
 
1. 连接 MySQL 数据库
使用MySQL命令行连接数据库:
mysql -u root -p passwd -h myserver -P 3306
参数的含义:
-u:指定用户名-p:用户名的密码-h:主机名,例如localhost-P:使用指定的端口连接数据库
2. 创建数据库
在 mysql 终端使用命令 create database db_name 创建一个数据库:
 
 这里我们需要创建 crashcourse 数据库。 然后使用这个数据库:use crashcourse:
 
3. 执行脚本创建表
使用某个sql脚本文件创建表:source create.sql。create.sql 文件必须在进入 mysql 命令行时所在的目录。
 
 
4. 查看某个表的结构:show命令
show 命令可以用来显示某个表的所有列:
 
 还有一个更快捷的方法:
 
 使用 help show 可以查看show命令的其它用法。下面只是show命令用法的部分。
 
create.sql 文件中的内容如下:
########################################
# MySQL Crash Course
# http://www.forta.com/books/0672327120/
# Example table creation scripts
########################################
########################
# Create customers table
########################
CREATE TABLE customers
(
  cust_id      int       NOT NULL AUTO_INCREMENT,
  cust_name    char(50)  NOT NULL ,
  cust_address char(50)  NULL ,
  cust_city    char(50)  NULL ,
  cust_state   char(5)   NULL ,
  cust_zip     char(10)  NULL ,
  cust_country char(50)  NULL ,
  cust_contact char(50)  NULL ,
  cust_email   char(255) NULL ,
  PRIMARY KEY (cust_id)
) ENGINE=InnoDB;
#########################
# Create orderitems table
#########################
CREATE TABLE orderitems
(
  order_num  int          NOT NULL ,
  order_item int          NOT NULL ,
  prod_id    char(10)     NOT NULL ,
  quantity   int          NOT NULL ,
  item_price decimal(8,2) NOT NULL ,
  PRIMARY KEY (order_num, order_item)
) ENGINE=InnoDB;
#####################
# Create orders table
#####################
CREATE TABLE orders
(
  order_num  int      NOT NULL AUTO_INCREMENT,
  order_date datetime NOT NULL ,
  cust_id    int      NOT NULL ,
  PRIMARY KEY (order_num)
) ENGINE=InnoDB;
#######################
# Create products table
#######################
CREATE TABLE products
(
  prod_id    char(10)      NOT NULL,
  vend_id    int           NOT NULL ,
  prod_name  char(255)     NOT NULL ,
  prod_price decimal(8,2)  NOT NULL ,
  prod_desc  text          NULL ,
  PRIMARY KEY(prod_id)
) ENGINE=InnoDB;
######################
# Create vendors table
######################
CREATE TABLE vendors
(
  vend_id      int      NOT NULL AUTO_INCREMENT,
  vend_name    char(50) NOT NULL ,
  vend_address char(50) NULL ,
  vend_city    char(50) NULL ,
  vend_state   char(5)  NULL ,
  vend_zip     char(10) NULL ,
  vend_country char(50) NULL ,
  PRIMARY KEY (vend_id)
) ENGINE=InnoDB;
###########################
# Create productnotes table
###########################
CREATE TABLE productnotes
(
  note_id    int           NOT NULL AUTO_INCREMENT,
  prod_id    char(10)      NOT NULL,
  note_date datetime       NOT NULL,
  note_text  text          NULL ,
  PRIMARY KEY(note_id),
  FULLTEXT(note_text)
) ENGINE=MyISAM;
#####################
# Define foreign keys
#####################
ALTER TABLE orderitems ADD CONSTRAINT fk_orderitems_orders FOREIGN KEY (order_num) REFERENCES orders (order_num);
ALTER TABLE orderitems ADD CONSTRAINT fk_orderitems_products FOREIGN KEY (prod_id) REFERENCES products (prod_id);
ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY (cust_id) REFERENCES customers (cust_id);
ALTER TABLE products ADD CONSTRAINT fk_products_vendors FOREIGN KEY (vend_id) REFERENCES vendors (vend_id);
 
小结:
- 登入 mysql 的命令行;
 - 查看有哪些数据库:
show databases; - 使用指定的数据库:
use database_name; - 当前所在数据库有哪些表:
show tables; - 查看一个表的结构:
describe table_name;或者show columns from table_name; 
各位道友,如有收获,记得一键三连呐。
最后,给各位道友介绍一下使用国外虚拟卡开通一些国外服务的渠道,当前我在用的是 wildcard,使用我的注册邀请码 IOQ1YDHH 注册,能得 2 美刀,抵消一部分的开卡费用。同时,我也能得 1 美刀,赠人玫瑰,手有余香啊,哈哈。自己买个 Github Copilot 、ChatGPT Plus 简直不要太香。



















