说简单了这种关系型数据库和excel差别不大 。说难了关系型数据库就像一个效率更高,存贮更加方便,安全性更好的多个excel。
至于每张表,会让你联系到excel吧(这个是ubuntu里面的liberoffice cale)
这个是mysql
+------+------+-----------+---------+--------------+
| name | age | class | teacher | school |
+------+------+-----------+---------+--------------+
| hzn | 1 | 六(1)班 | adl | 西安邮电 |
| hzn | 1 | 六(1)班 | adl | 西安邮电 |
| adl | 2 | 八(1)班 | hzn | 西安邮电 |
+------+------+-----------+---------+--------------+
掌握了mysql将可以更好的帮我们对于数据的储存和利用。你想一个大型游戏那么多用户,或者银行账户总不能完全把重要的用户信息储存在某几个没有安全保障的文件里吧,既不好操作数据的变化,也不好保护。
可以说数据库是伴随着信息的量化增长产生的。
我们可以学习一些简单的逻辑代码,操作大量的数据。
或者是把我们c++,java中的代码导入到mysql中。
言归正传,
首先你得下载mysql,
在Linux的ubuntu下 sudo apt-get install mysql-server下载;
记得新手第一次在root下直接 mysql 登录使用
后面学会了创建账户再用mysql -u (user)-p (passwd)登录
进去以后
是它给你给你的mysql>脚本界面
新手记得输入的每句话加 ;
输入show databases 可以看到很多数据库
| information_schema |
| mysql |
| performance_schema |
| sys |
这几个平时别乱调,系统自带;
我们自己去建几个数据库自己玩。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db1 |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| test1 |
+--------------------+
7 rows in set (0.00 sec)
使用 命令 CREATE DATABASE db1 就可以创建一个新的数据库db1.
mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.01 sec)
使用use db1 就可以切换到db1这个数据库中
mysql> use db1
Database changed
了解下mysql里基本的数据类型
基本数据类型
你可以选择看好建表后先去跳着看后面的DML.
DDL(对于表的增删改查)
1.建表 create table ((属性名)(类型),(属性名)(类型),……);
mysql> show tables
-> ;
Empty set (0.00 sec)
mysql> create table stu(name varchar(32),age int,class varchar(32),teacher varchar(32),school varchar(32));
Query OK, 0 rows affected (0.04 sec)
2. 查表 show tables ;
查看当前数据库有哪些表
mysql> show tables
-> ;
+---------------+
| Tables_in_db1 |
+---------------+
| stu |
| stu2 |
+---------------+
2 rows in set (0.00 sec)
说明在db1 这个table中有stu ,stu2两张表
3.查表结构.desc 表名;
mysql> desc stu
-> ;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | varchar(32) | YES | | NULL | |
| teacher | varchar(32) | YES | | NULL | |
| school | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
4.删表drop table 表名;
mysql> drop table sad
-> ;
Query OK, 0 rows affected (0.02 sec)
5.复制表create table 表名 (新)like 表名(旧)
mysql> create table stu10 like stu;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from stu10;
Empty set (0.00 sec)
mysql> select * from stu;
+------+------+-----------+---------+--------------+
| name | age | class | teacher | school |
+------+------+-----------+---------+--------------+
| adl | 18 | 八(1)班 | hzn | 西安邮电 |
+------+------+-----------+---------+--------------+
1 row in set (0.00 sec)
mysql> desc stu10;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | varchar(32) | YES | | NULL | |
| teacher | varchar(32) | YES | | NULL | |
| school | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
可见复制表仅仅是复制了表的属性,而不是内容。
6.改表名 alter table 表名(旧) rename to 表名(新)
mysql> desc stu10;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | varchar(32) | YES | | NULL | |
| teacher | varchar(32) | YES | | NULL | |
| school | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table stu10 rename to stu11;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu |
| stu11 |
| stu2 |
+---------------+
3 rows in set (0.00 sec)
7. alter table 表名 character set 编码模式(utf8 /gbk/……);
mysql> alter table stu11 character set gbk;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table stu11 ;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stu11 | CREATE TABLE `stu11` (
`name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`age` int DEFAULT NULL,
`class` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`teacher` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`school` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
8.添加属性 alter table 表名 add 属性 数据类型
mysql> alter table stu11 add score int
-> ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select *
-> from stu11;
Empty set (0.00 sec)
mysql> desc stu11;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | varchar(32) | YES | | NULL | |
| teacher | varchar(32) | YES | | NULL | |
| school | varchar(32) | YES | | NULL | |
| score | int | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
9.修改表属性名 alter table 表名 change 属性名(旧) 属性名(新)类型
mysql> alter table stu11 change score scores varchar(32);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu11;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | varchar(32) | YES | | NULL | |
| teacher | varchar(32) | YES | | NULL | |
| school | varchar(32) | YES | | NULL | |
| scores | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
同时 alter table 表名 modify 属性名 类型 可以只修改类型
10.删除属性 alter table 表名 drop 属性;
mysql> alter table stu11 drop scores;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu11;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | varchar(32) | YES | | NULL | |
| teacher | varchar(32) | YES | | NULL | |
| school | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
DML(对于表中数据 增删改 )+DQL(查)
1.插入数据 insert into 表名+values(数据)
mysql> insert into stu values("hzn",1,"六(1)班","adl","西安邮电"),("adl",2,"八(1)班","hzn","西安邮电");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
在idea链接数据库后在idead的console中字符串只能用单引号,不能用双引号
2.查看数据 select (属性名)或者(*) from 表名(可后加where,where为筛选条件)
mysql> select * from stu
-> ;
+------+------+-----------+---------+--------------+
| name | age | class | teacher | school |
+------+------+-----------+---------+--------------+
| hzn | 1 | 六(1)班 | adl | 西安邮电 |
| hzn | 1 | 六(1)班 | adl | 西安邮电 |
| adl | 2 | 八(1)班 | hzn | 西安邮电 |
+------+------+-----------+---------+--------------+
3 rows in set (0.00 sec)
3.delete from 表名 (where为筛选条件 )(若不加where将导致整张表删除~~)
mysql> delete from stu where name="hzn";
Query OK, 2 rows affected (0.01 sec)
mysql> select * from stu ;
+------+------+-----------+---------+--------------+
| name | age | class | teacher | school |
+------+------+-----------+---------+--------------+
| adl | 2 | 八(1)班 | hzn | 西安邮电 |
+------+------+-----------+---------+--------------+
1 row in set (0.00 sec)
同时 用这个truncate table 表名可以直接清空表
mysql> truncate table stu2;
Query OK, 0 rows affected (0.02 sec)
4.update 表名 set 属性=值,属性=值…… (可用where)(若不加where将导致整张表所有行的这些属性修改~)
mysql> update stu set age=18 where name="adl";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stu;
+------+------+-----------+---------+--------------+
| name | age | class | teacher | school |
+------+------+-----------+---------+--------------+
| adl | 18 | 八(1)班 | hzn | 西安邮电 |
+------+------+-----------+---------+--------------+
1 row in set (0.00 sec)
- if exists 如果存在
- where 遍历查找
- group by 分组
- having 分组后的条件
- order by 排序
- limit 分页
他们是重要的条件查询。
未完待续----------