一、MySQL服务器的启动与关闭

1、启动MySQL服务器

    在命令提示符下输入net start mysql 

2、连接MySQL服务器

    mysql -u root -p

3、关闭MySQL服务器

    net stop mysql

二、操作MySQL数据库

1、创建数据库

    create database 数据库名;

2、查看数据库

    show databases;

3、选择指定数据库

    use 数据库名;

4、删除数据库

 drop database 数据库名;

5.退出数据库

  exit

  qiut

  \q

三、操作MySQL数据表

1、创建表

mysql> create table tb1(
    -> id int(11),
    -> name varchar(25),
    -> deptID int(11),
    -> salary float
    -> );

2,SHOW TABLES;

MySQL数据库基本操作

3,查看数据表结构 describe +表名

MySQL数据库基本操作

4,修改数据表结构

    4.1修改表名

        ALTER TABLE 旧表名 RENAME TO 新表名

    列子:alter table tb1 rename to tb2

MySQL数据库基本操作

4.2 修改字段的数据类型

    ALTER TABLE 表名 MODIFY 字段名 数据类型

    alter table tb2 modify name varchar(60)

MySQL数据库基本操作

4.3修改字段名(修改字段名称的同时是可以修改数据类型的

    ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型

    alter table tb2 change name na varchar(40)   // 不想修改数据类型的话将原数据类型写在后面

4.4添加字段

    ALTER TABLE 表名 ADD 新字段名 新数据类型 约束条件 FIRST|AFTER 已存在的字段名

    注意;firstafter已存在字段名用于指定新增字段在表结构中的位置,如果没有这两个参数,默认添加到表的最后一列。这两个是可选参数。

     alter table tb2 add age int(10);

    添加无完整性约束条件的字段

    MySQL数据库基本操作

    添加有完整性约束的字段

     alter table tb2 add year int(10) not null;

    MySQL数据库基本操作

    在指定位置添加字段

     alter table tb2 add month int(12) after id

    MySQL数据库基本操作

4.5删除字段

    ALTER TABLE 表名 DROP 字段名

    alter table tb2 drop age

  MySQL数据库基本操作

4.6修改已存在字段的排列位置

    ALTER TABLE 表名 MODIFY 字段1 数据类型 FIRST|AFTER 字段2

            alter table tb2 modify year int(10) after na

    MySQL数据库基本操作

    修改列的排列位置可以顺便修改列的类型;

    alter table tb2 modify na varchar(20) after id

    MySQL数据库基本操作

6、删除指定数据表

    drop table 表名;

五、操作MySQL数据

1、添加表数据

语法1:insert into 表名 values(值1,值2,...)(自增长的列应写null)

语法2:insert into 表名(字段1,字段2,...) values (值1,值2,...)

语法3:insert into 表名 set 字段1=值1,字段2=值2,...

eg: insert into tb2 values(1,"judy",3,2012,15,1800)

MySQL数据库基本操作

2、更新表数据

update 表名 set 字段1=值1 where 查询条件

若无查询条件,表中所有数据行都会被修改。

 update tb2 set na="zhou" where na=2

MySQL数据库基本操作

update tb2 set id=1,month=4,year=2015,salary=1900 where na="zhou"

MySQL数据库基本操作

3、删除表数据

delete from 表名 where 查询条件

若无查询条件,表中所有数据行都会被删除。

 delete from tb2 where na="zhou"

MySQL数据库基本操作


4、查询表数据

select * from 表名;

5、限制查询记录数

select * from 表名 limit[start] length

start:表示从第几行记录开始输出,0表示第1行




    


    

    

    

    






相关文章: