MySQL数据库学习笔记一

Posted hhh江月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL数据库学习笔记一相关的知识,希望对你有一定的参考价值。

mysql数据库学习笔记一

文章目录

一、 Install MySQL.

Windows install mysql:download from the internet.

run the mysql

net start mysql

this is to run the mysql server.(maybe needed root.)

login in the mysql

mysql -h localhost -u user_name -p

and then, input password.

or the code showed below is also available to login in :

mysql -u user_name -p

and then, input password.

二、 Manage MySQL.

Login

in my computer, i need to do the things below :

first step

cd C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin

second step

mysql -u root -p

third step

671513

(this is the password.)

Add users(adding users)

this is to add an user :

root@host# mysql -u root -p
Enter password:*******
mysql> use mysql;
Database changed

mysql> INSERT INTO user 
          (host, user, password, 
           select_priv, insert_priv, update_priv) 
           VALUES ('localhost', 'guest', 
           PASSWORD('guest123'), 'Y', 'Y', 'Y');
Query OK, 1 row affected (0.20 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 1 row affected (0.01 sec)

mysql> SELECT host, user, password FROM user WHERE user = 'guest';
+-----------+---------+------------------+
| host      | user    | password         |
+-----------+---------+------------------+
| localhost | guest | 6f8c114b58f2ce9e |
+-----------+---------+------------------+
1 row in set (0.00 sec)

Select database

show databases;
use database_name;
show tables;
show columns from table_name;
show index from table_name;

三、Connect MySQL

normally connect mysql

mysql -u root -p
[root@host]# mysql -u root -p
Enter password:******

use port to connect remote mysql

for example:

mysql -u root -P 3307 -h 101.200.152.192 -p

or:

C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin>mysql -u root -P 3306 -h localhost -p
Enter password: ******

(password is 671513(this is mine.) and the port of the mysql is 3306 if not changed.)

四、MySQL Database Operation

create database

create database database_name;

delete database

drop database database_name;

if the databses does not, we should use the sentence below :

mysql> drop database if exists database_name;
Query OK, 0 rows affected, 1 warning (0.00 sec)

show and select databse

for example :

mysql> create test_db_1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test_db_1' at line 1
mysql> create database test_db_1;
Query OK, 1 row affected (0.01 sec)

mysql> create database if not exists test_db_1;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| test_db_1          |
| world              |
+--------------------+
7 rows in set (0.00 sec)

mysql> use test_db_1;
Database changed
mysql>



(attention that using if not exists can avoid some errors.)

五、MySQL的数据类型有关内容:

1、数字类型:

类型大小范围(有符号)范围(无符号)用途
TINYINT1Bytes(-128,127)(0,255)小整数值
SMALLINT2Bytes(-32768,32767)(0,65535)大整数值
MEDIUMINT3Bytes(-8388608,8388607)(0,16777215)大整数值
INT或INTEGER4Bytes(-2147483648,2147483647)(0,4294967295)大整数值
BIGINT8Bytes极大整数值
FLOAT4Bytes单精度浮点数值
DOUBLE8Bytes双精度浮点数值

2、时间类型:

类型大小范围(有符号)格式用途
DATE3Bytes1000-01-01/9999-12-31YYYY-MM-DD日期值
TIME3Bytes‘-838:59:59’/‘838:59:59’HH:MM:SS时间值或持续时间
YEAR1Bytes1901/2155YYYY年份值
DATETIME8Bytes1000-01-01 00:00:00/9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS混合日期与时间值
TIMESTAMP4Bytes1970-01-01 00:00:00/2038YYYYMMDD HHMMSS混合日期和时间值,时间戳

3、字符串类型:

类型大小用途
CHAR0-255Bytes定长字符串
VARCHAR0-65535Bytes变长字符串
TINYBLOB0-255Bytes不超过255个字符的二进制字符串
TINYTEXT0-65535Bytes短文本字符串
BLOB0-65535Bytes二进制形式的长文本数据
TEXT0-65535Bytes长文本数据
MEDIUMBLOB0-16777215Bytes二进制形式的中等长度文本数据
MEDIUMTEXT0-16777215Bytes中等长度文本数据
LONGBLOB0-4294967295Bytes二进制形式的极大文本数据
LONGTEXT0-4294967295Bytes极大文本数据

(字符创的小问题:char(n) 和 varchar(n) 中括号中 n 代表字符的个数,并不代表字节个数,比如 CHAR(30) 就可以存储 30 个字符。CHAR 和 VARCHAR 类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。)

六、总结

这是一些MySQL数据库的基本操作,希望对大家有一些帮助,谢谢大家的阅读与支持,后续还会继续推出相关的内容的。

谢谢大家的阅读与支持。

以上是关于MySQL数据库学习笔记一的主要内容,如果未能解决你的问题,请参考以下文章

MySQL学习笔记

数据类型和运算符 MySQL学习笔记

MySql 数据类型

MySql 数据类型

MySql 数据类型

MySQL学习笔记-数据类型与操作数据表