Mysql installation and basic use

MySQL

1. The definition of the database:

A database refers to a collection of organized and shared data stored in a computer for a long time and the relationship between the data. The data in the database is organized, described, and stored according to a certain data model, has less redundancy, higher data independence and easy scalability, and can be shared by multiple users within a certain range.

Second, the installation of Mysql in windows

From Oracle's official website, you can download the installed version and decompressed version of MySql. The URL is attached below
https://dev.mysql.com/downloads/mysql/

MySql Enterprice Edition Enterprise Edition-charge or try
MySql Community Edition Community Edition-free
MySql Classic Edition Classic Edition-free

1. Download

After downloading, unzip the compressed package to the location you want to install, open win+r, and enter cmd into the command window. Use the following command.

cd:/解压包路径/bin

2. Initialization operation

mysqld --initialize --console

It will display as follows: Copy the automatically generated default password and go to the next step.
Insert picture description here

3. Add mysql to the system service

mysqld --install

4. Start the service

net start mysql

5. Log in to the database

mysql -uroot -p   +回车之后直接粘贴刚刚复制的密码,回车。如果报错,很可能是你复制到时候,操作出了问题。

6. Modify password

Use the following code directly, and change the "new password" in the quotes to the one you want to change. It is recommended to use a simpler password.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

7. Other operations

net stop mysql  //停止服务
mysqld --remove   //卸载服务
select version()   //查看当前mysql的版本
如果忘记密码,则删除data文件夹,重新执行2-6即可

Three, create a library, table

1. How to create a database:

create database 数据库名  default character set utf8;  默认 utf8编码
create database 数据库名                                  也是创建数据库的
show databases                         查询所有的数据库
use  数据库名                         切换到你想要用的数据库

Insert picture description here
Insert picture description here

2. How to create a table

 create table 表名称(列名称 数据类型 约束规则,...) engine=数据库引擎 default charset utf8;
 create table 表名称(列名称 数据类型 约束规则,...) ;

mysql> create table t_student( 
-> id bigint primary key, 
-> name varchar(20) not null, 
-> sex boolean default 1 
-> ) engine=innodb default charset utf8;

具体的语法规则:
CREATE TABLE <表名>
–(<列名> <数据类型> [ <列级完整性约束条件> ]
–[,<列名><数据类型>[ <列级完整性约束条件>] ] …
–[,<表级完整性约束条件> ] );
其中:
<表名>:所要定义的基本表的名字
 <列名>:组成该表的各个属性(列)
<列级完整性约束条件>:涉及相应属性列的完整性约束条件
<表级完整性约束条件>:涉及一个或多个属性列的完整性约束条件

3. Delete a table

 drop table 表名称;                如果存在则删除表,否则报错
 drop table if exists 表名称;;            如果存在则删除表,不存在 也不会报错

4. Numerical column types in mysql

Insert picture description here

5. String column type in Mysql

Insert picture description here
The difference between char and varchar
1, char is fixed-length data, if char(10) but only contains 1 character, its actual length is still 10. Mysql processes the results of the query, so length cannot be used to determine the length it occupies; in varchar(10), if only 1 character is stored in it, its length is 1, so varchar is called variable length Character
2, the value range of char is 0-255; the value range of varchar is 0-65535. If the length of the string to be stored is not much different from the average length of all values, char, such as MD5, is suitable. For values ​​that change frequently, char is better than varchar, because fixed-length rows are not prone to fragmentation.
3. For very short columns, char is better than varchar, because varchar requires one or two extra bytes to store the length of the string.
4. If there is a space after char and varchar, char will automatically remove the space and store it, although varchar does not Will remove spaces, but when comparing strings, it will remove spaces for comparison

6. Large object type

Insert picture description here

7, set and enumeration

Insert picture description here

enum枚举类型实际上是当作字符串进行处理,设置当前列中所允许的取值,取值只能是枚举值中的任意一个值
create table t1(id enum(‘a’,’b’,’c’));
set集合类型实际上是当作字符串进行处理,设置当前列中所允许的取值,取值可以是集合中的任意多个值
create table t1(id set(‘a’,’b’,’c’));

8. Date and time column types

Insert picture description here

9, modify the basic table alter

修改表  ALTER TABLE <表名>
[ ADD <新列名> <数据类型> [ 完整性约束] ]
[ DROP <完整性约束名> ]
[ MODIFY <列名> <数据类型> ];
说明:
<表名>:要修改的基本表
ADD子句:增加新列和新的完整性约束条件
DROP子句:删除指定的完整性约束条件
MODIFY子句:用于修改列名和数据类型

Guess you like

Origin blog.csdn.net/weixin_42437438/article/details/115355586