mysql 表基本增删查改

对表的操作是在某个数据库下才能进行的,所以要先选择数据库 "use 数据库名;" 

 

1、创建数据表

1)"create table 表名 (字段1  类型  [约束],  字段2  类型  [约束]);"  创建数据表

创建学生信息表,包含字段(id,姓名,性别,生日),其中id为主键

mysql> create table stu(
    -> stu_id int auto_increment primary key,
    -> name varchar(30) not null,
    -> gender bit default 1,
    -> birthday date);
Query OK, 0 rows affected (0.01 sec)

2)"show tabales;"  查看当前数据库的所有表

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| cls            |
| stu            |
+----------------+
2 rows in set (0.00 sec)

3)"show create table 表名;"  查看创建表语句

mysql> show create table stu;
+-------+-------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                  |
+-------+-------------------------------------------+
| stu   | CREATE TABLE `stu` (
  `stu_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `gender` bit(1) DEFAULT b'1',
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------+
1 row in set (0.00 sec)

 4)"desc 表名;"  查看表的结构

mysql> desc stu;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| stu_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(30) | NO   |     | NULL    |                |
| gender   | bit(1)      | YES  |     | b'1'    |                |
| birthday | date        | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

 

2、select 查看表中数据

1)"select * from 表名;"   查看所有字段的数据,下边gender列看到什么都没有,因为gender的类型是二进制的位,显示不了字符,在图形界面可以显示如下图。

mysql> select * from stu;
+--------+--------+--------+------------+
| stu_id | name   | gender | birthday   |
+--------+--------+--------+------------+
|      1 | 张三   |       | 1990-01-01 |
|      2 | 李四   |       | 1991-03-02 |
|      3 | 王五   |        | 1995-12-01 |
|      4 | 赵六   |        | 1996-01-01 |
+--------+--------+--------+------------+
4 rows in set (0.00 sec)

2)"select 字段1, 字段2, 字段3  from 表名;"   查看指定字段的数据。

mysql> select name, birthday from stu;
+--------+------------+
| name   | birthday   |
+--------+------------+
| 张三   | 1990-01-01 |
| 李四   | 1991-03-02 |
| 王五   | 1995-12-01 |
| 赵六   | 1996-01-01 |
+--------+------------+
4 rows in set (0.00 sec)

 

 

3、表添加数据

1)"insert into 表名 values(value1, value2, value3...);"  没有指定插入的字段,必须按照字段的顺序全部插入相应的值,自增字段一般以0占空。顺序以表的结构为准,即"desc 表名;"

mysql> insert into stu values(0, '郭靖', 1, '1989-1-1');
Query OK, 1 row affected (0.00 sec)
mysql
> select stu_id, name, birthday from stu; +--------+--------+------------+ | stu_id | name | birthday | +--------+--------+------------+ | 1 | 张三 | 1990-01-01 | | 2 | 李四 | 1991-03-02 | | 3 | 王五 | 1995-12-01 | | 4 | 赵六 | 1996-01-01 | | 6 | 郭靖 | 1989-01-01 | +--------+--------+------------+ 5 rows in set (0.00 sec)

2)"insert into 表名(列1, 列2...) values(value1, value2...);"  只插入指定的值,注意:非空字段一定要有值

mysql> insert into stu(name, gender) values('黄蓉', 0);
Query OK, 1 row affected (0.00 sec)

mysql> select stu_id, name, birthday from stu;
+--------+--------+------------+
| stu_id | name   | birthday   |
+--------+--------+------------+
|      1 | 张三   | 1990-01-01 |
|      2 | 李四   | 1991-03-02 |
|      3 | 王五   | 1995-12-01 |
|      4 | 赵六   | 1996-01-01 |
|      6 | 郭靖   | 1989-01-01 |
|      7 | 黄蓉   | NULL       |
+--------+--------+------------+
6 rows in set (0.00 sec)

3)"insert into 表名 values(value1, value2..), (value1, value2..)...;"  一次插入多条记录,注意这是mysql特有的,其它数据库并不支持

mysql> insert into stu(name, gender) values('黄蓉', 0), ('杨过', 1), ('小龙女', 0);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

 

4、where子句

where子句用于筛选数据

1)从stu中查找郭靖的信息

mysql> select * from stu where name='郭靖';
+--------+--------+--------+------------+
| stu_id | name   | gender | birthday   |
+--------+--------+--------+------------+
|      6 | 郭靖   |       | 1989-01-01 |
+--------+--------+--------+------------+
1 row in set (0.00 sec)

2)从stu中查找所有男的

mysql> select * from stu where gender=1;
+--------+--------+--------+------------+
| stu_id | name   | gender | birthday   |
+--------+--------+--------+------------+
|      1 | 张三   |       | 1990-01-01 |
|      2 | 李四   |       | 1991-03-02 |
|      6 | 郭靖   |       | 1989-01-01 |
|      9 | 杨过   |       | NULL       |
+--------+--------+--------+------------+
4 rows in set (0.00 sec)

 

5、更改表数据

"update 表名 set 字段1=new-value1, 字段2=new-value2... [where 条件];"    更改表数据一般和where子句连用,不用where子句表示更改所有的字段,主键一般不进行更改

1)更改表中张三的出生日期为'2012-12-12'

mysql> update stu set birthday='2012-12-12' where name='张三';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from stu where name='张三';
+--------+--------+--------+------------+
| stu_id | name   | gender | birthday   |
+--------+--------+--------+------------+
|      1   |    张三   |           | 2012-12-12 |
+--------+--------+--------+------------+
1 row in set (0.00 sec)

 

6、删除数据

"delete from 表名 where 条件;"  delete语句要和where一起使用,不然表示把所有的数据都删除

1)将stu表中的'黄蓉删除'

mysql> delete from stu where name='黄蓉';
Query OK, 2 rows affected (0.00 sec)

mysql> select * from stu where name='黄蓉';
Empty set (0.00 sec)

 

posted @ 2018-10-22 10:22  nonzero  阅读(329)  评论(0编辑  收藏  举报