1、创建表:
关键字(保留字)CREATETABLE
例子一:教师表
CREATETABLEteacher(teacher_idvarchar(50)notnull,teacher_namevarchar(100)notnulldefault'',gendervarchar(10)notnulldefault'F',);
例子二:班级表
CREATETABLEclass(class_idvarchar(50)notnull,class_namevarchar(100)notnulldefault'');
例子三:学生表
CREATETABLEstudent(student_idvarchar(50)notnull,student_namevarchar(100)notnulldefault'',gendervarchar(10)notnulldefault'',ageintegernotnulldefault0,class_idvarchar(50)notnulldefault'')
2、表的修改操作:
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)
3、查询数据:SELECT字段列表FROM表名;
SELECTteacher_idFROMteacher;
SELECTteacher_id,teacher_name,genderFROMteacher;
4、排序返回结果:SELECT字段列表FROM表名
ORDERBY字段1[ASC/DESC],字段2[ASC/DESC]...;(ASC升序,DESC降序)
按age升序排列:SELECTclass_id,student_id,student_name,gender,birth_day,ageFROMstudentORDERBYageASC;#等价写法如下:SELECTclass_id,student_id,student_name,gender,birth_day,ageFROMstudentORDERBYage;
按age和student_name升序排列:SELECTclass_id,student_id,student_name,gender,birth_day,ageFROMstudentORDERBYageASC,student_nameASC;#等价写法如下:SELECTclass_id,student_id,student_name,gender,birth_day,ageFROMstudentORDERBY6ASC,3ASC;#6和3表示排在第6和排在第3的字段
返回前几行:关键字LIMIT
写法:
SELECT字段列表FROM表名LIMIT10;#返回查询结果的前10行
返回中间几行:关键字LIMITMOFFSETN(从第N行开始,返回M行记录)
两种写法:
SELECT字段列表FROM表名LIMITMOFFSETN;
SELECT字段列表FROM表名LIMITN,M;
特例:SELECT字段列表FROM表名LIMIT0,10;等价于
SELECT字段列表FROM表名LIMIT10;
返回后几行:TOP、LIMIT
举例:
第1步:先按学生编号倒序排序;
第2步:返回排序后的前5行;
第3步:将前5行数据升序排序;
SELECT*FROM(SELECTTOP5*FROMstudentORDERBYstudent_idDESC)aORDERBYstudent_idASC
5、过滤数据:关键字WHERE
SELECT字段列表FROM表名WHERE过滤条件:
例子:
SELECTstudent_id,student_nameFROMstudentWHEREgender='男';
SELECT*FROMstudentWHEREage<>10;
过滤NULL值:
例子:
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)0
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)1
关键字BETWEEN:
例子:
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)0
关键字IN(包含)、NOTIN(不包含):
例子:
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)0
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)0
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)0
6、高级过滤数据:
(1)、使用通配符过滤数据:关键字LIKE
百分号%匹配0~多个任意字符
下划线_匹配1个任意字符
方括号[]、[^]匹配1个字符集中的字符#MySQL不支持方括号
例子:
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)6
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)7
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)8
以下两句不支持在MySQL中查询:
更新表:RENAMETABLE旧表名TO新表名;更新字段名:ALTERTABLE表名CHANGE旧字段名新字段名字段类型;更新字段类型、NULL值、默认值:ALTERTABLE表名MODIFY字段名新字段类型;删除字段默认值:ALTERTABLE表名ALTERCOLUMN字段名DROPDEFAULT;设置主键:ALTERTABLE表名ADDPRIMARYKEY(主键字段列表);删除主键:ALTERTABLE表名DROPPRIMARYKEY;新增字段:ALTERTABLE表名ADDCOLUMN新字段名字段类型及其他属性;删除字段:ALTERTABLE表名DROPCOLUMN字段名;删除表:DROPTABLE表名;克隆表:SELECT*INTOteacher_bakFROMteacher;(方法一)CREATETABLEteacher_bakASSELECT*FROMteacher(方法二)(以表名teacher为例)插入整行字段:INSERTINTOteacherVALUES('T0001','高齐妍','男');(方法一)INSERTINTOteacher(teacher_id,teacher_name,gender)VALUES('T0001','高齐妍','男');(方法二更安全)9
SELECTteacher_idFROMteacher;0
使用通配符的注意点:
不要过度使用通配符;
如果确实需要使用,也尽量不要把通配符用在匹配模式的开始处;
要特别注意通配符的位置,否则很有可能返回的结果与预期不一致。