转载自下面链接,有改动
(1)mysql数据库相关操作
登录mysql,登录当前主机 -h 主机名可以省略
mysql -h 主机名 -u 用户名 -p;
注意,登录数据库后所有命令以分号结尾
----------------------------------------------------------
建立数据库的命令如下:
create database 数据库名称 【其他选项】
create database samp_db character set gbk;
上面命令标识将数据库的字符编码指定为gbk
---------------------------------------------------------
指定数据库进行操作:
use 数据库名称;
-------------------------------------------------------------
建立数据库中的表格
create table 表声明
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
也就是要说清表中
每
一列的作用
已上述的第一行定义为例,id为该列的名称,int为该列的类型,unsigned修饰前面类型 not null表示该列的内容不能为空。auto_increment 表示如果在进行插入数据时,该列为null,则系统自动产生一个比现存最大值大1的值作为该项值,每张表只能有一列定义为auto_increment。primary key表示该列为是表的主键,每个表同样智只能有一个主键。
建立完后输入show tables;显示如下
mysql> show tables;
+-------------------+
| Tables_in_samp_db |
+-------------------+
| students |
+-------------------+
1 row in set (0.00 sec)
---------------------------------------------------------------------------------
向表中插入数据
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
其中方括号中的内容是可选的,例如命令如下:
insert into students values(NULL,“王刚”);
注意在命令行中轻特别注意保持引号为英文引号。
---------------------------------------------------------------------------------------
查看表中数据
mysql> select id,name from students
-> ;
+-----+--------+
| id | name |
+-----+--------+
| 2 | 王刚 |
| 123 | 21 |
+-----+--------+
2 rows in set (0.00 sec)
--------------------------------------------------------------------------------------------
按特定条件查看表中数据
select 列名称 from 表名称 where 条件;
例子如下:
mysql> select * from students where name = "王刚";
+----+--------+
| id | name |
+----+--------+
| 2 | 王刚 |
+----+--------+
1 row in set (0.00 sec)
第一个列名称,(上面的*)表示到找到满足条件的横行时显示该行的哪几列
--------------------------------------------------------------------------------------------------------------
更改表中数据
例子如下:
mysql> update students set name="李娜" where id =123;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from students
-> ;
+-----+--------+
| id | name |
+-----+--------+
| 2 | 王刚 |
| 123 | 李娜 |
+-----+--------+
2 rows in set (0.00 sec)
mysql>
--------------------------------------------------------------------------------------------
删除表中的数据
delete from 表名称 where 删除条件;
--------------------------------------------------------------------------------------------
对表的修改
创建列
alter table 表名 add 列名 列数据类型 [after 插入位置];
实例如下:
mysql> alter table students add age int after name;
Query OK, 2 rows affected (0.23 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from students;
+-----+--------+------+
| id | name | age |
+-----+--------+------+
| 2 | 王刚 | NULL |
| 123 | 李娜 | NULL |
+-----+--------+------+
2 rows in set (0.00 sec)
删除列
alter table 表名 drop 列名称;
重命名表格
alter table 表名 rename 新表名;
删除整张表
drop table 表名;
删除数据库
drop database 数据库名;
------------------------------------------------------------------
其他操作:
修改密码
mysqladmin -u root -p password 新密码
--------------------------------------------------------------------
转载自下面链接,有改动
http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#d7