mysql筆記一之庫操作與 表操作
資料庫使用者名稱: root
密碼: mysql
char(5) 長度固定為5的字串 ab --> "ab "
varchar(5) abc --> "abc" 不能夠超出長度
1.-- 資料庫的操作
-- 連結資料庫
mysql -uroot -pmysql
-- 退出資料庫
exit 或者 quit 或者 ctr + d
-- 檢視建立資料庫
show databases;
-- 檢視當前使用的資料庫
select database();
-- 使用資料庫 切換
use python_info;
-- sql語句最後需要有分號;結尾
-- 顯示資料庫版本
select version();
-- 顯示時間
select now();
-- 建立資料庫 create
create database demo;
-- 指定字符集
create database demo charset=utf8;
-- 檢視資料庫的建立語句
show create database demo;
-- 刪除資料庫
drop database demo;
2.-- 資料表的操作
-- 檢視當前資料庫中所有表
show tables;
-- 建立表
-- auto_increment表示自動增長
-- 建立一個學生的資料表(id、name、age、high、gender、cls_id)
-- create table 資料表名字 (欄位 型別 約束[, 欄位 型別 約束]);
-- 欄位名 + 型別 + 約束(不分先後順序)
-- unsigned不能夠為負數
-- facebook 56個性別
-- enum 表示列舉
-- 班級id 在班級表中設定的是 int unsigned型別 學生表中需要引用班級的編號 需要保證型別是一致的
create table students(
id int unsigned primary key auto_increment not null,
name varchar(10) not null,
age tinyint unsigned default 0,
high decimal(5,2) default 0,
gender enum("男", "女", "中性", "保密"),
cls_id int unsigned default 0
);
-- 建立students表
-- 查看錶的建立語句
show create table classes;
engine(引擎) = innodb (支援外來鍵並且還是事務) 轉賬 a - 500-> b 500元 需要使用事務 提供了一種可以撤銷的機制
MyISAM 全文檢索引擎 查詢速度 更新非常慢 不支援 事務 和外來鍵
-- 查看錶結構
desc classes;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(15) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
-- 修改表結構 alter
-- 修改表-新增欄位
-- alter table 表名 add 列名 型別/約束;
-- 生日資訊
alter table students add birthday datetime;
-- 修改表-修改欄位:不重新命名版
-- alter table 表名 modify 列名 型別及約束;
alter table students modify birthday date;
-- 修改表-修改欄位:重新命名版
-- alter table 表名 change 原列名 新列名 型別及約束;
alter table students change birthday birth date;
-- 刪除對應的欄位
alter table students drop birth;
-- 刪除表
drop table students;