国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

看到當年自己學SQL Server 的筆記

2024-08-31 00:55:09
字體:
來源:轉載
供稿:網友
看到當年自己學SQL Server 的筆記
  1 數據庫  2 數據量DataBase,不同類型的數據應該放到不同的數據庫中,  3     1.便于對各個數據類別進行個性管理  4     2.避免命名沖突  5     3.安全性更高;  6 table(表):數據庫中的關系指的就是表;  7     一張表就是一個類,列就是類的字段,行就是一個類的對象  8 數據庫的主鍵(PRimary key);  9     主鍵就是數據行的唯一標識。不會重復的列才能當主鍵。一個表可以沒有主鍵,但是會非常難以處理,因此沒有特殊理由表都要設定主鍵 10     主鍵有兩種選用策略:業務主鍵和邏輯主鍵。業務主鍵是使用有業務意義的字段做主鍵,比如身份證號、銀行賬號等;邏輯主鍵是使用沒有任何業務意義的字段做主鍵,完全給程序看的,業務人員不會看的數據。因為很難保證業務主鍵不會重復(身份證號重復)、不會變化(帳號升位),因此推薦用邏輯主鍵。 11  12     1.主鍵的作用:唯一標識表中的一條記錄。 13     2.選擇多列同時作為一個主鍵→組合主鍵(復合主鍵).(一般不建議采用) 14     組合主鍵-復合主鍵(多列同時作為主鍵)—不推薦 15     1.唯一的。不能為空值 16     2.不經常變化的(穩定)比較穩定的列(不經常更新的,最好是建好以后再也不更新。) 17     3.大量字符串的列不適合作為主鍵 18     4.優先選擇單列作為主鍵(避免使用組合主鍵) 19     5.優先使用邏輯主鍵(沒有意義的),避免使用業務主鍵(身份證號、工號等。) 20  21 主鍵表,外鍵表 22     外鍵表就是A表引用了B表的主鍵,那么A表叫做外鍵表, 23     B表叫做主鍵表,兩者之間的聯系通過主鍵和外鍵聯系, 24  25 創建數據庫 26     create database MyDatabase 27     on 28     ( 29         name='文件名', 30         filename='文件地址', 31         size=3mb, 32         filegrowth=1mb,--自動增長     33     ) 34     log on --操作日志文件 35     ( 36         name='文件名_log', 37         filename='地址名_log', 38         size=1mb, 39         filegrowth=10%, 40     ) 41  42 創建表 43     create table Student 44     ( 45         stuid int primary key identity(1,1), 46         stuName nvarchar(15) not null, 47         stuGender bit not null, 48         stuAge int , 49         stuClass nvarchar(20), 50     ) 51      52 數據類型 53     nvarchar(10),存10個漢字,10個字母 54     varchar(10),里面的內容可以是10個也可以不是10,少于10個 55     char(10),5個漢字,10個字母, 如果存的內容(字母)小于10,不足的用空格補充.固定的內容 56     nchar(10),凡是帶n的那么就可以存漢字 57     int bit(true,false)寫代碼的時候用1,0的方式 float 58     --查詢表中的全部內容 59     select *from Student--(表名) 60  61 向表中插入數據 62     --第一種方式 63     insert into Student(stuName,stuGender,stuAge,stuClass)values('劉備',1,27,'三年四班') 64     --第二種方式 65     insert into Student values('小喬',0,18,'三年二班') 66     --第三種,可以多條插入 用select 和union 連接 67     insert into Student 68     select '貂蟬',0,18,'三年四班' union 69     select '郭嘉',1,38,'三年三班' union 70     select '張飛',1,34,'三年四班' 71 修改表中的數據 72     --修改數據 關鍵字 是update <表名> set <要修改的列名=要修改的值> where 條件 73     update Student  set stuAge-=1 74     update Student set stuAge=30 where stuid=1 75  76 刪除數據 77     --刪除數據 delete from <表名> where <條件> 78     delete from student where stuid=7 79     --delete 刪除的是數據,表還在,但自動增長的Id會接著已經刪除的id接著加,不會在從1開始 80     --第二種刪除方式 能把表給刪除 81     drop table student 82     --第三種方i 式刪除 刪除的也是數據 表還在 但id是從1開始 83     truncate table student 84     --truncate 效率要比 delete 快的多  85  86 約束--非空約束 87     not null 88     --主鍵約束 89     primary key  90     --為年齡增加一個檢查約束:年齡必須在-120歲之間,含歲與歲。 91     alter table student add constraint CK_stuAge check(stuAge >=0 and stuAge<=120) 92     --為EmpId增加一個主鍵約束 93     alter table student add constraint PK_stuId primary key(stuid) 94     --非空約束,為stuName增加一個非空約束 95     alter table student alter column stuName varchar(50) not  null 96     --為stuName增加一個唯一約束 97     alter table student add constraint UQ_stuName unique(stuName) 98     --為性別增加一個默認約束,默認為'男' 99     alter table student add constraint DF_stuGender default('男') for stuGender100     --手動修改一下stuName的數據類型(varchar(200))101     alter table student alter column stuName varchar(200)102 103 增加刪除列104     --增加一個EmpId列 105     --alter  table <表名> add <要加的列名> <數據類型>106     alter table student add EmpId int107 108     --手動刪除一列(刪除列) 109     --alter table <表名> drop column <要刪除的列名>110     alter table student drop column   empid  --(要刪除的列名)111 112 查詢表中的內容113     --第一種方式114     select stuName as '姓名',stuAge as '年齡'from student115     --第二種方式116     select 姓名=stuName,年齡=stuAge from student117     118     --帶條件查詢119     select *from Student where stuGender=0120     --查找前十位 top 121     select top 10 * from TblStudent122 123     --查找按照年齡從小到大 排序 order by  asc 124     select *from TblStudent order by TSAge  asc125 126     --查找按照年齡從大到小 排序 order by  desc 127     select *from TblStudent order by TSAge desc128 129     --查找年齡最小的前十位130     select top 10 * from tblstudent order by TSAge asc131 132     --查找年齡最大的顯示總數的10%133 134     select top 10  percent * from TblStudent order by TSAge desc135 136     --去除重復姓名查找137     select distinct TSName as '姓名' from TblStudent 138 139 聚合函數140 141     --查詢多少行; count()只能有一個參數142     select COUNT(TSName) as '人數' from TblStudent143     --也可以這樣查詢144     select COUNT(TSName) as '人數',COUNT(TSCardId) as '標號' from TblStudent145 146     --切換成績表147     select *from TblScore148 149     --查詢英語成績最高150     select MAX(tsenglish) as '英語最高' from TblScore151 152     --查詢英語最低 和數字最高分153     select MIN(tsenglish) as '英語最低',MAX(tsmath) as '數學最高' from TblScore154     155     --聚合函數還有sum()求和,avg()求平均值,len()長度,156     157 多條件查詢158     --查詢年齡在20-30之間的男生159     select * from student where stuage>=20 and stuage<=30 and stugender=1160     第二種做法 用between ...and ...161     select * from student where stuAge between 20 and 30 and stugender =1162     --in 查詢班級id 為1 或者2  或者 3 的同學信息163     select * from student where classid=1 or classid=2 or classid=3164     第二種做法165     select * fromstudent where classid in(1,2,3)166 模糊查詢167     --名字中已張字開頭的%代表任何內容,一字或多字168     select 8 from student where tsName like '張%'169     --姓張的后面跟個%符號的,170     select 8 from student where tsName like '張[%]%'171     --查詢姓張的的但是后面只能有一個字'_' 172     select 8 from student where tsName like '張_'173     --查詢姓張的姓名一共三個字174     select 8 from student where tsName like '張%' and len(tsName)=3175 176 數據分組    177     --查詢每個班級的id有多少人178     --group by 分組179     select stuid as '班級的id',180     count(*) as '人數'181     from student182     group by stuid183     184     --查詢每個班中男生有多少人185     select stuid as '班級',186     count(*) as '人數'187     from student188     where stuGender=1189     group by stuid190     191     --查詢每個班人數超過5個的人192     select stuid as '班級',193     count(*) as '人數'194     from student195     group by stuid196     having count(*) in(5)197     --注意having 不能使用在未分組的列 having 是篩選的意思198     199     --統計每種商品的總銷售量 并進行降序排序200     select 商品名稱,201     SUM(銷售數量) as '總銷售量'202     from MyOrders203     group by 商品名稱204     having SUM(銷售數量) >100205     order by '總銷售量' desc206     207     --請統計總銷售價格超過3000元的商品和銷售總價 并進行降序排序208     select 商品名稱,209     SUM(銷售數量*銷售價格) as '總銷售價'210     from MyOrders211     group by 商品名稱212     having SUM(銷售數量*銷售價格)>3000213     order by '總銷售價' desc214         215     --統計各個客戶對可口可樂的喜愛度(統計每個購買人對可口可樂的購買數量)216     select 購買人,217     sum(銷售數量)as '銷售總量'218     from MyOrders219     where 商品名稱='可口可樂'220     group by 購買人221     order by '銷售總量' desc    222 223 類型轉換函數224 數據類型轉換的語法是225     如果要把int類型轉換為字符串 convert(char,列名)226     例如如果要把所有的信息放在一個單元格之中的語法就是227     select stuname+convert(varchar(10),stuAge)+stuClass from student228 229     --表連接,首先列數相同 ,類型相同230 231     select *from TblTeacher232 233     select *from TblStudent234 235     select ttname ,ttgender from TblTeacher236     union --單獨的union 去除重復 加上all 不去重復237     select tsgender,tsaddress from TblStudent238     239     select * from TblScore240     241     --查詢學生的的 最高成績 最低成績 總分242     --第一種243     select max(tenglish) as '最高成績',244     MIN(tenglish) as '最低成績',245     SUM(tenglish) as '總成績'246     from TblScore247     248     --第二種249     select '最高成績',MAX(tenglish) from TblScore250     union251     select '最低成績',MIN(tenglish) from TblScore252     union 253     select '總成績',SUM(tenglish) from TblScore254 255     1 2 3 變成列256     select 1 union257     select 2 union258     select 3 259     260 數據備份--261     select *from TblStudent262     --把TblStudent表中的結構和數據,存到一個新表中,這個新表可以沒有,在備份數據的同時新表自動生成263     --就是把一個表復制過來264     select *into newd from TblStudent265     --表復制了 但沒有數據266     select *into newd1 from TblStudent where 1<>1267     --第三種 表有了 沒有數據 建議使用這個268     select top 0 * into newd2 from TblStudent269     --表要存在的一中270     insert into newd3 select *from TblStudent271 272 字符串函數273     select len("字符串的長度");274     select datalength('aa');--獲得字符串的字節數275     --獲得tblstudent表中tsname的字節數276     select *,datalength(tsName)from tblstudent277     select lower('轉小寫');278     select upper('轉大寫');279     select rtrim('去除字符串右側的空格');280     select ltrim('去除字符串左側的空格');281     select left('截取左邊字符串',2)--'截取' 從左邊切2個282     select right('從右邊切字符串',2)-- '符串' 從右邊切2個283
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝清县| 长岛县| 铜梁县| 泸水县| 奉化市| 芦溪县| 岚皋县| 临洮县| 偃师市| 杭锦后旗| 祁连县| 双峰县| 确山县| 清苑县| 高州市| 弥渡县| 虎林市| 永泰县| 大新县| 南开区| 喀喇沁旗| 平度市| 临武县| 湘潭市| 东阿县| 杭州市| 田林县| 广汉市| 东源县| 嘉祥县| 康保县| 冕宁县| 梅州市| 文水县| 柏乡县| 富源县| 翁源县| 永寿县| 高阳县| 南和县| 辽宁省|