一、怎樣查看數據庫信息?
desc 數據庫名;
?
二、怎樣查看數據表信息?
desc 表名; ? ? ? ? ?//查看表的屬性和屬性值
或者用select語句: //查看表的行記錄信息
select * from 表名 (where nub='100001');
三、將數據寫入數據庫要經過什么步驟?
1. 創建表:
? ? create datable 數據庫名;
2. 創建用戶:
? ? grant select,create,drop on stud.* to 用戶名@localhost identitied by "密碼"; //(注意雙引號)
3.創建(多個)空表,添加表的屬性和主鍵 :
? ? create table 表名(...主鍵..);
4.增加主鍵:
?
?
Alter
table 表名 add primary key (屬性名1,屬性名2);
5.
列
屬性字段操作(
都是用alter 開頭
):
? ?5.1增加一個列(即屬性):
? ? ? ?
Alter table 表名
add
屬性名 varchar(40) not null;
? ?5.2?修改列字段名稱:(例如修改列屬性名稱和屬性: )
Alter table 表名
change
name 新名字 varchar(20)(必須加上格式限制) not null;
5.3 修改列字段屬性值:(例如改成varchar(30):)
Alter table 表名
modify
屬性名 屬性新值(例如varchar(30)) not null;
? ?5.4.刪除列字段:
Alter table 表名
drop
column 屬性列名;
6. 行 記錄操作( insert ,update ,delete ):
? ?6.1. 往空表里插入行數據(即記錄):
insert into 表名 values('','','',..); //(單引號) 。注意增加列是alter,增加行是insert
? ?6.2.修改(更新)行信息:
? ?
update 用戶名 set 行某個屬性="該行修改后的屬性內容" where ... //注意單引號
例如:update std_info set name='張三豐' where nub='100001'; ? ? ?//將原記錄中名字“張三”改成“張三豐”
6.3刪除表某一行信息(即記錄):
? ? ? ? delete from 表名 where ..='...'; //注意delete 是直接接著from
7.刪除某個表:
drop table 表名;
四、查詢有哪幾種:
1.有序order查詢:
?
select * from 表名 order by 屬性名 desc; //默認升序 。有order?by就沒有where
2.分組group查詢:
?select .. from 表名 group by 屬性名(例如:class,sex); //有group by就沒有where
3.查詢表中行總數(記錄總數):
? select count(*) from 表名;
3.多個表聯合查詢:
select avg(b.score)
as
'平均分'
//查詢可以用as重用名
f
rom
std_info a,score_info b,curricula c
//表名可以直接重用名
where a.nub=b.nub and b.code=c.code and c.Curlum='英語' and a.class='9701';
4. 查看平均分:
select a.curlum as '課程名',avg(score) from curricula a,score_Info b where
a.code=b.code group by a.code;
注意:
?
1.四個聚合函數不能搭配order by。只能group by??
2.group by 有一個原則,就是 select 后面的所有列中,沒有使用聚合函數的列,必須出現在 group
by 后面。最常用的四個聚合函數:count,avg,min,max。
?
?
??
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
