数据库学习_mySQL
Posted Leslie X徐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库学习_mySQL相关的知识,希望对你有一定的参考价值。
mysql
mysql安装
可以在以下网站寻找ubuntu安装包
[https://packages.ubuntu.com/]
在Ubuntu系统下,利用apt-get install安装MySQL时使用以下指令
sudo apt-get install default-mysql-server
验证mysql服务器是否正常运行
sudo systemctl status mysql
保护MySQL
MySQL安装随附一个名为的脚本mysql_secure_installation,可让您轻松提高数据库服务器的安全性。
调用不带参数的脚本:
sudo mysql_secure_installation
命令 sudo mysql 进入程序,quit 退出。
授权操作
MariaDB [(none)]> grant all privileges on mysqldata.* to pi@localhost identified by 'xwh1995' with grant option;
mysql -p //使用密码登陆
类型
字段:
属性:
- int
- float/double
- char[8]/varchar[8] : 不常改变的使用varchar
- NULL
约束:给予某些字段赋予一些条件
- unique: 唯一
- default: 默认约束
- not null: 该字段不能为空
- primary key: 主键——指定这个表单里哪一个字段为主键,查询使用主键来查更快,比如学生的表单主键设置为学号id
- foreign key: 外键——连接其他表单中的某个字段
表达式
加减乘除
逻辑:or, and
like: 模糊查询
通配符:’%‘代替某某(多个),’*‘所有,’_'代替某一个(必须有一个)
一般使用:where + 表达式
如:where
库操作
查询库:show databases;
创建库:create database database_name;
链接库:use database_name;
删除库:drop database_name;
查看当前使用的数据库: select database();
MariaDB [(none)]> CREATE DATABASE mysqldata;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysqldata |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
表操作
表操作 | 指令 |
---|---|
查看数据库中所有表 | show tables; |
创建表 | create table table_name(字段 类型 约束); |
若创建字段需要使用中文 | 则在后面加上 charset=utf8; |
查看表头信息 | desc table_name; 或者 show columns from table_name; |
删除表单 | drop table_name; |
重命名表单 | alter table t1 rename t2; |
增加字段 | alter table 表名 add字段 类型 其他; |
表操作 | 指令 |
---|---|
查看表单内容 | select * from table_name where 表达式; |
查看表单根据排序 | select * from MyClass order by id asc/desc; |
查询前几行数据 | select * from MyClass limit 0,2; |
插入内容 | insert into table_name(insert_element) values(insert_value) |
更新表单内容 | update table_name set update_element=update_value where 表达式; |
删除表单内容 | delete from table_name where 表达式; |
MariaDB [(none)]> use mysqldata
MariaDB [(none)]> create table student(
-> id int(4) not null primary key auto_increment,
-> name char(20) not null,
-> sex int(4) not null default '0',
-> degree double(16,2)
-> );
MariaDB [mysqldata]> desc student
-> ;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| sex | int(4) | NO | | 0 | |
| degree | double(16,2) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
4 rows in set (0.005 sec)
MariaDB [mysqldata]> select * from student;
+----+------+-----+--------+
| id | name | sex | degree |
+----+------+-----+--------+
| 1 | Tom | 0 | 89.00 |
| 2 | John | 0 | 90.00 |
+----+------+-----+--------+
2 rows in set (0.001 sec)
MariaDB [mysqldata]> insert into tb_stu values
-> (3,'Max',0,99),
-> (4,'Jack',1,78),
-> (5,'Babala',1,67);
Query OK, 3 rows affected (0.007 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [mysqldata]> select * from tb_stu where sex=1;
+----+--------+-----+--------+
| id | name | sex | degree |
+----+--------+-----+--------+
| 4 | Jack | 1 | 78.00 |
| 5 | Babala | 1 | 67.00 |
+----+--------+-----+--------+
2 rows in set (0.001 sec)
MariaDB [mysqldata]> select * from tb_stu where sex=0;
+----+------+-----+--------+
| id | name | sex | degree |
+----+------+-----+--------+
| 1 | Tom | 0 | 89.00 |
| 2 | John | 0 | 90.00 |
| 3 | Max | 0 | 99.00 |
+----+------+-----+--------+
3 rows in set (0.001 sec)
MariaDB [mysqldata]> select * from tb_stu order by id desc;
+----+--------+-----+--------+
| id | name | sex | degree |
+----+--------+-----+--------+
| 5 | Babala | 1 | 67.00 |
| 4 | Jack | 1 | 78.00 |
| 3 | Max | 0 | 99.00 |
| 2 | John | 0 | 90.00 |
| 1 | Tom | 0 | 89.00 |
+----+--------+-----+--------+
5 rows in set (0.000 sec)
MariaDB [mysqldata]> select * from tb_stu order by id asc;
+----+--------+-----+--------+
| id | name | sex | degree |
+----+--------+-----+--------+
| 1 | Tom | 0 | 89.00 |
| 2 | John | 0 | 90.00 |
| 3 | Max | 0 | 99.00 |
| 4 | Jack | 1 | 78.00 |
| 5 | Babala | 1 | 67.00 |
+----+--------+-----+--------+
5 rows in set (0.001 sec)
MariaDB [mysqldata]> select * from tb_stu
-> where degree>80 and degree<100;
+----+------+-----+--------+
| id | name | sex | degree |
+----+------+-----+--------+
| 1 | Tom | 0 | 89.00 |
| 2 | John | 0 | 90.00 |
| 3 | Max | 0 | 99.00 |
+----+------+-----+--------+
3 rows in set (0.001 sec)
MariaDB [mysqldata]> select * from tb_stu
-> where degree<80;
+----+--------+-----+--------+
| id | name | sex | degree |
+----+--------+-----+--------+
| 4 | Jack | 1 | 78.00 |
| 5 | Babala | 1 | 67.00 |
+----+--------+-----+--------+
2 rows in set (0.001 sec)
MariaDB [mysqldata]>
以上是关于数据库学习_mySQL的主要内容,如果未能解决你的问题,请参考以下文章
[知了堂学习笔记]_Java代码实现MySQL数据库的备份与还原