2018-10-13 day4數(shù)據(jù)庫

1.簡介

????數(shù)據(jù)庫選擇:

????????Mysql數(shù)據(jù)庫:中小型項目用得比較多,免費

????????Orcale數(shù)據(jù)庫:大型項目中,收費的

? ??????XAMPP(Apache+MySQL+PHP+PERL)

2.NavicatforMySQL的使用

連接名:cxy // 顯示的名稱

主機名或ip地址:填寫的是服務(wù)器端的ip地址

端口:3306

用戶名:后臺服務(wù)器管理人員給到的

密碼:

高級:20936 (Simplified Chinese GB2312) ,不選擇就不能支持中文

3.數(shù)據(jù)庫命令行之基本操作

? ? 1. 顯示數(shù)據(jù)庫---show databases;

? ? 2. 選擇數(shù)據(jù)庫--use 數(shù)據(jù)庫名稱;

? ? ? ? ? ?!注意: 在創(chuàng)建表單、添加數(shù)據(jù)、刪除數(shù)據(jù)、修改數(shù)據(jù)...操作的前提是要進入到對應(yīng)的數(shù)據(jù)中,否則是操作不了的。

? ? 3. 創(chuàng)建數(shù)據(jù)庫---create database 數(shù)據(jù)庫名字;

? ? ? !? 注意: 命令行操作,要注意中英文狀態(tài),都是要使用英文的標(biāo)點符號!??!快捷鍵: F5刷新數(shù)據(jù)庫

? ? 4. 刪除數(shù)據(jù)庫--drop database 數(shù)據(jù)庫名字;

4.數(shù)據(jù)庫命令行之表單結(jié)構(gòu)操作

? ? ? ? 1. 查看數(shù)據(jù)庫中的表---show tables;

? ? ? ? 2. 創(chuàng)建表---creatw table 表名(字段1 屬性,字段2 屬性,....);

????????????????!創(chuàng)建表之前,一定要先進入對應(yīng)的數(shù)據(jù)庫中?。?!

? ??????????????????create table students(idint(4),namechar(20),ageint(4));?

? ? ? ? ? ? ? ? ? ? create table students(idint(4) primary key,namechar(20),ageint(4));

? ? ? ? 3. 刪除表---drop table 表名;

? ? ? ? 4. 修改表名---alter table 老表名 rename 新表名;

? ? ? ? 5. 表結(jié)構(gòu)---desc 表名;

? ??????????????????Field:域/字段/列 Type:類型Null:是否為空 Key:主鍵Default:默認(rèn)值 Extra:額外屬性

? ? ? ? 6. 增加一個字段---alter table 表名 add 新字段的名字 字段屬性

????????????????????如果要添加的某個字段不能為空,就是not null;

? ? ? ? ? ? ? ? ? ? ?例如: alter table students add sex char(10) not null;

? ? ? ? 7.? 刪除字段---alter table 表名 drop 字段名;

? ? ? ? 8. 修改字段---添加屬性---alter table 表名 change 老表名 新字段名 字段屬性

? ? ? ? ? ? ? ? ? alter table students changeididint(4) auto_increment;//給id添加自動增長的屬性

? ? ? ? 9. 修改字段---修改屬性---alter table students change id id int(4);

? ? ? ? 10. 修改字段---刪除主鍵---alter table students drop primary key;

? ? ? ? 11. 修改字段---添加主鍵---alter tables studens add primary key(id);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ---alter table students changeididint(4) primary key;

? ? !一張表中只能有一個字段是自動增長的,并且被設(shè)定為自動增長的這個字段一定要設(shè)置為主鍵;如果一個主鍵字段有自增長屬性,如果想要直接刪除主鍵,這是操作不了的;必須是先將自增長屬性去除,然后才能刪除主鍵;

5. 數(shù)據(jù)庫命令行之表單數(shù)據(jù)操作

? ? ? ? 1.查詢表中的所有數(shù)據(jù)---select * from 表單名;
? ? ? ? 2.增加數(shù)據(jù)---insert into 表名(字段1,字段2,...) value(值1,值2,...);

? ? ? ? 3.刪除數(shù)據(jù)---delete from 表名 where 條件;

? ? ? ? 4.修改數(shù)據(jù)---select 表名 set 字段=值 where 條件;

? ? ? ? 5.查詢數(shù)據(jù)---select 字段 from 表名 where 條件;

? ? ? ? 6.as---給查詢的結(jié)構(gòu)字段取別名,其目的是讓查詢結(jié)果展示更符合人們觀看習(xí)慣.

? ? ? ? 7.其他

? ? ? ? ? ? ? ? count()---統(tǒng)計數(shù)量

? ? ? ? ? ? ? ? avg() ---求平均分

????????????????sum()---求和

????????????????max()---最大值

????????????????min()---最小值

? ? ? ? ? ? ? ? distinct()---去重復(fù)

????????????????order by---排序

????????????????group by---分組

6.多表查詢

? ? ? ? 1.連接查詢

? ? ? ? ? ? ? ? 1.1 內(nèi)連接inner join? 或者? join---共有的部分

? ? ? ? ? ? ? ? 1.2 左連接left join----得到a的所有數(shù)據(jù),和滿足某一條件的b的數(shù)據(jù)。或者得到a同時去除與b相同的部分.

? ? ? ? ? ? ? ? 1.3 右連接right join

7.數(shù)據(jù)庫索引

? ? ? ? 1.alter table 表名 add index 索引名稱(列表);

? ? ? ? 2.create index 索引名稱 on 表名(列名);

? ? ? ? 3.刪除索引---drop index 索引名 on 表名;

? ? ? ? 4.顯示索引信息---show index from 表單名;

8.數(shù)據(jù)庫視圖

? ? ? ? ? ? 1.創(chuàng)建數(shù)據(jù)庫視圖---create view 視圖名稱 as select 語句;

? ? ? ? ? ? 2.刪除視圖---drop view 視圖名;

? ? ? ? ? ? 3.視圖的使用---select * from 表名;

9.數(shù)據(jù)存儲過程

? ? ? ? 1.創(chuàng)建存儲過程

? ? ? ? ? ? ? ? DELIMTER //? ? 定義結(jié)束符

? ? ? ? ? ? ? ? CREATE PROCEDURE p2()

其他!

mysql啟動和關(guān)閉

net start mysql

net stop mysql

mysql的登錄與退出

-u 用戶

-p 密碼

-v 版本信息

-h 主機地址

退出

exit

quit

-q

修改用戶密碼命令

mysqladmin -u用戶名 -p舊密碼 -password

顯示數(shù)據(jù)庫的命令

show databases

infromation_schema 數(shù)據(jù)信息

mysql 系統(tǒng)

performance

sys 系統(tǒng)運行信息

使用數(shù)據(jù)庫

use 數(shù)據(jù)庫名稱

select命令

此命令用于顯示當(dāng)前連接的信息

select database();

select version();

select now();

select user();

操作數(shù)據(jù)庫---數(shù)據(jù)庫創(chuàng)建、修改、刪除

創(chuàng)建

create database test1;

create database if not exists test2 character set utf8;

修改數(shù)據(jù)庫

alter database test2 charecter set Latin1;

????????查看數(shù)據(jù)庫編碼格式

? ? ? ? ? ? use test2

? ? ? ? ? ? show variables like 'charecter%';

刪除數(shù)據(jù)庫

drop database test2;

=====================================

數(shù)據(jù)類型

tinyint

smallint

mediumint

int

bigint

浮點數(shù)和定點數(shù)類型

float? ?4個字節(jié)存儲

double? ?8個字節(jié)存儲

declima

日期時間型

year? ? ?1901~2155? ? ?YYYY

time? ? ? ? ? ? ? ? ? ? ? ? ? HH:MM:SS

data? ? ? ? ? ? ? ? ? ? ? ? ?YYYY-MM-DD

timestamp? ? ? ? ? ? ? ?YYYY-MM-DD HH:MM:SS

datatime? ? ? ? ? ? ? ? ?YYYY-MM-DD HH:MM:SS

字符型

char? ? ? ? ?0<=M<=255

varchar? ?

text

tinytext

enum('value1',value2')? ? 取決于枚舉值的個數(shù)

set? ? ?取決于set數(shù)目? ?最多64個

創(chuàng)建表---create table 表名(字段 屬性,字段 屬性,);

create table reader(card_id char(18),name varchar(10),sex enum('男','女'),age tinyint,tel char(11),balance decimal(7,3));

查看數(shù)據(jù)庫表---show table from book;

查看表的結(jié)構(gòu)---show columns from 表名;

? ? ? ? ? ? ? ? ? ? ?---desc reader;

!顯示創(chuàng)建表的語句---show create table 表名;

修改

添加列---alter table 表名 add 新列表 數(shù)據(jù)類型 約束條件 first after 已存在的列;

alter table reader add email2 varchar(30) after tal;

修改列名

alter table reader change email2 email——bak varchar(30);

修改列的數(shù)據(jù)類型

alter table reader modify email varchar(25);

修改列的排列位置

alter table 表名 modify balance decimal(7,3) after email;

刪除列

alter table drop 列名;

alter table reader drop? email_bak

修改表名

alter table reader rename to readerinfo;

查看表名---show tables;

刪除數(shù)據(jù)庫表---可一次刪除一個或多個數(shù)據(jù)庫表名稱

? ? ? ? drop table [if exists] 表1,表2,表3;

? ? ? ? ? drop table if exists t3,t4;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容