SQL server資料庫的學習內容

2021-07-05 06:32:02 字數 3626 閱讀 2232

這周我們學習了sql server資料庫的知識。主要學習內容如下:

主鍵:在關係型資料庫表中,用乙個唯一的識別符號來標識每一行,這個識別符號就是主鍵(primary key)。外來鍵:在關係型資料庫中,外來鍵(forergn  key)就是用來表達表和表之間的關聯關係。

結構化查詢語言sql:

sql是一種用於管理關係型資料庫。並與資料庫中的資料進行通訊的計算機語言。

sql資料庫**的建立內容:

-- 建立資料庫

create database producttest

use producttest

-- 刪除資料庫

drop database producttest

-- 建立**

create table t_product(

id int primary key auto_increment, -- id

productname varchar(50),-- 商品名

price decimal(10,2),-- **

factory varchar(10),-- 產地

createdate date,-- 生產日期

keepdate int, -- 保質期

producttype varchar(20) -- 類別

)charset=utf8;

-- 刪除**

drop table t_product;

-- 新增

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('鴿子蛋',20,'山東','2015-08-26',30,'禽蛋類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('黃瓜',3,'宜賓','2015-09-01',10,'蔬菜類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('鯉魚',10,'綿陽','2015-09-10',30,'魚類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('鯽魚',8,'遂寧','2015-09-05',15,'魚類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('鵪鶉蛋',20,'江蘇','2015-06-28',60,'禽蛋類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('鴨蛋',10,'山東','2015-08-20',30,'禽蛋類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('雞蛋',7,'成都','2015-07-10',30,'禽蛋類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('蝦',4,'浙江','2015-08-20',60,'海鮮類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('魷魚',30,'福建','2015-06-25',60,'海鮮類');

insert into t_product(productname,price,factory,createdate,keepdate,producttype) values ('大蒜',5,'德陽','2015-01-18',60,'蔬菜類');

-- 查詢表中所有的記錄

select*from t_product;

-- 刪除id為3的記錄

delete from t_product where id=3;

-- 修改商品大蒜**為9元

update t_product set price=9 where productname='大蒜';

-- 所有海鮮類商品打8折

update t_product set price=price*0.8 where producttype='海鮮類';

-- 所有禽蛋類商品**上浮2%

update t_product set price=price*1.02 where producttype='禽蛋類';

-- 查詢所有魚類商品

select*from t_product where producttype='魚類';

-- 查詢所有保質期3個月以上的商品

select*from t_product where keepdate>90;

-- 查詢所有山東省產的禽蛋類商品(產地模糊查詢)

select*from t_product where factory like '%山東%' and producttype='禽蛋類';

-- 查詢所有2023年9月份所產的商品

select*from t_product where createdate>='2015-09-01' and createdate<'2015-10-01';

-- 查詢所有過期商品

-- now()得到當前時間

-- date_add(date,int)在指定時間上增加或減少相應的值

select*from t_product p where

date_add(p.createdate,interval p.keepdate day) -- 查詢所有的類別

-- distinct去掉重複的記錄

select distinct p.producttype from t_product p;

-- limit 0,3 從第一條記錄開始顯示3條記錄

select*from t_product limit 0,3;

-- in查詢id在(5,8,10)範圍內的商品

select*from t_product where id in (5,8,10);

insert into t_product(productname,price,factory) values('人參',1000,'長白山製藥廠');

-- 查詢生產日期為null的商品

-- 查詢null值使用 is null 和is not null

select*from t_product where createdate is null

select*from t_product where createdate is not null

-- 按**進行排序,預設為asc公升序,加上desc為降序

select*from t_product order by price asc;

select*from t_product order by price desc;

SQLServer資料庫學習總結

1.sql基礎 sql server2000安裝 配置,伺服器啟動 停止,企業管理器 查詢分析器 第一代資料庫 網狀資料庫和層次資料庫 第二代資料庫 關聯式資料庫 資料庫 db 資料庫管理系統 dbms 資料庫系統 dbs sql server 2000 提供了不同版本 企業版 標準版 個人版 開發...

QT學習 連線sql server資料庫

使用vs2017編譯的qt連線sql server,如果沒有設定好資料來源,會出現 未發現資料來源名稱並且未指定預設驅動程式 的錯誤,具體解決辦法參考 連線資料庫 如下 連線sqlserver資料庫 odbc資料來源使用者名稱,見 控制面板 管理工具 odbc資料來源 qstring dsn qst...

SQL SERVER資料庫索引 學習筆記

定義 索引是對資料庫表中一列或多列的值進行排序的一種結構,使用索引可快速訪問資料庫表中的特定資訊。優點與缺點 建立索引的目的是加快對錶中記錄的查詢或排序。為表設定索引要付出代價的 一是增加了資料庫的儲存空間,二是在插入和修改資料時要花費較多的時間 因為索引也要隨之變動 唯一索引唯一索引是不允許其中任...