mysql基本語句詳細教程
看他們網上的,寫得都是千篇一律,同時,好多也寫得不是很好,下面是我自己總結的有關mysql的使用細節,也是我在學習過程中的一些記錄吧,希望對你有點幫助,后面有關存儲過程等相關操作還沒有總結好,下次總結好了再發給你吧,呵呵~~~~~MySql學習筆記MySql概述:MySql是一個種關聯數據庫管理系統,所謂關聯數據庫就是將數據保存在不同的表中,而不是將所有數據放在一個大的倉庫中。
這樣就增加了速度與提高了靈活性。并且MySql軟件是一個開放源碼軟件。
注意,MySql所支持的TimeStamp的最大范圍的問題,在32位機器上,支持的取值范圍是年份最好不要超過2030年,然后如果在64位的機器上,年份可以達到2106年,而對于date、與datetime這兩種類型,則沒有關系,都可以表示到9999-12-31,所以這一點得注意下;還有,在安裝MySql的時候,我們一般都選擇Typical(典型安裝)就可以了,當然,如果還有其它用途的話,那最好選擇Complete(完全安裝);在安裝過程中,一般的還會讓你進行服務器類型的選擇,分別有三種服務器類型的選擇,(Developer(開發機)、Server Machine(服務器)、Dedicated MySql Server Machine(專用MYSQL服務器)),選擇哪種類型的服務器,只會對配置向導對內存等有影響,不然其它方面是沒有什么影響的;所以,我們如果是開發者,選擇開發機就可以啦;然后接下來,還會有數據庫使用情況對話框的選擇,我們只要按照默認就可以啦;連接與斷開服務器:連接:在windows命令提示符下輸入類似如下命令集:mysql –h host –u user –p例如,我在用的時候輸入的是:mysql –h localhost –u root –p然后會提示要你輸入用戶密碼,這個時候,如果你有密碼的話,就輸入密碼敲回車,如果沒有密碼,直接敲回車,就可以進入到數據庫客戶端;連接遠程主機上的mysql,可以用下面的命令:mysql –h 159.0.45.1 –u root –p 123斷開服務器:在進入客戶端后,你可以直接輸入quit然后回車就可以了;下面就數據庫相關命令進行相關說明你可以輸入以下命令對數據庫表格或者數據庫進行相關操作,在這里就省略了,然后直接進行文字說明了;Select version(),current_date;//從服務器得到當前mysql的版本號與當前日期Select user(); //得到當前數據庫的所有用戶Use databasename; 進入到指定的數據庫當中,然后就可以操作這個數據庫當中的表格了Show databases; //查詢目前數據庫中所有的數據庫,并且顯示出來;Create batabase databasename;創建數據庫,例如:create database manager;Show tables; //查看當前數據庫中的所有表格;Create table tablename(colums);創建表,并且給表指定相關列,例如:create table pet(name varchar(20),owner varchar(20),species varchar(20),sex char(1),birth date,death date);Describe tablename;將表當中的所有信息詳細顯示出來,例如:describe pet;可以用命令一次插入多條記錄,例如:Insert into pet values('Puffball','Diane','hamster','f','1993-12-3',null),( 'Puffball','Diane','hamster','f','1993-12-3',now());Select * from pet; 從pet表當中查詢出所有的記錄,顯示出來;Delete from pet where id=1;刪除ID為1的那一條記錄;Update pet set birth='2001-1-3' where name='Bowser';更新name為Bowser的記錄當中的birth字段的值;Select distinct owner from pet;從pet表中選擇出owner字段的值唯一的行,如果有多行記錄這個字段的值相同,則只顯示最后一次出現這一值的一行記錄;有關日期計算:Select name,birth,curdate(),(year(curdate())-year(birth)) as age from pet;此處,year()函數用于提取對應字段的年份,當然類似的還有month(),day()等;在mysql當中,sql語句可以使用like查詢,可以用”_”配任何單個字符,用”%”配任意數目字符,并且SQL模式默認是忽略大小寫,例如:select * from pet where name like '%fy';當然也可以用正則表達式模式進行配。同時在sql當中,也要注意分組函數、排序函數、統計函數等相關用法,在這里只列舉一二;Select species,count(*) from pet group by speceis;Select * from pet order by birth desc;查詢最大值的相關操作:Select max(age) from pet;取前多少項記錄,這個主要用于分頁查詢操作當中,Select * from pet order by birth desc limit 3;取前三條記錄,Select * from pet order by birth desc limit 0,3;這個可以用于分頁查詢,limit后面的第一個參數,是起始位置,第二個參數是取記錄條數;有關創建表格自增長字段的寫法:Create table person(id int(4) not null auto_increment,name char(20) not null,primary key (id));修改表操作:向表中增加字段:注意,在這個地方,如果是增加多個字段的時候,就要用括號括起來,不然會有問題,如果是單個字段的話,不用括號也沒事;Alter table test add(address varchar(50) not null default 'xm',email varchar(20) not null);將表中某個字段的名字修改或者修改其。
mysql語句練習問題
select *tId,*,*,*,*,*
into TableA
from Student a, Course b, Grade c
where *tId=*tId and *=*
select *tId,*,*,
sum(case cname when "語文" then score else 0 end) as 語文,
sum(case cname when "數學" then score else 0 end) as 數學,
sum(case cname when "英語" then score else 0 end) as 英語,
sum(case cname when "哲學" then score else 0 end) as 哲學,
sum(score)*1.0/4 as "平均成績"
from TableA
group by name
SQL測試題(注:最佳答案必須能在MySQL下運行)
/*閑著沒事,瞅瞅百度上的問題,今天天晚了,先解決一個,另一個明兒個再說了!第二道題也算已經搞定了!環境 : mysql Ver 14.12 Distrib 5.0.45, for Win32 (ia32)參考 : exist與in 的區別http://**change888/archive/2008/03/31/**//*********************************問題 1 **************************************/drop table if exists s;create table if not exists s (s varchar(32), sn varchar(32), sd varchar(32), sa int);insert into s values ('s1', '朱', '開發本部', 23);insert into s values ('s2', '牛', '人事部', 25);insert into s values ('s3', '楊', '財務部', 26);insert into s values ('s4', '馬', '開發本部', 22);insert into s values ('s5', '呂', '人事部', 27);insert into s values ('s6', '于', '開發本部', 28);insert into s values ('s7', '侯', '開發本部', 28);drop table if exists c;create table if not exists c (c varchar(32), cn varchar(32));insert into c values ('c1', '軟件工程');insert into c values ('c2', '計算機技術與科學');insert into c values ('c3', '車輛工程');drop table if exists sc;create table if not exists sc (s varchar(32), c varchar(32)); insert into sc values ('s1', 'c1');insert into sc values ('s1', 'c2');insert into sc values ('s1', 'c3');insert into sc values ('s2', 'c1');insert into sc values ('s2', 'c3');insert into sc values ('s3', 'c2');insert into sc values ('s4', 'c2');insert into sc values ('s4', 'c3');insert into sc values ('s5', 'c1');insert into sc values ('s6', 'c3');/* 1. 查詢選修課程名稱為 “軟件工程” 的學員學號和姓名 */select s.s '學號', * '姓名' from s where s.s in (select sc.s from sc where sc.c in (select c.c from c where * = '軟件工程'));/* 2. 查詢選修課程編號為 “C2” 的學員姓名和所屬單位 */select * '姓名', * '所屬單位' from s where s.s in (select sc.s from sc where sc.c = 'C2');/* 3. 查詢選修課程編號 不 為 “C2” 的學員姓名和所屬單位 */select * '姓名', * '所屬單位' from s where s.s not in (select sc.s from sc where sc.c = 'C2')ands.s in (select sc.s from sc);/* 4. 查詢選修全部課程的學員姓名和所屬單位 */select * '姓名', * '所屬單位' from s where (select count(DISTINCT sc.c) from sc where sc.s = s.s)=(select count(DISTINCT c.c) from c );/* 5. 查詢選修了課程的學員人數 */select count(DISTINCT sc.s) '人數' from sc;/* 6. 查詢選修課程 >= 2 門的學員學號和所屬單位 (不得不用 CASE 語句了)*/select * '姓名', * '所屬單位' from s where s.s in (select CASE WHEN count(DISTINCT sc.c) >=2 THEN sc.s END from sc group by sc.s );/* 運行結果------------------------------------1+------+------+| 學號 | 姓名 |+------+------+| s1 | 朱 || s2 | 牛 || s5 | 呂 |+------+------+------------------------------------2+------+----------+| 姓名 | 所屬單位 |+------+----------+| 朱 | 開發本部 || 楊 | 財務部 || 馬 | 開發本部 |+------+----------+------------------------------------3+------+----------+| 姓名 | 所屬單位 |+------+----------+| 牛 | 人事部 || 呂 | 人事部 || 于 | 開發本部 |+------+----------+------------------------------------4+------+----------+| 姓名 | 所屬單位 |+------+----------+| 朱 | 開發本部 |+------+----------+------------------------------------5+------+| 人數 |+------+| 6 |+------+------------------------------------6+------+----------+| 姓名 | 所屬單位 |+------+----------+| 朱 | 開發本部 || 牛 | 人事部 || 馬 | 開發本部 |+------+----------+*//*********************************問題 2 **************************************/drop table if exists s ;create table if not exists s ( sno varchar(32), sname varchar(32));insert into s values ('s1', '朱');insert into s values ('s2', '牛');insert into s values ('s3', '楊');insert into s values ('s4', '馬');insert into s values ('s5', '呂');insert into s values ('s6', '于');insert into s values ('s7', '侯');drop table if exists c;create table if not exists c ( cno varchar(32), cname varchar(32), cteacher varchar(32));insert into c values ('c1', '數學', '張');insert into c values ('c2', '日語', '李'); /*假設李老師同時教授日語和英語*/insert into c values ('c3', '英語', '李');drop table if exists sc;create table if not exists sc (sno varchar(32), cno varchar(32), scgrade double);insert into sc values ('s1', 'c1', 75);insert into sc values ('s1', 'c2', 70);insert into sc values ('s1', 'c3', 80);insert into sc values ('s2', 'c1', 50);insert into sc values ('s2', 'c3', 40);insert into sc values ('s3', 'c1', 50);insert into sc values ('s3', 'c2', 60);insert into sc values ('s4', 'c1', 90);insert into sc values ('s4', 'c2', 40);insert into sc values ('s4', 'c3', 20);insert into sc values ('s5', 'c1', 80);insert into sc values ('s6', 'c1', 85);/* 1. 沒有 選 修過“李”老師講授課程的所有學生姓名 */select * '姓名' from s where * not in(select * from sc where * in (select * from c where *er = '李'));/* 2. 列出有二門以上(含兩門)不及格課程的學生姓名及其平均成績 */select * '姓名', AVG(*e) '平均成績' from s, sc where * = * (select count(*) from sc where * = * and *。