【笔记三】:表的创建、查询、更新、删除

目录

1,表的创建

2,插入数据

3,查询数据

4,更新数据

5,删除数据

6,删除表


1,表的创建

create tab <表名>(<字段>, <类型>, ...);

如:插入以下数据

idnamesexageinterestbrithday
10001小明boy16football2004-3-5
10002小红girl14music2006-6-18
10003小张boy15painting2005-08-14
10004露丝girl13dance2007-07-19
MariaDB [mydb]> create table students( id int(10), name varchar(10), sex varchar(10), age int(3), interest varchar(100),  birthday date );
Query OK, 0 rows affected (0.043 sec)

查看表结构 

desc <表名>;

MariaDB [mydb]> desc students;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(10)      | YES  |     | NULL    |       |
| name     | varchar(10)  | YES  |     | NULL    |       |
| sex      | varchar(10)  | YES  |     | NULL    |       |
| age      | int(3)       | YES  |     | NULL    |       |
| interest | varchar(100) | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
6 rows in set (0.005 sec)

2,插入数据

inster into <table_name> ( field1, field2,...fieldN )
                       values
                       ( value1, value2,...valueN );

方式一:在添加数据的时候(如果我们指定了添加的列名,那么需要根据指定列名的顺序去添加对应的值) students中的key和value要一一对应

MariaDB [mydb]> insert into students(id,name,sex,age,interest,birthday) values(10001,'小明','boy',16,'football','2004-3-5');
Query OK, 1 row affected (0.008 sec)

方式二:在没有指定列名的时候我们添加的值要和表的列名一一对应

MariaDB [mydb]> insert into students values(10002,'小红', 'girl', '14', 'music', '2006-6-18');
Query OK, 1 row affected (0.007 sec)

(1)列名顺序自定义,但是列值会和列名的顺序一一对应

(2)数字类型的数据不需要加单引号,字符串类型或日期类型的值需要添加单引号

(3)列名部分的内容可以省略,但是此时该列值的顺序要和创建表时列的顺序一致

一次插入多行数据:

MariaDB [mydb]> insert into students values(10003,'小张','boy',15,'painting','2005-8-14'),(10004,'露丝','girl','13','dance','2007-7-19');
Query OK, 2 rows affected (0.008 sec)
Records: 2  Duplicates: 0  Warnings: 0

3,查询数据

select * from <表名>; 

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 10001 | 小明   | boy  |   16 | football | 2004-03-05 |
| 10002 | 小红   | girl |   14 | music    | 2006-06-18 |
| 10003 | 小张   | boy  |   15 | painting | 2005-08-14 |
| 10004 | 露丝   | girl |   13 | dance    | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.000 sec)

 

select <field1, field2,...fieldN> from <table_name1, table_name2...>
[where condition1 [AND [OR]] condition2.....
  • 查询语句中你可以使用一个或者多个表,表之间使用逗号, 分割,并使用WHERE语句来设定查询条件。
  • 你可以在 WHERE 子句中指定任何条件。
  • 你可以使用 AND 或者 OR 指定一个或多个条件。
  • WHERE 子句也可以运用于 SQL 的 DELETE 或者 UPDATE 命令。
  • WHERE 子句类似于程序语言中的 if 条件,根据 MySQL 表中的字段值来读取指定的数据
操作符描述实例
=等号,检测两个值是否相等,如果相等返回true(A = B) 返回false。
<>, !=不等于,检测两个值是否相等,如果不相等返回true(A != B) 返回 true。
>大于号,检测左边的值是否大于右边的值, 如果左边的值大于右边的值返回true(A > B) 返回false。
<小于号,检测左边的值是否小于右边的值, 如果左边的值小于右边的值返回true(A < B) 返回 true。
>=大于等于号,检测左边的值是否大于或等于右边的值, 如果左边的值大于或等于右边的值返回true(A >= B) 返回false。
<=小于等于号,检测左边的值是否小于或等于右边的值, 如果左边的值小于或等于右边的值返回true(A <= B) 返回 true。

查询查询students表中id等于10001的学生

MariaDB [mydb]> select * from students where id=10001;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 10001 | 小明   | boy  |   16 | football | 2004-03-05 |
+-------+--------+------+------+----------+------------+
1 row in set (0.001 sec)

查询查询students表中id不等于10001的学生

MariaDB [mydb]> select * from students where id!=10001;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 10002 | 小红   | girl |   14 | music    | 2006-06-18 |
| 10003 | 小张   | boy  |   15 | painting | 2005-08-14 |
| 10004 | 露丝   | girl |   13 | dance    | 2007-07-19 |
+-------+--------+------+------+----------+------------+
3 rows in set (0.001 sec)

查询查询students表中id大于10001并且小于10004的学生

MariaDB [mydb]> select * from students where id>10001 and id<10004;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 10002 | 小红   | girl |   14 | music    | 2006-06-18 |
| 10003 | 小张   | boy  |   15 | painting | 2005-08-14 |
+-------+--------+------+------+----------+------------+
2 rows in set (0.001 sec)

查询查询students表中id大于10001并且小于10004的学生,只输出学生name和id

MariaDB [mydb]> select id,name from students where id>10001 and id<10004;
+-------+--------+
| id    | name   |
+-------+--------+
| 10002 | 小红   |
| 10003 | 小张   |
+-------+--------+
2 rows in set (0.001 sec)

4,更新数据

update <table_name> set <field1=new-value1, field2=new-value2>
[where Clause]
  • 你可以同时更新一个或多个字段。
  • 你可以在 WHERE 子句中指定任何条件。
  • 你可以在一个单独表中同时更新数据。

将id是10001学生的id修改为20001

MariaDB [mydb]> update students set id=20001 where id=10001;
Query OK, 1 row affected (0.009 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 20001 | 小明   | boy  |   16 | football | 2004-03-05 |
| 10002 | 小红   | girl |   14 | music    | 2006-06-18 |
| 10003 | 小张   | boy  |   15 | painting | 2005-08-14 |
| 10004 | 露丝   | girl |   13 | dance    | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)

将name=‘小红’的id修改为20002

MariaDB [mydb]> update students set id=20002 where name='小红';
Query OK, 1 row affected (0.008 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 20001 | 小明   | boy  |   16 | football | 2004-03-05 |
| 20002 | 小红   | girl |   14 | music    | 2006-06-18 |
| 10003 | 小张   | boy  |   15 | painting | 2005-08-14 |
| 10004 | 露丝   | girl |   13 | dance    | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)

 将性别改为中文显示

MariaDB [mydb]> update students set sex='男' where sex='boy';
Query OK, 2 rows affected (0.008 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [mydb]> update students set sex='女' where sex='girl';
Query OK, 2 rows affected (0.010 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 20001 | 小明   | 男   |   16 | football | 2004-03-05 |
| 20002 | 小红   | 女   |   14 | music    | 2006-06-18 |
| 10003 | 小张   | 男   |   15 | painting | 2005-08-14 |
| 10004 | 露丝   | 女   |   13 | dance    | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)

 将id<20000的批量增加10000

MariaDB [mydb]> update students set id=id+10000 where id<20000; 
Query OK, 2 rows affected (0.008 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 20001 | 小明   | 男   |   16 | football | 2004-03-05 |
| 20002 | 小红   | 女   |   14 | music    | 2006-06-18 |
| 20003 | 小张   | 男   |   15 | painting | 2005-08-14 |
| 20004 | 露丝   | 女   |   13 | dance    | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)

 

当我们需要将字段中的特定字符串批量修改为其他字符串时,可已使用以下操作:

UPDATE table_name SET field=REPLACE(field, 'old-string', 'new-string') [WHERE Clause]
MariaDB [mydb]> update students set interest = replace(interest, 'dance', 'painting');
Query OK, 2 rows affected (0.009 sec)
Rows matched: 5  Changed: 2  Warnings: 0

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 20001 | 小明   | 男   |   16 | football | 2004-03-05 |
| 20002 | 小红   | 女   |   14 | music    | 2006-06-18 |
| 20003 | 小张   | 男   |   15 | painting | 2005-08-14 |
| 20004 | 露丝   | 女   |   13 | painting | 2007-07-19 |
| 20005 | 丽丽   | 女   |   15 | painting | 2005-12-01 |
+-------+--------+------+------+----------+------------+
5 rows in set (0.001 sec)

 

新增一列

如果想在一个已经建好的表中添加一列,可以用诸如:

alter table <表名> add column <列名> <类型> not null;

这条语句会向已有的表中加入新的一列,这一列在表的最后一列位置。如果我们希望添加在指定的一列,可以用:

alter table <表名> add column <列名> <类型> not null after <在此列后面添加>;

注意,上面这个命令的意思是说添加新列到某一列后面。如果想添加到第一列的话,可以用:

alter table <表名> add column <列名> <类型> not null first;

删除一列:

alter table <表名> drop column <列名>;

 

MariaDB [mydb]> alter table score add column class int(1) not null after id;
Query OK, 0 rows affected (0.071 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mydb]> select * from score;
+-------+-------+-----------+------+----------+--------+
| id    | class | name      | math | language | sports |
+-------+-------+-----------+------+----------+--------+
| 20001 |     0 | 小明      |   81 |       86 |     93 |
| 20002 |     0 | 小红      |   86 |       86 |     89 |
| 20003 |     0 | 小张      |   77 |       83 |     93 |
| 20004 |     0 | 露丝      |   88 |       78 |     65 |
| 20005 |     0 | 丽丽      |   92 |       94 |     64 |
| 20006 |     0 | 李明      |   75 |       78 |     88 |
| 20007 |     0 | 张大明    |   54 |       65 |     95 |
+-------+-------+-----------+------+----------+--------+
7 rows in set (0.001 sec)

5,删除数据

delete from <table_name> [where Clause]
  • 如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
  • 你可以在 WHERE 子句中指定任何条件
  • 您可以在单个表中一次性删除记录。

删除id=10004的学生

MariaDB [mydb]> delete from students where id=10004;
Query OK, 1 row affected (0.008 sec)

MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id    | name   | sex  | age  | interest | birthday   |
+-------+--------+------+------+----------+------------+
| 20001 | 小明   | 男   |   16 | football | 2004-03-05 |
| 20002 | 小红   | 女   |   14 | music    | 2006-06-18 |
| 10003 | 小张   | 男   |   15 | painting | 2005-08-14 |
+-------+--------+------+------+----------+------------+
3 rows in set (0.001 sec)

 

delete,drop,truncate 都有删除表的作用,区别在于:

  •  1、delete 和 truncate 仅仅删除表数据,drop 连表数据和表结构一起删除,打个比方,delete 是单杀,truncate 是团灭,drop 是把电脑摔了。
  •  2、delete 是 DML 语句,操作完以后如果没有不想提交事务还可以回滚,truncate 和 drop 是 DDL 语句,操作完马上生效,不能回滚,打个比方,delete 是发微信说分手,后悔还可以撤回,truncate 和 drop 是直接扇耳光说滚,不能反悔。
  •  3、执行的速度上,drop>truncate>delete,打个比方,drop 是神舟火箭,truncate 是和谐号动车,delete 是自行车。

 

6,删除表

drop database <数据库名>;
MariaDB [mydb]> drop table students;
Query OK, 0 rows affected (0.048 sec)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值