数据库基础

1.mysql安装

mysql安全设置(系统会一路问你几个问题,看不懂复制之后翻译,基本上一路yes):

 mysql_secure_installation

 mysql安装完后不允许远程连接  执行以下代码

grant all PRIVILEGES on *.* to root@'%' identified by 'root' WITH GRANT OPTION;

 2.数据库的简单用法

一、新建数据库,数据库名为anyun

mysql> create database anyun;
Query OK, 1 row affected (0.00 sec)

 二、查看数据库

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| anyun              |

| mysql              |

| performance_schema |

| qqq                |

+--------------------+

5 rows in set (0.00 sec)

 三、使用数据库

mysql> use anyun;

Database changed

 四、创建表

mysql> CREATE TABLE world(

    -> id INT NOT NULL AUTO_INCREMENT,

    -> name VARCHAR(100) NOT NULL,

    -> parent_id INT NOT NULL,

    -> level INT NOT NULL,

    -> PRIMARY KEY (id)

    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.43 sec)

 五、查看表结构

mysql> desc world;

+-----------+--------------+------+-----+---------+----------------+

| Field     | Type         | Null | Key | Default | Extra          |

+-----------+--------------+------+-----+---------+----------------+

| id        | int(11)      | NO   | PRI | NULL    | auto_increment |

| name      | varchar(100) | NO   |     | NULL    |                |

| parent_id | int(11)      | NO   |     | NULL    |                |

| level     | int(11)      | NO   |     | NULL    |                |

+-----------+--------------+------+-----+---------+----------------+

 六、插入数据

mysql> insert into world(name,parent_id,level) values ('上海市',0,1),('北京市',0,1),('湖北省',0,2),('江苏省',0,1),('重庆市',0,2);

Query OK, 5 rows affected (0.02 sec)

Records: 5  Duplicates: 0  Warnings: 0

 七、查询world表的所以数据

mysql> select * from world;

+----+-----------+-----------+-------+

| id | name      | parent_id | level |

+----+-----------+-----------+-------+

|  1 | 上海市    |         0 |     1 |

|  2 | 北京市    |         0 |     1 |

|  3 | 湖北省    |         0 |     2 |

|  4 | 江苏省    |         0 |     1 |

|  5 | 重庆市    |         0 |     2 |

+----+-----------+-----------+-------+

5 rows in set (0.00 sec)

猜你喜欢

转载自www.cnblogs.com/hackxf/p/8861809.html