下面以一个实例来解析下数据库这些命令的用法:
要想使用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
- mysql> use mydata;
- Database changed
- mysql> create table data (ID int unsigned,Name char(20),Age int unsigned,Gender enum ('Female','Male'),Course char(30)); //建表结束
- Query OK, 0 rows affected (0.01 sec)
- 注:在建表中(1)data将ID设为长度为整型的数字字段:并且是无符号的
- (2)将NAME设为长度为20的字符字段
- (3)将Age设为整型的数字字段而且是无符号的
- (4)将Gender设为和Male或Female匹配的字符
- (5)课程设置长度为30的字符字段。
mysql> desc data; #查看定义的表
- +--------+-----------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-----------------------+------+-----+---------+-------+
- | ID | int(10) unsigned | YES | | NULL | |
- | Name | char(20) | YES | | NULL | |
- | Age | int(10) unsigned | YES | | NULL | |
- | Gender | enum('Female','Male') | YES | | NULL | |
- | Course | char(30) | YES | | NULL | |
- +--------+-----------------------+------+-----+---------+-------+
- 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; #完成后,查看表里的内容:
- mysql> select * from data;
- +------+------+------+--------+----------+
- | ID | Name | Age | Gender | Course |
- +------+------+------+--------+----------+
- | 1 | aa | 24 | Male | math |
- | 2 | bb | 20 | Female | language |
- | 3 | cc | 21 | Male | English |
- | 4 | dd | 22 | Male | history |
- | 5 | ee | 25 | Female | Chinese |
- +------+------+------+--------+----------+
- 5 rows in set (0.00 sec)
- 以上就是自己定义的数据的信息了哦。。
对表中数据的操作(定义表):
update tb_name set colum_name=value where condition; #更改某一字段的属性(约束条件)
一般来讲,where 中的condition指的是对表中某字段或某些字段做判定(等值比较,或正则表达式匹配,通配符匹配)
- eg:
- mysql>update data set Course='math' where ID=2;#将ID号为2的人选的课程更改为math
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from data;
- +------+------+------+--------+---------+
- | ID | Name | Age | Gender | Course |
- +------+------+------+--------+---------+
- | 1 | aa | 24 | Male | math |
- | 2 | bb | 20 | Female | math |
- | 3 | cc | 21 | Male | English |
- | 4 | dd | 22 | Male | history |
- | 5 | ee | 25 | Female | Chinese |
- +------+------+------+--------+---------+
- 5 rows in set (0.00 sec)
delete from tb_name where condition; #从表中删除符合某种条件的数据
- eg:
- mysql> delete from data where Age < 22; #将年龄小于22的所有人删除
- Query OK, 2 rows affected (0.00 sec)
- mysql> select * from data;
- +------+------+------+--------+---------+
- | ID | Name | Age | Gender | Course |
- +------+------+------+--------+---------+
- | 1 | aa | 24 | Male | math |
- | 4 | dd | 22 | Male | history |
- | 5 | ee | 25 | Female | Chinese |
- +------+------+------+--------+---------+
- 3 rows in set (0.00 sec)
查询:
select 字段 from 表 [where condition]
- mysql> select Name ,Age ,ID from data where Gender='Male';
- #查找性别为男的姓名、年龄和ID号。
- +------+------+------+
- | Name | Age | ID |
- +------+------+------+
- | aa | 24 | 1 |
- | dd | 22 | 4 |
- +------+------+------+
- 2 rows in set (0.00 sec)
对字段进行操作:
alter table tb_name drop 字段名称; #删除字段
- mysql> alter table data drop Age; #删除年龄这个字段
- Query OK, 3 rows affected (0.02 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> select * from data;
- +------+------+--------+---------+
- | ID | Name | Gender | Course |
- +------+------+--------+---------+
- | 1 | aa | Male | math |
- | 4 | dd | Male | history |
- | 5 | ee | Female | Chinese |
- +------+------+--------+---------+
- 3 rows in set (0.01 sec)
alter table tb_name add 字段名 类型 [{first|after column_name}]; #添加字段
- mysql> alter table data add class int unsigned after ID;
- #在ID 号之后添加一个CLASS 字段
- Query OK, 3 rows affected (0.05 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> select * from data;
- +------+-------+------+--------+---------+
- | ID | class | Name | Gender | Course |
- +------+-------+------+--------+---------+
- | 1 | NULL | aa | Male | math |
- | 4 | NULL | dd | Male | history |
- | 5 | NULL | ee | Female | Chinese |
- +------+-------+------+--------+---------+
- 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'; #赋予某用户什么权限
- mysql> grant all on mydata.data to 'abc'@'172.16.%.%' identified by 'RedHat';
- Query OK, 0 rows affected (0.00 sec)
- mysql> flush privileges;
- #将mydata库中表data的所有权限授予172.16/24这个网段内的abc用户,
- 并给用户指定密码为redhat
- #让新建用户和授权立即生效:flush privileges;
revoke privileges on db_name.tb_name to 'username'@'host'; #从用户收回在数据库对象上的权限
- mysql> revoke delete on mydata.data from 'abc'@'172.16.%.%';
- #从abc用户上收回对数据库delete的权限
- Query OK, 0 rows affected (0.00 sec)
- mysql> flush privileges;
验证效果,再打开一台主机(172.16.10.1),连接该数据库,进行操作。
远程连接有些命令选项:
-h:指定服务器地址
-u:指定用户名
-p:让用户输入密码
- [root@www ~]# mysql -h 172.16.9.1 -u abc -p; #以用户abc的身份连接服务器上的数据库
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 12
- Server version: 5.0.77 Source distribution
- Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- mysql> mysql> select * from data; #可以查询的哦!!
- +------+-------+------+--------+---------+
- | ID | class | Name | Gender | Course |
- +------+-------+------+--------+---------+
- | 1 | NULL | aa | Male | math |
- | 4 | NULL | dd | Male | history |
- | 5 | NULL | ee | Female | Chinese |
- +------+-------+------+--------+---------+
- 3 rows in set (0.00 sec)
- mysql> delete from data where ID=1;
- ERROR 1142 (42000): DELETE command denied to user 'abc'@'172.16.10.1' for table 'data'
- #在这里就会显示不允许delete操作的提示了!!
以上就是数据库的操作了^_^
