步骤:

  1. 使用show语句找出在服务器上当前存在什么数据库:

mysql>show databases;


  1. 创建一个数据库test:

mysql>create database test;


  1. 选择你所创建的数据库:

mysql>use test;


4创建一个数据表:

首先查看刚才创建的数据库中存在什么表:

mysql>show tables;


(说明刚才创建的数据库中还没有数据库表)

接着我们创建一个关于students的数据表:包括学生的学号(id),姓名(name),性别(sex),年龄(age)。

mysql>create table students(id int unsigned notnull auto_increment primary key,name char(8) not null,sex char(4) not null,agetinyint unsigned not null,);


解释:以 "id int unsigned not null auto_increment primary key" 行进行介绍:

"id" 为列的名称;

"int" 指定该列的类型为 int(取值范围为 -8388608到8388607), 在后面我们又用 "unsigned" 加以修饰, 表示该类型为无符号型, 此时该列的取值范围为 0到16777215;

"not null" 说明该列的值不能为空, 必须要填, 如果不指定该属性, 默认可为空;

"auto_increment" 需在整数列中使用, 其作用是在插入数据时若该列为 NULL, MySQL将自动产生一个比现存值更大的唯一标识符值。在每张表中仅能有一个这样的值且所在列必须为索引列。

"primary key" 表示该列是表的主键, 本列的值必须唯一, MySQL将自动索引该列。

下面的 char(8) 表示存储的字符长度为8, tinyint的取值范围为 -127到128, default 属性指定当该列值为空时的默认值。

 

创建一个表后,用show tables显示数据库中有哪些表:

mysql>show tables;


  1. 显示表结构:

mysql>describe students;


  1. 在表中添加记录:

首先用select命令来查看表中的数据:

mysql>select*from students;


(说明刚才创建的数据库表中还没有任何记录)

接着加入一条新纪录:

mysql>insert into studentsvalue(‘01’,’Tom’,’F’,’18’);


 

 

 

再用select命令来查看表中的数据的变化:

mysql>select*from students;


  1. 用文本方式将数据装入一个数据库表:

创建一个文本文件“student.sql”,每行包括一个记录,用TAB键把值分开,并且以在create table语句中列出的次序,例如:

02   Tony    F    18

03   Amy    M    18

04   Lisa    M    18

将文本文件“student.sql”装载到students表中:

mysql>load data localinfile”e:\\student.sql”into table students;


再使用select命令来查看表中的数据的变化:

mysql>select*from students;