上一节我们在mysql里面新建了一个用户,数据库,并且为用户添加了权限,现在,我们来对数据库进行一些操作。
首先,假定你已经登录并且有一个数据库的操作权限了。
键入:use dbname;
eg:use SwenDB;
接下来,我们为这个空荡荡的数据库添加点表了。
键入:creat tbname(列名 数据类型,columnname type,.......);
eg:create student(id int(12),name varchar(50),birthday varchar(20),content varchar(50));
现在,表中有了 id,name,birthday和content四列,你可以用desc或者describe查看表的结构。
键入:desc tbname;
eg: desc student;
显示:
+----------+-------------+------+-----+---------+-------+
|
Field | Type | Null
| Key | Default | Extra
|
+----------+-------------+------+-----+---------+-------+
|
id | int(12) |
YES | | NULL
| |
| name |
varchar(50) | YES | | NULL
| |
| birthday | varchar(20) | YES
| | NULL
| |
| content | varchar(50) |
YES | | NULL
|
|
+----------+-------------+------+-----+---------+-------+
4 rows in set
(0.00 sec)
好的,现在我们正式往student表插入一些数据吧。
键入:insert into tbname values(values);
eg:insert into student values(201430630314,‘Swen‘,‘2014-01-01‘,‘say something.‘);
显示:Query OK, 1 row affected, 1 warning (0.00 sec) 表明你已经成功注入一条信息了。
我们还有另一种插入方式(= =lll等等,好像有什么不对劲)
键入:insert into student(列名) values(value),(value),(value); 可以同时插入多条表项。
eg:insert into student(id,name,birthday,content) values(201430630314,‘Jack‘,‘2014-01-01‘,‘say nothing.‘),(201430630314,‘Kate‘,‘2014-01-01‘,‘hello!‘),(201430630311,‘Peter‘,‘2014-01-02‘,‘hi!‘);
now,我们来查看一下我们都插入了些什么数据进去。
键入:select 列名 from tbname;
eg:select * from student;
显示:
+------------+-------+------------+----------------+
|
id | name |
birthday | content
|
+------------+-------+------------+----------------+
| 2147483647 |
Swen | 2014-01-01 | say something. |
| 2147483647 | Jack |
2014-01-01 | say nothing. |
| 2147483647 | Kate |
2014-01-01 | hello! |
|
2147483647 | Peter | 2014-01-02 |
hi!
|
+------------+-------+------------+----------------+
4 rows in set (0.00
sec)
你会发现id哪里有点问题,我们暂时先不管。其他数据一切正常。
好,我们再来几个select操作来玩玩。
eg: select name from student;
显示:
+-------+
| name |
+-------+
| Swen |
| Jack
|
| Kate |
| Peter |
+-------+
4 rows in set (0.00
sec)
eg:select birthday,content from student;
显示:
+------------+----------------+
| birthday |
content
|
+------------+----------------+
| 2014-01-01 | say something. |
|
2014-01-01 | say nothing. |
| 2014-01-01 |
hello! |
| 2014-01-02 |
hi!
|
+------------+----------------+
4 rows in set (0.00 sec)
好了,现在,我们感觉Jack这个名字不够威风,不够霸气,我们想要改一下名字哟啊怎么办呢?用update。
键入:update tbname set 列名=新值 where 列名=旧值; (where后面的语句只是为了定位你要修改的值而已)
eg:update student set name=‘Jacky‘ where name=‘Jack‘;
显示:
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1
Warnings: 0
我们再来查看一下
eg:select * from student;
显示:
+------------+-------+------------+----------------+
|
id | name |
birthday | content
|
+------------+-------+------------+----------------+
| 2147483647 |
Swen | 2014-01-01 | say something. |
| 2147483647 | Jacky | 2014-01-01
| say nothing. |
| 2147483647 | Kate | 2014-01-01 |
hello! |
| 2147483647 | Peter
| 2014-01-02 |
hi!
|
+------------+-------+------------+----------------+
4 rows in set (0.00
sec)
我们的Jack已经改名为Jacky了!
好,现在我们看Kate不爽,怎么办?delete了他!
键入:delete from tbname where 条件; (这里注意一下,一定不要忘了加where和后面的条件,要不然就清空了整个表了)
eg: delete from student where name=‘Kate‘;
显示:Query OK, 1 row affected (0.00 sec)
我们看看:
eg: select * from
student;
显示:
+------------+-------+------------+----------------+
|
id | name |
birthday | content
|
+------------+-------+------------+----------------+
| 2147483647 |
Swen | 2014-01-01 | say something. |
| 2147483647 | Jacky | 2014-01-01
| say nothing. |
| 2147483647 | Peter | 2014-01-02 |
hi!
|
+------------+-------+------------+----------------+
3 rows in set (0.00
sec)
哈哈,Kate不见了!
这一节我们体验了一下增删查改数据库里面的表里面的数据,那么,我们要是设计表的时候考虑不周,表的列少了或者多了,列名错了,怎么添加或者删除列或修改呢?
下一节我们来体验一下如何来修改表的目录结构。