啟動(dòng)mariadb
linux系統(tǒng)下敲 systemctl start mariadb
搜索mariadb的端口號(hào)(默認(rèn)是3306) 可以去阿里云安全組打開這個(gè)端口,使得外網(wǎng)可以訪問我們自己服務(wù)器上的MySQL數(shù)據(jù)庫
ps - ef | grep mariadb
啟動(dòng)數(shù)據(jù)庫
mysql -u root -p
然后輸入自己之前設(shè)置的密碼就可以連接上mysql數(shù)據(jù)庫
(設(shè)置密碼命令:create user 'root'@'%' identified by '123456';
;
刷新:flush privileges;
)
使用select version();命令可以查看數(shù)據(jù)庫版本型號(hào)
MariaDB [(none)]> select version();
+----------------+
| version() |
+----------------+
| 5.5.60-MariaDB |
+----------------+
1 row in set (0.00 sec)
使用show databases;可以查看mysql中所有的數(shù)據(jù)庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
使用可視化工具navicat來更好的操作mysql:
- 安裝:安裝包安裝(一直點(diǎn)next安裝就行了)
- 安裝好了之后進(jìn)入軟件界面 首先點(diǎn)擊連接選擇需要連接到哪個(gè)(數(shù)據(jù)庫)
MySQL
MariaDB
Amazon的數(shù)據(jù)庫
阿里云服務(wù)器上的MySQL
騰訊云上的
- 我們使用的阿里云的服務(wù)器所有選擇阿里云,然后填寫名字,填寫自己服務(wù)器公網(wǎng)地址號(hào),然后輸入自己MySQL的密碼,默認(rèn)端口為3306
- 創(chuàng)建之后沒有顯示數(shù)據(jù)庫中的數(shù)據(jù),需要給文件權(quán)限:
grant all privileges on . to 'root'@'%' with grant option;
給MySQL中所有文件權(quán)限
Mysql中的SQL:
SQL是一種語言:可以用來正刪改查數(shù)據(jù)庫中的數(shù)據(jù).
- 創(chuàng)建:
# 創(chuàng)建一個(gè)名為school的數(shù)據(jù)庫并制定默認(rèn)字符集為utf-8
create database school default charset utf8;
- 刪除:
# 如果存在名為school的數(shù)據(jù)庫就刪除它
drop database if exists school;
# 切換到school數(shù)據(jù)庫上下文環(huán)境
use school;
-- 創(chuàng)建學(xué)生表
# 一般表的前面會(huì)有tb或者t或者tbl等前綴
create table tb_student
(
# 學(xué)生的學(xué)號(hào),int表示類型,not null 表示不能為空,可以在這句后面加 comment '學(xué)號(hào)'來當(dāng)注釋..
stuid int not null comment '學(xué)號(hào)',
# 學(xué)生的姓名,varchar(20)表示能輸入的數(shù)據(jù)長度字符最大為20,
stuname varchar(20) not null comment '學(xué)生姓名',
# 學(xué)生性別,可以通過bit來表示,默認(rèn)default 1,
stusex bit default 1 comment 1,
# 學(xué)生生日,可視化工具中date來獲取日歷選擇生日,SQL中直接輸入但是格式是'xxxx-xx-xx'形式
stubirth date comment '生日',
# 設(shè)置主鍵:主鍵是表中唯一的值,一個(gè)表中只要主鍵確定了,那么對(duì)應(yīng)的表的實(shí)例的數(shù)據(jù)也確定了
primary key(stuid)
);
修改學(xué)生表:
# 新增一個(gè)學(xué)生表屬性:stuaddr(家庭住址),并且設(shè)置最大輸入字符為255個(gè)
alter table tb_student add colum stuaddr varchar(255);
# 修改stuaddr屬性最大輸入字符數(shù)為511,
alter table tb_student change column stuaddr stuaddr varchar(511);
# 刪除學(xué)生表中的stuaddr屬性
alter table tb_student drop column stuaddr;
創(chuàng)建一個(gè)學(xué)院表:
-- 學(xué)院表
create table tb_college
(
colid int not null comment '學(xué)院編號(hào)',
colname varchar(20) not null comment '學(xué)院名',
colweb varchar(50) comment '學(xué)院網(wǎng)站',
primary key(colid)
);
-- 學(xué)院表中插入數(shù)據(jù)
-- auto_increment 自動(dòng)增長字段:必須是主鍵,類型必須是int類型,不能為空
insert into tb_college(colid,colname,colweb) values
(001,'計(jì)算機(jī)學(xué)院','www.computer.com'),
(002,'航空維修學(xué)院','www.ewqq.com'),
(003,'物理學(xué)院','www.gdhlj.com');
將學(xué)生表和學(xué)院表關(guān)聯(lián)起來:
-- 修改學(xué)生表添加學(xué)院編號(hào)(colid)列
alter table tb_student add column colid int;
-- 修改學(xué)生表添加外鍵約束(參照完整性)
alter table tb_student add constraint fk_student_colid foreign key (colid) references tb_college (colid);
-- 更新學(xué)生表為學(xué)生指定所屬學(xué)院
update tb_student set colid=1 where stuid between 1001 and 1006;
update tb_student set colid=2 where stuid in (1007, 1008);
update tb_student set colid=3 where stuid=1009;
創(chuàng)建老師表(表中主鍵忘記寫的兩種補(bǔ)救方法):
# 創(chuàng)建老師表
create table tb_teacher
(
teaid int not null comment '工號(hào)',
teaname varchar(20) not null comment '老師名字',
teasex bit default 1 comment '性別',
teabirth date comment '生日',
teatitle varchar(10) default '助教' comment '老師職稱',
colid int not null comment '所在學(xué)院'
-- colid int not null,
-- foreign key(colid) references tb_college(colid)
);
# 主鍵約束alter(表忘記寫主鍵的情況下使用)
alter table tb_teacher add constraint pk_teacher_teaid
primary key(teaid);
# 添加外檢約束alter
alter table tb_teacher add constraint fk_teacher_colid
foreign key(colid) references tb_college (colid);