1. 程式人生 > 其它 >MySQL基礎命令

MySQL基礎命令

連線遠端MySQL

mysql -h 110.110.110.110 -u root -p

連結本地MySQL

mysql -u root -p

檢視資料庫

show databases;

結果

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| php                |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

選擇資料庫

use mysql;

檢視資料表

show tables;

建立資料庫

create database school_info;

刪除資料庫

drop database school_info;

建立表

CREATE TABLE `school_info`.`school`  (
  `id` bigint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NULL COMMENT '學校名稱',
  `create_at` bigint(11) NULL,
  `update_at` bigint(11) NULL,
  `status` tinyint(4) NULL DEFAULT 0 COMMENT '狀態',
  PRIMARY KEY (`id`)
) ENGINE = InnoDB COMMENT = '學校資訊表';

檢視資料表的結構

desc school;

結果

+-----------+---------------------+------+-----+---------+----------------+
| Field     | Type                | Null | Key | Default | Extra          |
+-----------+---------------------+------+-----+---------+----------------+
| id        | bigint(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name      | varchar(200)        | YES  |     | NULL    |                |
| create_at | bigint(11)          | YES  |     | NULL    |                |
| update_at | bigint(11)          | YES  |     | NULL    |                |
| status    | tinyint(4)          | YES  |     | 0       |                |
+-----------+---------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

新增表資料 - 插入記錄

insert into school values(2,"學校3",1625568499,1625568499,1);
或
insert into school(name,create_at,update_at,status) values("學校4",1625568499,1625568499,1);

檢視資料表的內容

select * from school;

結果

+----+---------+------------+------------+--------+
| id | name    | create_at  | update_at  | status |
+----+---------+------------+------------+--------+
|  1 | 學校1   | 1625568499 | 1625568499 |      1 |
|  2 | 學校2   | 1625568499 | 1625568499 |      1 |
|  3 | 學校3   | 1625568499 | 1625568499 |      1 |
|  4 | 學校4   | 1625568499 | 1625568499 |      1 |
+----+---------+------------+------------+--------+
4 rows in set (0.00 sec)

新增表字段

alter table school add `adress` varchar(200) NULL COMMENT '地址' after `name`;

刪除列

alter table school drop `adress`;

修改表記錄

update school set `name`='學校名稱1' where `id`=1;

刪除表記錄

delete from school where `id`=4;

清空表記錄

truncate table school;

刪除資料表

drop table if exists school;