sql
概念:結(jié)構(gòu)化查詢(xún)語(yǔ)言(sql = structured query language),
也是一種編程語(yǔ)言(數(shù)據(jù)庫(kù)查詢(xún)和程序設(shè)計(jì)語(yǔ)言),可以用于數(shù)據(jù)的存取及查詢(xún),更新,管理關(guān)系型數(shù)據(jù)庫(kù)系統(tǒng)
ps: 不同數(shù)據(jù)庫(kù)系統(tǒng)之間的sql不能完全相互通用;
分類(lèi)
針對(duì)操作的對(duì)象不同,可以分成不同語(yǔ)言
1: 數(shù)據(jù)操作(數(shù)據(jù)管理)語(yǔ)言? dml(data management language)
?? ?1): 查詢(xún)數(shù)據(jù)?????????? dql
?? ?2): 增, 刪, 改?? ??? ??? dml
2: 數(shù)據(jù)定義語(yǔ)言??????????? ddl(data definition language)???? –比如表的定義
3: 數(shù)據(jù)控制語(yǔ)言?? ??? ??? ??? dcl(data control language)
****************************************************************************************************************
數(shù)據(jù)庫(kù), 表, 數(shù)據(jù)三者之間的關(guān)系
表是數(shù)據(jù)的載體, 數(shù)據(jù)庫(kù)又是表的容器
****************************************************************************************************************
數(shù)據(jù)庫(kù)操作
mysql>?show?databases;????--查看所有的數(shù)據(jù)庫(kù) +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?mysql??????????????| |?performance_schema?| |?test???????????????| +--------------------+
創(chuàng)建數(shù)據(jù)庫(kù)
語(yǔ)法: create database [if not exists] db_name [數(shù)據(jù)選項(xiàng)]
例:
create?database?student_system;
例:
create?database?if?not?exists?student_system;
–會(huì)先判斷student_system是否已經(jīng)存在,如果存在就不會(huì)創(chuàng)建,可以避免異常
例:
C:WINDOWSsystem32>mysqladmin?-uroot?-p?create?bbbb
–可以通過(guò)mysqladmin來(lái)進(jìn)行創(chuàng)建
*********************************************************************
數(shù)據(jù)庫(kù)的命名規(guī)則
1: 見(jiàn)名知意, 建議是使用下劃線(xiàn)的方式
2: 可以使用任意的字符,比如數(shù)字,符號(hào),中文等
?? ?create database 胖胖;
3: 如果命名很特殊,比如是以純數(shù)字或者關(guān)鍵詞來(lái)命名時(shí),就要使用限定符來(lái)包裹(限定符指反引號(hào)“);
?? ?create database `123456`;
4: 是否區(qū)分大小寫(xiě)(這個(gè)當(dāng)前的操作系統(tǒng)有關(guān));
5: 數(shù)據(jù)庫(kù)的名字都可以使用反引號(hào)來(lái)創(chuàng)建
***********************************************************************
ps: 數(shù)據(jù)庫(kù)創(chuàng)建時(shí)會(huì)形成一個(gè)目錄,目錄名是數(shù)據(jù)庫(kù)名,如果數(shù)據(jù)庫(kù)名是特殊字符,那么文件名會(huì)經(jīng)編碼形式來(lái)表式
?? ?目錄下面會(huì)有一個(gè)db.opt文件保存著數(shù)據(jù)庫(kù)的選擇信息;
***********************************************************************
數(shù)據(jù)庫(kù)的相關(guān)操作
1:?show?databases;?????--查看所有的數(shù)據(jù)庫(kù) 2:?drop?[if?exists]?database?bbbb;?--刪除指定的數(shù)據(jù)庫(kù) 3:?show?create?database?student_system;??--查看數(shù)據(jù)庫(kù)(student_system)創(chuàng)建信息 ????+----------------+------------------------------------------------------------------------+ ????|?Database???????|?Create?Database???????????????????????????| ????+----------------+------------------------------------------------------------------------+ ????|?student_system?|?CREATE?DATABASE?`student_system`?/*!40100?DEFAULT?CHARACTER?SET?gbk?*/?| ????+----------------+------------------------------------------------------------------------+ 4:?alter?database?db_name?[指定的操作]???--修改數(shù)據(jù)庫(kù)信息 ????例:?alter?database?student_system?character?set?'utf8';
********************************************************************************************************************
表的相關(guān)操作
表的創(chuàng)建?? ? ?
?? ?創(chuàng)建語(yǔ)法: create table [if not exists] tbl_name(列結(jié)構(gòu))[選項(xiàng)]
?? ?表是數(shù)據(jù)的載體, 數(shù)據(jù)庫(kù)又是表的容器,所以在創(chuàng)建表之前,需要先定它所屬的那個(gè)數(shù)據(jù)庫(kù),
?? ?表一定屬性某一個(gè)數(shù)據(jù)庫(kù)的
1: 可以在創(chuàng)建表的時(shí)候, 在表名之前指明所屬的數(shù)據(jù)庫(kù)
????create?table?`student_system`.student( ????????name?varchar(20), ????????sex?varchar(3), ????????age?int ????);
2: 可以先使用use db_name指明當(dāng)前默認(rèn)的數(shù)據(jù)庫(kù),然后再創(chuàng)建表
??
?use?student_system ????create?table?teacher( ????????name?varchar(20), ????????sex?varchar(3), ????????age?int ????);
3: show tables;??? –查看所有的表,也要先指明當(dāng)前默認(rèn)的數(shù)據(jù)庫(kù)
4: show create table teacher;?? –查看創(chuàng)建表(teacher) 創(chuàng)建信息
?? show create table teacherG
5: describe teacher;?? –查看表(teacher)的結(jié)構(gòu)
?? ?
+-------+-------------+------+-----+---------+-------+ ????|?Field?|?Type????????|?Null?|?Key?|?Default?|?Extra?| ????+-------+-------------+------+-----+---------+-------+ ????|?name??|?varchar(20)?|?YES??|?????|?NULL????|???????| ????|?sex???|?varchar(3)??|?YES??|?????|?NULL????|???????| ????|?age???|?int(11)?????|?YES??|?????|?NULL????|???????| ????+-------+-------------+------+-----+---------+-------+ ????desc?teacher;??--可以簡(jiǎn)寫(xiě)describe?teacher;
6: drop table [if exists] tbl_name;?? –刪除表(包裹表結(jié)構(gòu))
?? ?例: drop table student;
?? ?例: drop table if exists student;
***************************************************************************************************************
修改表
修改表名
語(yǔ)法:rename?table?old_table_name?to?new_table_name? 例:?rename?table?student?to?student_1; 例:?rename?table?student_2?to?student_1,?teacher?to?teacher_1;???--可以同時(shí)修改多個(gè)表名 例:?rename?table?student_1?to?`test`.student_2;?--可以跨數(shù)據(jù)庫(kù)重命名,?可以通過(guò)這個(gè)表重命名的方式來(lái)對(duì)數(shù)據(jù)庫(kù)重命名
修改列的定義
新加列(add)
alter?table?student_1?add?id?int;
刪除列(drop)
alter?table?student_1?drop?id;
修改列定義(modify)
alter?table?student_1?modify?name?varchar(10);
重命名列(change)
alter?table?student_1?change?age?student_age?int(3);
*******************************************************************************************************
表數(shù)據(jù)操作(增刪改查)
插入數(shù)據(jù)(創(chuàng)建數(shù)據(jù)create)
語(yǔ)法: insert into 表名(字段列表) values(值列表)
例: insert into teacher_1(name,age) values(‘胖胖’, 18);
例: insert into teacher_1 values(‘小胖’,’男’, 16);?? –如果沒(méi)有指定字段列表,那么要插入的值要和列中的字段順序一樣
?? ?insert into teacher_1(name,age) values(‘小未’, 19);
?? ?insert into teacher_1 values(‘阿哈’,’女’,18);
查詢(xún)數(shù)據(jù)(讀取數(shù)據(jù)read)
語(yǔ)法: select 字段列表 from 表名 where 查詢(xún)條件
例: select name,age from teacher_1;
例: select * from teacher_1;?? –如果字段列表使用*號(hào)來(lái)代替, 那么表示查詢(xún)所有的字段
例: select * from teacher_1 where name = ‘胖胖’;??? –可能使用查詢(xún)條件進(jìn)行數(shù)據(jù)過(guò)濾,拿到想要的數(shù)據(jù);
例: select * from teacher_1 where 1;?????? –where 1表示條件永遠(yuǎn)成立
?? ?select * from teacher_1 where 0;
修改數(shù)據(jù)(update)
語(yǔ)法: update 表名 set 字段=新值,… where 條件
例: update teacher_1 set sex=’女’ where name = ‘小胖’;
?? ?update teacher_1 set sex = ‘保密’, age = 15, name = ‘阿呵’ where name = ‘阿哈’;
刪除數(shù)據(jù)(delete)
語(yǔ)法: delete from 表名 where 條件
例: delete from teacher_1 where age = ’18’;
例: delete from teacher_1;?? –如果沒(méi)有條件進(jìn)行刪除,則會(huì)刪除整個(gè)表的刪除(不同于drop table teacher_1)
ps: 在刪除數(shù)據(jù)時(shí),一定要給一個(gè)具有嚴(yán)格邏輯判斷條件,不然很容易造成數(shù)據(jù)誤刪除,最后造成數(shù)據(jù)的損失
curd(create update read delete)–增刪改查