您的位置:首页 > 数据库

SQL server数据库的学习内容

2015-09-13 14:13 351 查看
这周我们学习了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='禽蛋类';

-- 查询所有2015年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) <NOW();

-- 查询所有的类别

-- 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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: