MySQL簡易操作整理

2021-08-24 23:10:35 字數 2613 閱讀 1250

1.安裝與啟動2.登入

mysql -h 主機名 -u 使用者名稱 -p

-h: 指定客戶端所要登入的mysql主機名,當前機器可省略

-u:指定使用者名稱

-p:告訴伺服器已密碼登入,若密碼為空可省略

修改密碼:mysqladmin -u root -p password 新密碼

3.建立

a.建立資料庫:create database 資料庫名[其他選項]

例:才create database samp_db character set gbk;

建立samp_db資料庫並將編碼設定為gbk便於使用中文,語句以」;」 結束。

show database;可檢視已建立資料庫。

b.選擇需要運算元據庫

登入資料庫時指定: mysql -d 所選擇的資料庫名 -h 主機名 -u 使用者名稱 -p

例:mysql -d samp_db -u root -p //指定登入時選擇samp_db 資料庫

登入後指定:use 資料庫名

例:use samp_db

c.建立資料庫表:create table 表名稱(列宣告);

例:create table students

( id int unsigned not null auto_increment primary key,

name char(8) not null,

*** char(4) not null,

age tinyint unsigned not null,

tel char(13) null default 「-」

); not null:不能為空,輸入資料為null會報錯,如果不指定該屬性, 預設可為空;

auto_increment:自增屬性,一般用於主鍵,自動產生乙個比現存值更大的唯一識別符號值;

primary key:定義列為主鍵。

使用sql檔案:mysql -d samp_db -u root -p < createtable.sql

show tables;可檢視已建立表名稱。

4.常用操作

a.插入:insert [into] 表名 [(列名1, 列名2, 列名3, …)] values (值1, 值2, 值3, …);

例: insert into students values(null, 「王剛」, 「男」, 20, 「13811371377」);// 插入完整資料

insert into students (name, ***, age) values(「孫麗華」, 「女」, 21);//插入部分資料

b.查詢:select 列名稱 from 表名稱 [查詢條件];

例:select name,age from students; //在students表中查詢name與age兩列

select * from students;//查詢students表的全部資訊

c.更新表中資料:update 表名稱 set 列名稱=新值 where 更新條件;

例:update students set tel=default where id=5; //將students tel列id為5的手機號改為預設的」-」

update students set age=age+1; //將所有年齡增加1

d.刪除:delete from 表名稱 where 刪除條件;

例:delete from students where id=2; //刪除id為2的行

delete from students where age<20; //刪除年齡小於21歲的資料

delete from students; //刪除表中所有資料

e.新增列:alter table 表名 add 列名 列資料型別 [after 插入位置];

例:address: alter table students add address char(60); //在表的最後新增address 指定為char

alter table students add birthday date after age; //在表age的列後插入列birthday指定為date 型別

f.修改列:alter table 表名 change 列名稱 列新名稱 新資料型別;

例:alter table student change tel telphone char(13) default 「-「;

alter table students change name name char(16) not null;

g.刪除列:alter table 表名 drop 列名稱;

例:alter table students drop birthday;

h.重新命名表:alter table 表名 rename 新錶名;

例:alter table students rename workmates;

i.刪除整張表:drop table 表名;

例:drop table workmates;

j.刪除整個資料庫

例:drop database samp_db;

mysql常用操作整理

對庫的操作 建立 create database db test1 刪除 drop database db test1 查詢 show databases 切換庫 use db name 對錶的操作 建立 create table tb name col name col type not null...

php實現Mysql簡易操作類

自己封裝的mysql簡易操作類,已塞在ben框架中,基於pdo來寫的,風格上有些無厘頭。mysql.class.php public function drop table else public function insert table,name,value null else for i 0 ...

MySql操作要點整理

tinyint 1 位元組 smallint 2 個位元組 mediumint 3 個位元組 int 4 個位元組 integer 4 個位元組 bigint 8 個位元組 float x 4 如果 x 24 或 8 如果 25 x 53 float 4 個位元組 double 8 個位元組 dou...