1 mysql常用術(shù)語
? ? 1) 數(shù)據(jù)庫? database (***)
? ? 2) 表? table? (***)
? ? 3) 行(row)和列(column)
? ? 4) 記錄 (record) 一個數(shù)據(jù)行? 就是一條記錄
? ? 5)字段(field) 每個列 就是一個字段
? ? 備注: 結(jié)構(gòu)化查詢語言SQL(Structured query language)众羡,
? ? ? ? ? 操作關(guān)系數(shù)據(jù)庫的通用語言薇溃,用于執(zhí)行數(shù)據(jù)的檢索和其他操作铆惑。
? ? 數(shù)據(jù)庫 =》 表 =》 記錄(行)
2 mysql安裝
? ? 1) 下載 phpStudy 集成工具
? ? 2) 下一步 安裝 點擊啟動 (綠色代表成功)
? ? 啟動會同時啟動兩個東西:
? ? ? ? apache:? 這是集成的一個服務(wù)器? 端口是 80
? ? ? ? mysql:? 這是數(shù)據(jù)庫? 端口是 3306
3 連接數(shù)據(jù)庫
? ? 語法: mysql -h主機名? -u用戶名 -p密碼
? ? mysql –hlocalhost –uroot –proot
? ? 測試是否連接成功:
? ? ? ? show databases;
? ? 說明: 需要配置環(huán)境變量, 把以下路徑 放入環(huán)境變量
? ? ? ? C:\phpStudy\PHPTutorial\MySQL\bin
4 數(shù)據(jù)庫的操作 (**)
? ? 1) 查看數(shù)據(jù)庫列表
? ? ? ? show databases;
? ? 2) 創(chuàng)建數(shù)據(jù)庫
? ? ? ? create database 數(shù)據(jù)庫的名字;
? ? ? ? 例子: create database web;
? ? 3) 切換(使用數(shù)據(jù)庫)
? ? ? ? use 數(shù)據(jù)庫名稱;
? ? ? ? 例子: use web;
? ? 4) 刪除數(shù)據(jù)庫
? ? ? ? drop database 數(shù)據(jù)庫的名字;
? ? ? ? 例子: drop database web;
? ? 5) 修改數(shù)據(jù)庫的選項(一般修改編碼)
? ? ? ? alter database 數(shù)據(jù)庫的名字? 修改內(nèi)容
? ? ? ? 例子: alter database web charset=gbk;
? ? 補充: 查看創(chuàng)建數(shù)據(jù)庫的一些信息
? ? 6) show create database 數(shù)據(jù)庫的名字
? ? ? ? 例子: show create database web;
5. 表的基本操作(*****)
? ? 1) 創(chuàng)建表
? ? ? ? 語法:
? ? ? ? ? ? create table 表名 (
? ? ? ? ? ? ? ? 字段名稱 數(shù)據(jù)類型和長度 屬性修飾,
? ? ? ? ? ? 字段名2 字段類型2 屬性修飾2
? ? ? ? ? ? )[表選項]
? ? ? ? 例子:
? ? ? ? ? ? create table student (
? ? ? ? ? ? ? ? id int primary key auto_increment,
? ? ? ? ? ? ? ? name varchar(50),
? ? ? ? ? ? ? ? age int,
? ? ? ? ? ? ? ? sex varchar(10)
? ? ? ? ? ? );
? ? ? ? ? ? create table teacher (
? ? ? ? ? ? ? ? name varchar(50),
? ? ? ? ? ? ? ? age int,
? ? ? ? ? ? ? ? sex varchar(10)
? ? ? ? ? ? );
? ? 2) 查看當前數(shù)據(jù)庫有哪些表
? ? ? ? show tables;
? ? 3) 查看表的結(jié)構(gòu)
? ? ? ? desc 表的名字;
? ? ? ? 例子: desc student;
? ? 4) 刪除表
? ? ? ? drop table [if exists] 表名
? ? ? ? 例子: drop table if exists teacher;
? ? 補充:
? ? ? ? 5)修改表:
? ? ? ? ? ? alter table 表名 選項=新值
? ? ? ? ? ? 例子: alter table student charset=utf8;
? ? ? ? 6)查看建表的一些信息:
? ? ? ? ? ? show create table 表的名字
? ? ? ? ? ? 例子: show create table student;
6 記錄的基本操作 (******)
? ? 1) 增加記錄(插入數(shù)據(jù)) ------ 增
? ? ? ? 語法: insert into 表名 (字段列表) values (值列表)
? ? ? ? 例子: insert into teacher(name, age, sex) values('李尋歡', 35, '男');
? ? ? ? ? ? ? insert into teacher(name, age, sex) values('xunhuanLi', 35, 'man');
? ? ? ? ? ? ? insert into student(name, age, sex) values('小張', 20, '男');
? ? ? ? ? ? ? insert into student(name, age, sex) values('小李', 21, '女');
? ? ? ? ? ? ? insert into student(name, age, sex) values('小王', 22, '男');
? ? ? ? 補充:?
? ? ? ? ? ? ? insert into 表明 values (值列表)? // 參數(shù)一定要按照順序
? ? ? ? ? ? ? 例子:? insert into teacher values('老王', 35, '男');? ?
? ? ? ? ? ? ? ? ? ? insert into student values(default,'小強', 18, '男');?
? ? ? ? ? ? ? 注意: 如果使用這種 省略字段列表的方式 需要使用 default 或 null 占位
? ? ? ? dos控制臺中文編碼問題 (*****)
? ? ? ? ? ? 在插入數(shù)據(jù)之前 set names gbk;
? ? 2) 查詢數(shù)據(jù) (*******) ------- 查
? ? ? ? a) 基本查詢:
? ? ? ? ? ? 語法: select 字段列表 from 表的名字渔呵;
? ? ? ? ? ? 例子: select * from student;? // 查詢表里面的所有字段
? ? ? ? ? ? ? ? ? select name from student;? // 查詢表里面的name字段?
? ? ? ? ? ? ? ? ? select age from student;? // 查詢表里面的age字段?
? ? ? ? ? ? ? ? ? ? 補充: 查詢出來 取別名
? ? ? ? ? ? ? ? ? ? select 字段名 as 別名 from 表名;
? ? ? ? ? ? ? ? ? ? 例子: select name as n from student;
? ? ? ? b) 帶條件查詢
? ? ? ? ? ? 語法:? select 字段列表 from 表的名字 where 條件
? ? ? ? ? ? 例子: select * from student where sex = '女';
? ? ? ? ? ? ? ? ? select * from student where sex <> '女';
? ? ? ? ? ? ? ? ? select * from student where age > 18;
? ? ? ? ? ? ? ? ? select * from student where age < 21;
? ? ? ? ? ? ? ? ? select * from student where age >= 18;
? ? ? ? ? ? ? ? ? select * from student where age <= 21;
? ? ? ? ? ? ? ? ? select * from student where age BETWEEN 20 AND 22;? // 在什么之間
? ? ? ? ? ? ? ? ? select * from student where age > 18 and sex='女'; // 并且
? ? ? ? ? ? ? ? ? select * from student where age > 18 or id=6;? // 或者
? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '小%';? // 正則匹配以。塌西。開頭的
? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '老%';
? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%強';? // 正則匹配以柔吼。。結(jié)尾的
? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%李';
? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%李%';? // 包含這個字符的
? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%王%';
? ? 3) 刪除數(shù)據(jù) (****)------? 刪
? ? ? ? 語法: delete from 表名 [where 條件]
? ? ? ? 例子: delete from student where sex = '女';? // 刪除所有性別為女的數(shù)據(jù)
? ? ? ? 例子: delete from student where id = '8';? // 刪除id = 8數(shù)據(jù)
? ? ? ? 備注: 如果不寫條件 代表刪除所有數(shù)據(jù)
? ? ? ? ? ? 例如: delete from student;? // 刪除 student 表 所有數(shù)據(jù)
? ? 4) 修改數(shù)據(jù) (****) -----改
? ? ? ? 語法: update 表名 set 字段=值, 字段=值 [where 條件表達式];
? ? ? ? 例子: update student set age = 38, sex = '女';
? ? ? ? ? ? ? update student set age = 8, sex = '男' where id = 1;
7 數(shù)據(jù)類型
? ? 商品表 (關(guān)鍵字 可以使用反引號)
? ? create table `goods` (
? ? ? ? id int primary key auto_increment,
? ? ? ? `sort` tinyint unsigned,
? ? ? ? name varchar(100),
? ? ? ? price float(10, 2),
? ? ? ? `desc` text,
? ? ? ? ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
? ? );
? ? insert into goods(name, price, `desc`) values('ipone18', 9999.999, '這是一個手機1');
? ? insert into goods(name, price, `desc`) values('ipone19', 8888.999, '這是一個手機2');
? ? insert into goods(name, price, `desc`) values('ipone20', 7777.999, '這是一個手機3');
? ? 補充: 查詢結(jié)果排序
? ? select * from goods order by ctime asc;? // 升序 默認
? ? select * from goods order by ctime desc; // 降序排列
? ? select * from goods where 1 order by ctime desc;? // 條件可以拼接 order by 只能在最后