|
2#
楼主 |
发表于 2007-5-27 18:44:59
|
只看该作者
2.4 插入数据
2.4.1 使用INSERT语句进行插入记录操作(数据库表中每一行就是一个记录,插入记录实际上就是向表中插入一行)
格式:INSERT INTO table_name(数据表名) VALUES(值1,值2,值3...);
在VALUES关键字后括号中的数值和字符段,必须和你所建的表所定义的字段数据类型必须一致!
mysql> insert into emp values
-> ('100005','啸天',27,3000,'1979-07-10','male');
插入成功后会有如下信息显示:
Query OK, 1 row affected (0.03 sec)
2.4.2 查看表中的数据(可以确定数据是否已经成功插入)
mysql> select * from emp;
成功插入后的数据显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 3000 | 1979-07-10 | male |
+--------+----------+---------+---------+------------+---------+
1 row in set (0.00 sec)
一次性插入多行的格式:INSERT INTO table_name(数据表名) VALUES(值1,值2,值3...),(值1,值2,值3...);
mysql> insert into emp values
-> ('100001','红枫',29,8000,'1977-01-01','male'),
-> ('100002','丽鹃',27,7000,'1979-12-31','fmale');
可以查看插入后的数据结果:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 3000 | 1979-07-10 | male |
| 100004 | 红枫 | 29 | 8000 | 1977-01-01 | male |
| 100002 | 丽鹃 | 27 | 7000 | 1979-12-31 | fmale |
+--------+----------+---------+---------+------------+---------+
3 rows in set (0.00 sec)
----------------------我------------割--------------------------
2.5 修改数据
2.5.1 使用UPDATE语句来更新表中的数据
mysql> update emp set emp_id=100001 where emp_name='红枫';
修改结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 3000 | 1979-07-10 | male |
| 100001 | 红枫 | 29 | 8000 | 1977-01-01 | male |
| 100002 | 丽鹃 | 27 | 7000 | 1979-12-31 | fmale |
+--------+----------+---------+---------+------------+---------+
3 rows in set (0.00 sec)
如果该语句没有后面的where限制,将会对表中所有的记录都进行修改。
如,给全部人加薪1000,可以如下修改:
mysql> update emp set emp_sal=emp_sal+1000;
修改结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
+--------+----------+---------+---------+------------+---------+
3 rows in set (0.00 sec)
----------------------我------------割--------------------------
2.6 高级查询方法
2.6.1 记录查询
查询emp表中,emp_name为啸天的全部信息
mysql> select * from emp where emp_name='啸天';
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
+--------+----------+---------+---------+------------+---------+
1 row in set (0.00 sec)
查询emp表中,emp_sal,工资在5000以上的全部信息
mysql> select * from emp where emp_sal>5000;
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
+--------+----------+---------+---------+------------+---------+
2 rows in set (0.00 sec)
查询emp表中在1978年1月1日之后出生的
mysql> select * from emp where emp_bir>'1978-01-01';
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
+--------+----------+---------+---------+------------+---------+
2 rows in set (0.00 sec)
查询emp表中在1979年12月1日之前出生,工资在5000以上的
mysql> select * from emp where emp_bir<'1979-12-01' and emp_sal>5000;
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
+--------+----------+---------+---------+------------+---------+
1 row in set (0.00 sec)
2.6.2 字段查询
CEO查看员工工资情况
mysql> select emp_name,emp_sal from emp;
查询结果显示如下:
+----------+---------+
| emp_name | emp_sal |
+----------+---------+
| 啸天 | 4000 |
| 红枫 | 9000 |
| 丽鹃 | 8000 |
+----------+---------+
3 rows in set (0.00 sec)
查看1978年后出生的人的姓名、工资和性别
mysql> select emp_name,emp_sal,emp_sex from emp where emp_bir>"1977-12-31";
查询结果显示如下:
+----------+---------+---------+
| emp_name | emp_sal | emp_sex |
+----------+---------+---------+
| 啸天 | 4000 | male |
| 丽鹃 | 8000 | fmale |
+----------+---------+---------+
2 rows in set (0.00 sec)
2.6.3 查询结果排序
用ORDER BY语句对emp表中所有员工工资高低顺序查询结果(默认是从低到高——升序)
mysql> select * from emp order by emp_sal;
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
+--------+----------+---------+---------+------------+---------+
3 rows in set (0.00 sec)
用DESC关键字来进行从高到低排序——降序
mysql> select * from emp order by emp_sal desc;
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
+--------+----------+---------+---------+------------+---------+
3 rows in set (0.00 sec)
2.6.4 查询结果数量的限制
用LIMIT查看emp表中工资收入排名前两个员工的资料:
mysql> select * from emp order by emp_sal desc limit 2;
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
+--------+----------+---------+---------+------------+---------+
2 rows in set (0.00 sec)
查看工资排名第2到第3的员工资料:
mysql> select * from emp order by emp_sal desc limit 1,2;
查询结果显示如下:
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100002 | 丽鹃 | 27 | 8000 | 1979-12-31 | fmale |
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
+--------+----------+---------+---------+------------+---------+
2 rows in set (0.01 sec)
使用rand()抽样调查,随机抽取2个员工,查看其资料
mysql> select * from emp order by rand() limit 2;
如如下结果:(随机的)
+--------+----------+---------+---------+------------+---------+
| emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex |
+--------+----------+---------+---------+------------+---------+
| 100005 | 啸天 | 27 | 4000 | 1979-07-10 | male |
| 100001 | 红枫 | 29 | 9000 | 1977-01-01 | male |
+--------+----------+---------+---------+------------+---------+
2 rows in set (0.01 sec) |
|