创建表语句:
CREATE TABLE employee(
eid INT,
ename VARCHAR(20),
gender CHAR(2),
birthday DATE,
email VARCHAR(20),
remark VARCHAR(50)
);
Employee:表名; eid、ename、gende、birthday、email、remark:字段名称;
DATE、INT:字段类型; VARCHAR(20):类型长度
查看所有表语句:
show tables;
如图所示:
查看表结构:
desc employee;
如图所示:
删除表:
drop table Employee;
在表中添加字段:
ALTER TABLE employee ADD COLUMN age INT;
在表中删除字段:
ALTER TABLE employee DROP COLUMN remark;
在表中修改字段类型:
ALTER TABLE employee MODIFY COLUMN email VARCHAR(50);
在表中修改字段名称:
ALTER TABLE employee CHANGE COLUMN ename(原名称) username(修改后名称) VARCHAR(20);
在表中修改表名称:
alter table student rename to teacher;
在表中插入数据:
INSERT INTO USER VALUES(1,'张三','1999-09-09',100,'淄博');
修改字段:
UPDATE USER SET sid=2 WHERE sname='李四';
UPDATE USER SET birthday='2019-08-30' WHERE sid=2;
修改多个字段:
UPDATE USER SET birthday='2018-08-30',score=99 WHERE sid=2;
查询所有字段:
SELECT * FROM USER
查询某些字段:
SELECT eid,username,gender FROM employee;
查询时添加常量列:
SELECT eid,username,gender,age,'技术部'AS'部门' FROM employee;
SELECT eid AS'编号',username FROM employee;
插入列:
ALTER TABLE employee ADD COLUMN js INT;
查询时合并列:
SELECT eid,username,(js+java+css+cj)AS'总成绩'FROM employee;
统计表中有哪些性别:
SELECT DISTINCT gender FROM employee;
查询表中的所有男的姓名:
SELECT username FROM employee WHERE gender='男';
查询表中姓张的人的所有信息:
SELECT * FROM employee WHERE username LIKE '张%';
查询表中有效行数:
SELECT COUNT(*)FROM employee;
limit分页 *起始行,查询几行
起始行=(当前页-1)*每页显示的条数
第一页:
SELECT * FROM employee LIMIT 0,2;
顺序排序(递增):
SELECT * FROM employee ORDER BY js ASC;
查询表中男女总人数大于2的:
SELECT gender,COUNT(*) FROM employee GROUP BY gender HAVING COUNT(*)>2;