手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL用法----基于Web服务器

[日期:2012-12-02] 来源:Linux社区  作者:林夕jing [字体:大 中 小]

下面以一个实例来解析下数据库这些命令的用法:

要想使用MySQL中这些命令,首先在我们系统上要有和MySQL相关的一些rpmbao

这里我们使用yum安装mysql.i386 、mysql-server.i386(基于Red Hat Enterprise Linux Server release 5.8 )这里yum安装就不多做介绍了

使用service mysqld start 启动数据库服务,

使用mysql命令进入数据库系统,便可进行进一步的操作了:

mysql>drop database if exists mydata; #如果存在mydata则删除

mysql> create database mydata; #建立库mydata

mysql> use mydata; #打开库mydata

Database changed

  1. mysql> use mydata;
  2. Database changed
  3. mysql> create table data (ID int unsigned,Name char(20),Age int unsigned,Gender enum ('Female','Male'),Course char(30)); //建表结束
  4. Query OK, 0 rows affected (0.01 sec)
  5. 注:在建表中(1)data将ID设为长度为整型的数字字段:并且是无符号的
  6. (2)将NAME设为长度为20的字符字段
  7. (3)将Age设为整型的数字字段而且是无符号的
  8. (4)将Gender设为和Male或Female匹配的字符
  9. (5)课程设置长度为30的字符字段。

mysql> desc data; #查看定义的表

  1. +--------+-----------------------+------+-----+---------+-------+
  2. | Field | Type | Null | Key | Default | Extra |
  3. +--------+-----------------------+------+-----+---------+-------+
  4. | ID | int(10) unsigned | YES | | NULL | |
  5. | Name | char(20) | YES | | NULL | |
  6. | Age | int(10) unsigned | YES | | NULL | |
  7. | Gender | enum('Female','Male') | YES | | NULL | |
  8. | Course | char(30) | YES | | NULL | |
  9. +--------+-----------------------+------+-----+---------+-------+
  10. 5 rows in set (0.00 sec)

#以下为在表中插入字段

mysql> insert into data values (1, 'aa', 24, 'Male', 'math');

Query OK, 1 row affected (0.00 sec)

mysql> insert into data values (2, 'bb', 20, 'Female', 'language');

Query OK, 1 row affected (0.00 sec)

按照以上方式插入5条数据信息

mysql> select * from data; #完成后,查看表里的内容:

  1. mysql> select * from data;
  2. +------+------+------+--------+----------+
  3. | ID | Name | Age | Gender | Course |
  4. +------+------+------+--------+----------+
  5. | 1 | aa | 24 | Male | math |
  6. | 2 | bb | 20 | Female | language |
  7. | 3 | cc | 21 | Male | English |
  8. | 4 | dd | 22 | Male | history |
  9. | 5 | ee | 25 | Female | Chinese |
  10. +------+------+------+--------+----------+
  11. 5 rows in set (0.00 sec)
  12. 以上就是自己定义的数据的信息了哦。。

对表中数据的操作(定义表):

update tb_name set colum_name=value where condition; #更改某一字段的属性(约束条件)

一般来讲,where 中的condition指的是对表中某字段或某些字段做判定(等值比较,或正则表达式匹配,通配符匹配)

  1. eg:
  2. mysql>update data set Course='math' where ID=2;#将ID号为2的人选的课程更改为math
  3. Query OK, 1 row affected (0.00 sec)
  4. Rows matched: 1 Changed: 1 Warnings: 0
  5. mysql> select * from data;
  6. +------+------+------+--------+---------+
  7. | ID | Name | Age | Gender | Course |
  8. +------+------+------+--------+---------+
  9. | 1 | aa | 24 | Male | math |
  10. | 2 | bb | 20 | Female | math |
  11. | 3 | cc | 21 | Male | English |
  12. | 4 | dd | 22 | Male | history |
  13. | 5 | ee | 25 | Female | Chinese |
  14. +------+------+------+--------+---------+
  15. 5 rows in set (0.00 sec)

delete from tb_name where condition; #从表中删除符合某种条件的数据

  1. eg:
  2. mysql> delete from data where Age < 22; #将年龄小于22的所有人删除
  3. Query OK, 2 rows affected (0.00 sec)
  4. mysql> select * from data;
  5. +------+------+------+--------+---------+
  6. | ID | Name | Age | Gender | Course |
  7. +------+------+------+--------+---------+
  8. | 1 | aa | 24 | Male | math |
  9. | 4 | dd | 22 | Male | history |
  10. | 5 | ee | 25 | Female | Chinese |
  11. +------+------+------+--------+---------+
  12. 3 rows in set (0.00 sec)

查询:

select 字段 from 表 [where condition]

  1. mysql> select Name ,Age ,ID from data where Gender='Male';
  2. #查找性别为男的姓名、年龄和ID号。
  3. +------+------+------+
  4. | Name | Age | ID |
  5. +------+------+------+
  6. | aa | 24 | 1 |
  7. | dd | 22 | 4 |
  8. +------+------+------+
  9. 2 rows in set (0.00 sec)

对字段进行操作:

alter table tb_name drop 字段名称; #删除字段

  1. mysql> alter table data drop Age; #删除年龄这个字段
  2. Query OK, 3 rows affected (0.02 sec)
  3. Records: 3 Duplicates: 0 Warnings: 0
  4. mysql> select * from data;
  5. +------+------+--------+---------+
  6. | ID | Name | Gender | Course |
  7. +------+------+--------+---------+
  8. | 1 | aa | Male | math |
  9. | 4 | dd | Male | history |
  10. | 5 | ee | Female | Chinese |
  11. +------+------+--------+---------+
  12. 3 rows in set (0.01 sec)

alter table tb_name add 字段名 类型 [{first|after column_name}]; #添加字段

  1. mysql> alter table data add class int unsigned after ID;
  2. #在ID 号之后添加一个CLASS 字段
  3. Query OK, 3 rows affected (0.05 sec)
  4. Records: 3 Duplicates: 0 Warnings: 0
  5. mysql> select * from data;
  6. +------+-------+------+--------+---------+
  7. | ID | class | Name | Gender | Course |
  8. +------+-------+------+--------+---------+
  9. | 1 | NULL | aa | Male | math |
  10. | 4 | NULL | dd | Male | history |
  11. | 5 | NULL | ee | Female | Chinese |
  12. +------+-------+------+--------+---------+
  13. 3 rows in set (0.00 sec)

alter table tb_name change old_name new_name datatype; #更改字段名

alter table tb_name modify column_name datatype; #更改一个字段的属性

(可以按照上面的例子自己尝试一下哦)

数据控制语言DCL:

grant privileges on db_name to 'username'@host'identified by 'passwd'; #赋予某用户什么权限

  1. mysql> grant all on mydata.data to 'abc'@'172.16.%.%' identified by 'RedHat';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> flush privileges;
  4. #将mydata库中表data的所有权限授予172.16/24这个网段内的abc用户,
  5. 并给用户指定密码为redhat
  6. #让新建用户和授权立即生效:flush privileges;

revoke privileges on db_name.tb_name to 'username'@'host'; #从用户收回在数据库对象上的权限

  1. mysql> revoke delete on mydata.data from 'abc'@'172.16.%.%';
  2. #从abc用户上收回对数据库delete的权限
  3. Query OK, 0 rows affected (0.00 sec)

 

  1. mysql> flush privileges;

验证效果,再打开一台主机(172.16.10.1),连接该数据库,进行操作。

远程连接有些命令选项:

-h:指定服务器地址

-u:指定用户名

-p:让用户输入密码

  1. [root@www ~]# mysql -h 172.16.9.1 -u abc -p; #以用户abc的身份连接服务器上的数据库
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 12
  5. Server version: 5.0.77 Source distribution
  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  7. mysql> mysql> select * from data; #可以查询的哦!!
  8. +------+-------+------+--------+---------+
  9. | ID | class | Name | Gender | Course |
  10. +------+-------+------+--------+---------+
  11. | 1 | NULL | aa | Male | math |
  12. | 4 | NULL | dd | Male | history |
  13. | 5 | NULL | ee | Female | Chinese |
  14. +------+-------+------+--------+---------+
  15. 3 rows in set (0.00 sec)
  16. mysql> delete from data where ID=1;
  17. ERROR 1142 (42000): DELETE command denied to user 'abc'@'172.16.10.1' for table 'data'
  18. #在这里就会显示不允许delete操作的提示了!!

 

以上就是数据库的操作了^_^

linux
相关资讯       MySQL基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款