MySQL基礎操作

來源:互聯網
上載者:User

標籤:for   分割線   記錄   char   一個   use   ODB   def   lin   

一:基礎操作

1.資料庫建立

create database jmdb;      建立一個名為 jmdb 的資料庫show databases;         顯示當前資料庫列表
show database jm;        查看資料庫的建立方式drop database jmdb;      刪除名為jmdb的資料庫
use jm;             選擇資料庫

 

2.遠端連線

1.mysql> use mysql;
2.mysql> select user,host from user;

+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+

 3.mysql> update user set host=‘%‘ where user=‘root‘;  將host列修改成 ‘%‘

 4.mysql> select user,host from user;   
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
+---------------+-----------+

---------------------------------------分割線---------------------

遠端連線需要關閉防火牆

[[email protected] ~]# systemctl stop firewalld      # 臨時關閉

[[email protected] ~]# systemctl disable firewalld  # 禁止開機啟動

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

 

3.表建立

use jmdb;             選擇一個名為jmdb的資料庫

# 建立表

create table student(
id int primary key auto_increment,      # id,修飾成主鍵,自增
name varchar(20) unique,                # 表示該列資料不能有重複
age int                                 #int類型
)engine=InnoDB default charset=utf8;    # 指定資料庫引擎Innodb,編碼為utf8

約束: primary key (非空並且唯一):能夠唯一區分目前記錄的欄位稱為主鍵

    unique    : 唯一,就是資料不能有重複的,一旦有重複就會報錯

    not null    : 該列資料不可為空,否則報錯

    auto_increment: 主鍵欄位必須是數字類型

    foreign key  :外鍵約束

-----------------------------------------------分割線-----------------------------

查看錶資訊:

  desc student  查看錶結構

  show columns from student  查看錶結構

  show tables    查看當前資料的所有表

  show create table student  查看錶的建立語句

 

-----------------------------分割線-----------------------------------------------

修改表結構:

  1.增加欄位(列):

    alter table student add [列名] 類型 [約束性條件] [first|after 列名]

    alter table student add abd int after name;  添加abd列,指定它在 name 列的後面

    alter table student add aba int first ;    添加ada列,指定該列在 最前面

    alter table student add abs int,           添加多列

                add acc varchar(20),

                add caa varchar(10);

 

-------------------------------分割線-------------------------------------

  2.修改一列的類型

    alter table student modify 列名 類型 [約束條件] [first|after 欄位名]

    alter table student modify aba varchar(20) unique; 將 aba 列修飾成 唯一 欄位

  3.修改列名

    alter table 表  change 列名 新列名 類型 [約束條件] [first|after 欄位名]

    alter table student change aba aBa varchar(10);  將aba 修改成 aBa

  4.刪除列

    alter table 表 drop [column] 列名

    alter table student drop aBa;

    alter table student drop column abd,drop column abs; 刪除多列,必須要有column

  5.修改表名

    rename table 表名 to 新表名;

  6.修改字元集

    alter table student default CHARACTER SET utf8 COLLATE utf8_general_ci; 

  7.刪除表:

    drop table 表名;

-----------------------------------分割線----------------------------------------

  8.添加主鍵

    alter table student add primary key(欄位名...)

  9.刪除主鍵

    alter table student modify id int;  先刪除 auto_increment

    alter tabel student drop primary key; 才能刪除主鍵

-----------------------------------分割線---------------------------------------

 索引:

  普通索引:alter table student add (index|key) [索引名](欄位.....)

    1.alter table student add index (name)  #為name建立索引,索引名預設為欄位名

    2.alter table student add index username(name)  #為name建立索引,索引名為username

    注意:key和index的使用方法一樣

 

  唯一索引:alter table studnet add unique (index|key) [索引名](欄位名....)

    1.alter table studnet add unique index username(name)  #為欄位name建立唯一索引,索引名為username

  

  聯合索引:

    1.alter table student add index user_age(name, age)

    2.alter table student add unique index user_age(name, age)

 

  刪除索引:

    alter table student drop (index|key) 欄位名

    如:alter table student drop index username  #刪掉索引username

 4.主鍵

#單欄欄位主鍵
create table student( id int primary key auto_increment, name varchar(20) unique, age int )engine=InnoDB default charset=utf8;
# 多欄位聯合主鍵create table student( id int auto_increment, name varchar(20), age int, primary key (id,name) )engine=InnoDB default charset=utf8;
#一張表只能有一個主鍵
#主鍵類型不一定得是整型

 

MySQL基礎操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.