一:MySQL简介
    MySQL是一种中型的关系型数据库管理系统(DBMS,DataBase Management System)。
二:使用CMD操作MySQL
    1.进入MySQL环境
      mysql -h Mysql主机地址 -u 用户名 -p
    2.显示数据库
      show databases;
    3.进入数据库
      use 数据库名称;
    4.创建数据库(SQL)
      create database 数据库名称 default character set ‘utf8‘;
    5.查看数据库中的所有表
      show tables;
   
    6.查看数据库创建语句
      show create database 数据库名称;
    7.查看表结构:
      desc 表名称;  (或者describe 表名);
    8.查看表创建语句
      show create table 表名称;
三:常用的SQL语句
    SQL: 结构化查询语言(Structured Query Language)用来操作关系型数据库的标准语言。
    1.创建表
      create table 表名称(
         字段名1  数据类型   [primary key  auto_increment],
         字段名2  数据类型   [not null], 
         ...
         字段名n  数据类型   [约束] 
      );
      注意:
      数值型数据类型: integer (int) 
                      float(有效位数,小数位数)
                      double(有效位数,小数位数)
                      decimal(有效位数,小数位数)
      字符串数据类型:varchar(n)   
                     char(n)
    2.删除表,库
       drop table 表名称;
       drop database 库名称;
    3.插入数据行(记录) 
      insert into 表名称(字段名1,字段名2,...)values(值1,值2,...);
      insert into 表名称 values(值1,值2,...);
      一条SQL插入多条记录:
      insert into 表名称(字段名1,字段名2,...)values(值1,值2,...),(值1,值2,...)...;
      insert into 表名称 values(值1,值2,...),(值1,值2,...)...;
    4.修改数据行(记录)
      update 表名称 set 字段名1=值1 [where 条件] [order by 字段名 [desc]] [limit 修改行数];
    5.删除数据行(记录)
      delete from 表名称 [where 条件] [order by 字段名 [desc]] [limit 删除行数];
      
      如果要删除表中所有数据,除了使用delete from 表名称,还可使用
      truncate table 表名称;   ======》更高效,因为它不返回删除的影响行数。
    6.查询数据
      select 字段名1,字段名2... from 表名称 [where 条件];
      给查询的结果字段起一个别名:
      select 字段名1 as 别名1,字段名2 as 别名2... from 表名称 [where 条件];
    
四:修改数据表的SQL(DDL)
    1.给数据表添加一列(字段)
      alter table 表名称 add column 列名称 数据类型 [约束];
    2.修改数据表中的一列(字段)的数据类型或约束
      alter table 表名称 modify column 列名称 数据类型 [约束];
    3.修改数据表中的列名(字段名)
      alter table 表名称 change column 旧列名称 新列名称 数据类型 [约束];
    4.删除数据表中的列(字段)
      alter table 表名称 drop column 列名称;
     
五:JDBC (Java DataBase Connection)
    1.加载数据库驱动程序   
      Class.forName("xxx.驱动类");
    2.获取数据库连接对象
      Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?characterEncoding=utf8","用户名","密码");
    
    3.获取数据库操作对象
      PreparedStatement ps=conn.prepareStatement("SQL语句,可加?");
      ps.setXXX(1,xxx);
      ps.setXXX(2,xxx);
    4.执行SQL
      如果是insert、delete、update SQL语句:
      int count=ps.executeUpdate(); // 执行insert、delete、update SQL语句,返回影响的行数
      如果是select查询语句
       ResultSet executeQuery()
       ResultSet是带着一个游标的查询结果集(以表的形式存在),最初,游标指向第一行之前。
       while(rs.next()){
           String name=s.getString("结果集中的列名");
           ...
       }
    
原文:http://www.cnblogs.com/mingmng/p/6597435.html