SQL语句具体使用样例

1、建立person表

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person`  (
  `Id` int(10) NOT NULL AUTO_INCREMENT,
  `LastName` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  `FirstName` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  `Address` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  `City` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  PRIMARY KEY (`Id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 19 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES (1, 'Adams', 'John', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (2, 'Adams', 'John', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (3, 'Bush', 'George', '	Fifth Avenue	', '	New York');
INSERT INTO `person` VALUES (4, 'Carter', 'Thomas', '	Changan Street	', '	Beijing');
INSERT INTO `person` VALUES (5, 'Adams', 'John', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (6, 'Adams', 'John', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (7, 'Adams', 'John', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (8, 'Adams', 'John', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (9, 'Adams', 'Thomas', '	Oxford Street', 'London');
INSERT INTO `person` VALUES (10, 'aaa', 'aaa', 'a', 'a');
INSERT INTO `person` VALUES (11, 'b', 'b', 'b', 'b');
INSERT INTO `person` VALUES (12, 'c', 'c', 'c', 'c');
INSERT INTO `person` VALUES (13, 'd', 'd', 'd', 'd');
INSERT INTO `person` VALUES (14, 'e', 'e', 'e', 'e');
INSERT INTO `person` VALUES (15, 'f', 'f', 'f', 'London');
INSERT INTO `person` VALUES (16, 'g', 'g', 'g', 'London');
INSERT INTO `person` VALUES (17, 'h', 'h', 'h', NULL);
INSERT INTO `person` VALUES (18, 'j', NULL, 'j', 'j');

SET FOREIGN_KEY_CHECKS = 1;
USE custom
-- 创建表 
create table person (
    Id INT(10) PRIMARY KEY auto_increment,
    LastName VARCHAR(10),
    FirstName VARCHAR(10),
    Address VARCHAR(10),
    City VARCHAR(10)

);
-- 插入数据
insert into person(Id,LastName,FirstName,Address,City)VALUES(null,"Adams",    "John","    Oxford Street","    London");
insert into person(Id,LastName,FirstName,Address,City)VALUES(null,"Bush    ",    "George    ","    Fifth Avenue    ","    New York");
insert into person(Id,LastName,FirstName,Address,City)VALUES(null,"Carter    ",    "Thomas    ","    Changan Street    ","    Beijing");
insert into person(Id,LastName,FirstName,Address,City)VALUES(null,"Adams",    "John","    Oxford Street","    London");
insert into person(Id,LastName,FirstName,Address,City)VALUES(null,"Adams",    "Thomas","    Oxford Street","    London");
-- 选择语句select
select * from person;
-- 选择去除重复SELECT DISTINCT
SELECT DISTINCT LastName,FirstName from person;
-- 更新表数据
-- top子句,在mysql中使用limit
select * from person LIMIT 3
-- in操作符
SELECT * from person WHERE LastName in ('Adams','Carter    ') ORDER BY LastName;
-- 多个字段使用in操作符的情况
SELECT * from person WHERE (LastName,FirstName) in (('Adams','John'),('Carter','Thomas'));
-- BETWEEN...AND 在他们之间,两边都包含
SELECT * FROM person where LastName BETWEEN 'a' and 'c' ORDER BY LastName ASC
-- not操作符,不在这个之间的值
SELECT * FROM person where LastName  NOT BETWEEN 'a' and 'Carter' ORDER BY LastName ASC
SELECT * FROM person where LastName  NOT BETWEEN 'a' and 'Carter' ORDER BY LastName DESC

-- 更新操作
-- 更新单一字段
update person set LastName = 'aaabbb' where City = 'a';
-- 更新多个字段
update person set LastName = 'aaa',FirstName = 'aaa' where City = 'a';

CREATE TABLE Location
(
Id int PRIMARY KEY auto_increment,
LocationName varchar(255) NOT NULL,
P_id varchar(255)
)

-- unique约束
CREATE TABLE P1
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (Id_P)
)

-- 为unique约束命名并且为多个列添加该约束
CREATE TABLE P2
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT unique_colum UNIQUE(Id_p,LastName)
)

-- 修改列的约束
alter table p1 add UNIQUE(Id_P)

-- 撤销unique约束
alter table p1 drop index 约束名

-- 外键foreignkey
ALTER TABLE location add FOREIGN Key(P_id) references person(Id)

-- 索引的创建
CREATE INDEX id_index
ON p1 (City)

-- 查看表
DESC p2

-- 为p1的city添加索引
CREATE INDEX city_index on p1(City)


-- 删除p1表中的名字为id_index的索引
ALTER TABLE p1 DROP INDEX id_index

-- 在原来的表中添加列
ALTER TABLE p1 ADD COLUMN Birthday DATE

-- 修改列的类型
ALTER TABLE p1 MODIFY Birthday VARCHAR(255)

-- 修改列名
ALTER TABLE p1 CHANGE birthday Date_birth YEAR;


-- 指定auto_increment的开始起始值,默认从0开始

-- 表重命名
alter table p2 rename to person2

-- 添加列
alter table person2 add column birthday varchar(255);

-- 删除列
alter table person2 drop column FullName;

-- 修改列名
alter table person2 change birthday Date_birth varchar(255);-- 要记得后面一定要加上类型,否则会报错

-- 修改列名的类型
alter table person2 modify City varchar(100);

-- 视图操作
create view cityView AS
select FirstName,LastName,City
from person
where City = 'London';

-- 对上面的视图进行操作
select person.* from person inner join cityView on person.City = cityView.City;

-- 更新视图
create or replace view cityView as
select FirstName,LastName,City
from person
where City = 'London';

-- 删除视图
drop view cityView;

-- 日期函数
-- 当前的日期和时间
select NOW();

-- 当前日期
select curdate();

-- 当前时间
select curtime();

-- 提取日期或日期/时间表达式的日期部分
select date(now())
select date(expr)

-- 返回日期/时间按的单独部分
select extract(year FROM now()) year
-- 给日期添加指定的时间间隔
select date(date_add(now(),interval 2 year))
date_add(date,INTERVAL expr type)
select date_add()    

-- 从日期减去指定的时间间隔
select date_sub();

-- 返回两个日期之间的天数
select datefiff(date(date_add(now(),interval 2 year)),now());
select datefiff(date1,date2);

-- 用不同的格式显示日期/时间
select DATE_FORMAT(now(),'%m-%d-%Y')
select DATE_FORMAT()    

-- 空值null的处理
SELECT LastName,COALESCE(FirstName,'exception')
FROM person;

-- 聚合函数
select avg(OrderPrice) AS op from orders;

-- 统计个数
select count(distinct City) from person

-- 最大值和最小值
select max(City) from person order by City asc; 

-- 统计总和
select sum(OrderPrice) from orders; 

-- group by 分组
select count(*),City from person GROUP BY City;


-- 谁花的钱是最多的
select sum(OrderPrice) money from orders group by Customer;

SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500

-- 转换大小写
select UCASE(Customer) from orders where OrderPrice=1000;

MID(str,pos,len)

-- ROUND 函数用于把数值字段舍入为指定的小数位数
select ROUND(Price,1) from orders;

 

你可能感兴趣的:(数据库,sql语句简单操作,工作总结)