您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

Mysql数据库必备基础命令

1.安装

[root@localhost ~]# yum install mariadb mariadb-server  #安装
[root@localhost ~]# systemctl start mariadb 
[root@localhost ~]# netstat -pltun|grep mysql  #默认端口是3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      13650/mysqld        
[root@localhost ~]# 

2.mysql创建库、创建表

 

#1.查看所有的数据库

#2.创建新的库

#3.进入新创建的库

#4.查看库里有多少表

ariaDB [(none)]> show databases;  #查看当前所有库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> create database ws character set utf8 collate utf8_bin;  #创建库 编码是utf格式
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;       
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sool               |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> use sool
Database changed
MariaDB [sool]> show tables;
Empty set (0.00 sec)

MariaDB [sool]> create table cake(id int(8) primary key auto_increment, name char (10)); #创建一个表  设置成自增
Query OK, 0 rows affected (0.00 sec)

3、增

 insert info 表名 values(字段,字段)

MariaDB [sool]> insert into t1 values(1,"张三");
Query OK, 1 row affected, 1 warning (0.00 sec)
MariaDB [sool]> select id=1 from t1  #这就是根据字段IP查询
    -> ;
MariaDB [nncter]> select *from cake
    -> ;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 五门   |
|  3 | 七天   |
|  4 | 保思   |
|  5 | 聆三   |
+----+--------+

 4.删

   delete from 表面 where#判断 字段

MariaDB [nncter]> select *from cake
    -> ;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 五门   |
|  3 | 七天   |
|  4 | 保思   |
|  5 | 聆三   |

MariaDB [nncter]> delete from cake where id=2;
MariaDB [nncter]> select * from cake;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  3 | 七天   |
|  4 | 保思   |
|  5 | 聆三   |
+----+--------+


5.删除整个表

MariaDB [nncter]> show tables;
+------------------+
| Tables_in_nncter |
+------------------+
| cake             |
| same             |
+------------------+
2 rows in set (0.00 sec)
MariaDB [nncter]> drop table same;
Query OK, 0 rows affected (0.03 sec)
MariaDB [nncter]> show tables;
+------------------+
| Tables_in_nncter |
+------------------+
| cake             |
+------------------+
1 row in set (0.00 sec)

6.查

MariaDB [nncter]> select  * from cake ; #全局查询 不建议
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  3 | 七天   |
|  4 | 保思   |
|  5 | 聆三   |
+----+--------+
4 rows in set (0.00 sec)

#or和and查询   or是或者  and是并且
MariaDB [nncter]> select * from cake where id>=1 and id<=4; 
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  3 | 七天   |
|  4 | 保思   |
+----+--------+
3 rows in set (0.00 sec)

#加判断查询
MariaDB [nncter]> select id=1 from cake   where name="张三";
+------+
| id=1 |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

7.改

修改表名

MariaDB [nncter]> alter table cake rename to cake1;
Query OK, 0 rows affected (0.00 sec)

MariaDB [nncter]> show tables;
+------------------+
| Tables_in_nncter |
+------------------+
| cake1            |
+------------------+
1 row in set (0.00 sec)

MariaDB [nncter]> 

 修改表结构

我们知道通过desc语句可以查看一张表的结构,例如查看stu表的结构

MariaDB [nncter]> desc cake1;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(8)   | NO   | PRI | NULL    | auto_increment |
| name  | char(10) | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.03 sec)

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进