MySql基本操作

2021-08-21 17:22:10 字數 1921 閱讀 7682

@概述

@資料庫連線

-- 本地連線

mysql -u 賬號 -p 回車輸入密碼

-- 遠端連線

mysql -u 賬號 -p 密碼 -h 伺服器位址

@建立資料庫和表

-- 建立銀魂資料庫並且設定欄位為utf-8

create database gintama charset=utf8;

-- 使用資料庫

use gintama;

-- 建立組織表

create table organizations(

id integer primary key auto_increment, -- 設定id為主鍵且自增長

name varchar(10) not null, -- 設定組織姓名不能為空

remarks varchar(50),

memberid integer default 0-- 設定memberid預設為0 首領id

);-- 建立成員表

create table members(

id integer primary key auto_increment, -- 設定id為主鍵且自增長

name varchar(10) not null, -- 設定姓名不能為空

*** bool not null , -- 設定性別不能為空

occupationid integer default 0-- 設定occupationid預設為0

);-- 建立職業表

create table occupations(

id integer primary key auto_increment, -- 設定id為主鍵且自增長

name varchar(10) not null -- 設定名字不能為空

);-- 建立職業和成員關係表

create table member_occpation(

memid int not null,

occid int not null,

primary key(memid,occid)

);

@其他庫操作

-- 顯示資料庫

show databases;

-- 選擇資料庫

use dbname;

--刪除資料庫

drop database dbname;

@其他表操作

-- 顯示資料庫下面的表

show tables;

-- 檢視表結構

desc 表名;

-- 檢視表的建立過程

show create table 表名;

-- 修改表之增加列 內為可選引數

alter table 表名 add 列名稱 列型別 [列引數] [not null default ];

-- 修改表之刪除列

alter table 表名 drop 列名稱;

-- 修改表之修改列

alter table 表名 change 舊列名 新列名 列型別 [列引數] [not null default ] ;

-- 修改表之增加主鍵

alter table 表名 add primary key(主鍵所在列名);

-- 清空表資料

truncate 表名;

-- 修改表之增加索引

alter table 表名 add [unique|fulltext] index 索引名(列名);

-- 修改表之刪除索引

alter table 表名 drop index 索引名;

mysql基本操作 MySQL基本操作

mysql中新增使用者,新建資料庫,使用者授權,刪除使用者,修改密碼 注意每行後邊都跟個 表示乙個命令語句結束 1.新建使用者 1.1 登入mysql mysql u root p 密碼 1.2 建立使用者 mysql insert into mysql.user host,user,passwor...

mysql 基本操作 mysql基本操作

mysql 建立表,並設定主鍵自增 create table log logid int 4 primary key not null auto increment,logtitle varchar 32 not null logcontent varchar 160 not null logtim...

mysql基本操作

1,檢視資料庫狀態 及啟動停止 etc init.d mysqld status etc init.d mysqld start etc init.d mysqld stop 2,給使用者配置初始密碼123456 mysqladmin u root password 123456 3,修改root使...