语句
创建一条一个表
# create table Department
#(
#DepId int auto_increment PRIMARY KEY,
#DepName VARCHAR(50),
#DepInfo VARCHAR(50),
#DepGG VARCHAR(50)
#)
#删除一列
alter table department DROP COLUMN DepGG
#增加一列 默认是增加一列
alter table department add DepSS VARCHAR(20)
#手动修改列的数据类型
alter table department MODIFY column DepSS char(78)
#为表增加一个主键约束
alter table department add constraint PK_Department_DepId PRIMARY key (DepId)
#为表增加一个非空的约束(修改列)
alter table department modify column DepName varchar(50) not NULL
#为DepName 增加一个唯一约束
alter table department add constraint UQ_Department_DepName
UNIQUE(DepName)
#为DepInfo增加一个检查约束,要求性别只能是:'t' or 'h'
alter table department add constraint CK_department_EmpGendercheck CHECK(DepInfo="t" or DepInfo="h")
#增加外键约束
alter table department add constraint FK_department_Department
foreign key (DepId) REFERENCES record(id)
#删除约束
alter table department drop constraint CK_department_EmpGendercheck
Distinct去除重复数据
. select distinct sName from student
. select distinct sName,sAge from student
DISTINCT是对查询出的整个结果集进行数据重复处理的,而不是针对某一个列。
#select * from tblstudent
#按照年龄,降序排序
#select * from Tblstudent order by tsAge desc
#按照年龄,升序排序 默认是升序排序
select * from Tblstudent order by tsAge asc
#统计出所有人的年龄的总和
select sum(tsAge) as 年龄总和 from tblstudent
#--统计当前表中一共有多少条记录
#select count(*)from tblstudent
#--模糊查询:
#--通配符:_ % [] ^
# _表示任意的单个字符
select * from tblstudent where tsName like '张_'
#--% 匹配任意多个任意字符
#-- 无论姓名字数,只要第一个字符是'张'的就查询出来
select * from tblstudent where tsName like '张%'
# []表示筛选,范围。
select * from tblstudent where tsName like '张[0-3]瓦'
--现根据英语成绩排序,再根据数学成绩排序(先按照英语成绩排序,当英语成绩相同的时候再按照数学成绩排序)
select * from Tbiscore order by tEnglish desc , tmath desc
----对分组以后的数据进行筛选:使用having
---having与where都是对数据进行筛选,where是对分组前的每一行数据进行筛选,而having是对分组后的每一组数据进行筛选
---类型转换函数--
select 100+200
select 100+'1000'
---大多数情况下,联合的时候不需要去除重复,同时要保持数据的顺序,所以一般建议使用 union all