Mysql增删改查sql语句

关于数据库的一些操作:

进入mysql 命令行: mysql -uroot –p
查看所有数据库: show databases;
创建数据库: create database wg charset utf8;
删除数据库: drop database wg;
选择数据库: use databases;
查看所有表: show tables;
查看创建数据库的语句:show create database databasename;
查看创建表的语句:show create table tablename;
查看表结构:desc tablename;

增:

新建数据库

MariaDB [(none)]> create table students(id int auto_increment primary key,name varchar(10) not null,sex varchar(12),address varchar(50),phone int not null unique);
#自增长			auto_increment
#非空			not null
#默认值			default 'xx'
#唯一			unique
#指定字符集		charset
#主键			primary key

MariaDB [(none)]> create table scores(id int auto_increment primary key,s_id int not null,grade float not null);                                                

插入数据

MariaDB [wg]> insert into students(id,name,sex,phone) values(122,'wg','男','110');
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [wg]> insert into students values (111,'wg','女','122',123);
Query OK, 1 row affected, 1 warning (0.00 sec)


MariaDB [wg]> drop tables students;
Query OK, 0 rows affected (0.00 sec)

MariaDB [wg]> truncate scores;    #快速删除表数据,自增长id从头在来,快速,从磁盘直接删除,不可恢复
Query OK, 0 rows affected (0.00 sec)

MariaDB [wg]> delete from scores;     #删除整个表的数据,自增长继续
Query OK, 0 rows affected (0.00 sec)

该表名

MariaDB [wg]> alter table 1708a rename 8a;   #old  - new
Query OK, 0 rows affected (0.00 sec)

1.modify修改表结构

MariaDB [wg]> desc 8a;   #修改前
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
MariaDB [wg]> alter table 8a modify id varchar(20);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [wg]> desc 8a;  #修改后 发现 id 的type发生改变
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

2.change修改表结构

MariaDB [wg]> desc 8a;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(16) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
MariaDB [wg]> alter table 8a change id scoresss varchar(10);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [wg]> desc 8a;   #我们发现filed 和type 都发生改变
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| scoresss | varchar(10) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

新增字段


MariaDB [wg]> desc 8a;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

MariaDB [wg]> alter table 8a add age float after id;   #新增字段的位置放在了id后面
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [wg]> desc 8a;     
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(10) | YES  |     | NULL    |       |
| age   | float       | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)


不指定条件,修改所有
MariaDB [wg]> update student set age=18;
#不指定条件修改所有
MariaDB [wg]> select * from  8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   12 |
|    2 | NULL |    2 |
|    3 | NULL |    4 |
|    4 | wg   |   13 |
|    5 | g    |   16 |
+------+------+------+
5 rows in set (0.00 sec)

MariaDB [wg]> update student set age=18;
ERROR 1146 (42S02): Table 'wg.student' doesn't exist
MariaDB [wg]> update 8a set age=18;
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

MariaDB [wg]> select * from  8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    4 | wg   |   18 |
|    5 | g    |   18 |
+------+------+------+
5 rows in set (0.00 sec)

指定条件
MariaDB [wg]> update 8a set age=19 where name=‘wg’;
#只修改网工
MariaDB [wg]> select * from  8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    4 | wg   |   18 |
|    5 | g    |   18 |
+------+------+------+
5 rows in set (0.00 sec)

MariaDB [wg]> update 8a set age=19 where name='wg';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [wg]> select * from  8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    4 | wg   |   19 |
|    5 | g    |   18 |
+------+------+------+
5 rows in set (0.00 sec)

查看新建表语句
mysql> show create table tablename;

MariaDB [wg]> show create table 8a;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                          |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| 8a    | CREATE TABLE `8a` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `age` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+

查看所有表
MariaDB [wg]> show tables;

MariaDB [wg]> show tables;
+--------------+
| Tables_in_wg |
+--------------+
| 8a           |
| scores       |
+--------------+
2 rows in set (0.00 sec)

查询两条数据

MariaDB [wg]> select * from 8a limit 2;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
+------+------+------+
2 rows in set (0.00 sec)

从第几条开始查询,下面的n条

#下表从0开始
MariaDB [wg]> select * from 8a limit 1,3;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    4 | wg   |   19 |
+------+------+------+
3 rows in set (0.00 sec)


指定条件
MariaDB [wg]> select * from 8a where age=18;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    5 | g    |   18 |
+------+------+------+
4 rows in set (0.00 sec)


指定多个条件查询必须同时满足

MariaDB [wg]> select * from 8a where name='g' and age>10;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    5 | g    |   18 |
+------+------+------+
1 row in set (0.00 sec)

多个条件,有一个满足即可

MariaDB [wg]> select * from 8a where age=18 or name='null';
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    5 | g    |   18 |
+------+------+------+
4 rows in set (0.00 sec)

取反查找
<>也是不等于

MariaDB [wg]> select * from 8a where age!=18;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | wg   |   19 |
+------+------+------+
1 row in set (0.00 sec)

#<>也是不等于
MariaDB [wg]> select * from 8a where age<>18;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | wg   |   19 |
+------+------+------+
1 row in set (0.00 sec)
模糊匹配查找
#%代表的是通配符,必须得用like
MariaDB [wg]> select * from 8a where name like '%g%';
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | wg   |   19 |
|    5 | g    |   18 |
+------+------+------+
2 rows in set (0.00 sec)

查找任意一个单字符
#_通配符表示任意一个单字符,姚字后面只能跟一个字
MariaDB [wg]> select * from 8a where name like 'w_';
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | wg   |   19 |
+------+------+------+
1 row in set (0.00 sec)

为表起别名

查询数据时,如果表名很长,使用起来不方便,此时,就可以为表取一个别名,用这个别名来代替表的名称SELECT * FROM 表名 [AS] 别名
注意,为表指定别名,AS关键字可以省略不写

`8a`表,取别名`a`,并查询`8a`表中age字段值为`19`的记录
MariaDB [wg]> select * from 8a as a where age='19';
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | wg   |   19 |
+------+------+------+
1 row in set (0.00 sec)


为字段取别名

在查询数据时,为了使显示的查询结果更加直观,可以为字段取一个别名
SELECT 字段名 [AS] 别名 [,字段名 [AS] 别名,……] FROM 表名;
注意,为字段指定别名,AS关键字可以省略不写

查询student表中所有记录的,id和age字段值,并为这两个字段起别名,s_id和s_age;

MariaDB [wg]> select id as s_id, age s_age from 8a;
+------+-------+
| s_id | s_age |
+------+-------+
|    1 |    18 |
|    2 |    18 |
|    3 |    18 |
|    4 |    19 |
|    5 |    18 |
+------+-------+
5 rows in set (0.00 sec)


between 在什么和什么之间的数据
MariaDB [wg]> select * from 8a as a where age between 1 and 100;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | NULL |   18 |
|    2 | NULL |   18 |
|    3 | NULL |   18 |
|    4 | wg   |   19 |
|    5 | g    |   18 |
+------+------+------+
5 rows in set (0.00 sec)

MariaDB [wg]> select * from 8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | bw   |    5 |
|    2 | yjs  |    1 |
|    3 | cm   |   10 |
|    4 | wg   |   19 |
|    5 | g    |   18 |
+------+------+------+
5 rows in set (0.00 sec)


排序(升降)
#降序desc
MariaDB [wg]> select * from 8a order by age desc;  
+------+------+------+
| id   | name | age  |
+------+------+------+
|    4 | wg   |   19 |
|    5 | g    |   18 |
|    3 | cm   |   10 |
|    1 | bw   |    5 |
|    2 | yjs  |    1 |
+------+------+------+
5 rows in set (0.00 sec)

#asc升序
MariaDB [wg]> select * from 8a order by age asc;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    2 | yjs  |    1 |
|    1 | bw   |    5 |
|    3 | cm   |   10 |
|    5 | g    |   18 |
|    4 | wg   |   19 |
+------+------+------+
5 rows in set (0.00 sec)

查询字段为空的数据

MariaDB [wg]> select * from 8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | bw   |    5 |
|    2 | yjs  |    1 |
|    3 | cm   |   10 |
|    4 | wg   |   19 |
|    5 | g    |   18 |
|    1 | NULL |    5 |
|    1 | yx   | NULL |
+------+------+------+
7 rows in set (0.00 sec)


查询字段为空的数据

MariaDB [wg]> select * from 8a  where name='' or age is null;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | yx   | NULL |
+------+------+------+
1 row in set (0.00 sec)

去重

MariaDB [wg]> select * from 8a;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | bw   |    5 |
|    2 | yjs  |    1 |
|    3 | cm   |   10 |
|    4 | wg   |   19 |
|    5 | g    |   18 |
|    1 | NULL |    5 |
|    1 | yx   | NULL |
|    7 | NULL |    5 |
+------+------+------+
8 rows in set (0.00 sec)

MariaDB [wg]> select distinct age from 8a;
+------+
| age  |
+------+
|    5 |
|    1 |
|   10 |
|   19 |
|   18 |
| NULL |
+------+
6 rows in set (0.00 sec)

求值
#count统计行数 
MariaDB [wg]> select count(age) from 8a;
+------------+
| count(age) |
+------------+
|          7 |
+------------+
1 row in set (0.00 sec)
#最大值
MariaDB [wg]> select max(age) from 8a;
+----------+
| max(age) |
+----------+
|       19 |
+----------+
1 row in set (0.00 sec)
#最小值
MariaDB [wg]> select min(age) from 8a;
+----------+
| min(age) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)
#平均值
MariaDB [wg]> select avg(age) from 8a;
+----------+
| avg(age) |
+----------+
|        9 |
+----------+
1 row in set (0.00 sec)
#求和
MariaDB [wg]> select sum(age) from 8a;
+----------+
| sum(age) |
+----------+
|       63 |
+----------+
1 row in set (0.00 sec)


你可能感兴趣的:(mysql)