显示数据库
Show databases;
创建数据库
create database test1 ;
删除数据库
DROP DATABASE test1
使用数据库
Use test1
创建表
create table student (
学号 int, 姓名 varchar(32), 性别 char(2), 年龄 int, 年级int, 专业 varchar(50)
)character set utf8;
追加字符集
alter table student character set utf8;
插入单行
insert into Strdents (姓名,性别,出生日期) values ('开心朋朋','男','1980/6/15');
删单行
delete from a where name='ws';
删单列
alter table staff drop column 列名
删全部
truncate table student
delete from student;
删除表
Drop table student
删除数据库
Drop database test1
查全部
select * from student;
更新
update student set 年龄=18 where 姓名='ws';
查看表的结构
desc student;
show create table student;(查看建表语句)
修改表名称
Rename table student to student1;
删除列名
Alter table student Drop name;
修改列名
Alter table student change column namenewname varchar(20);
修改数据类型
Alter table student modify sexvarchar(2)
更新某种数据
update salary set salary=5000.00 (更改多个,号隔开)
增设主键
alter table table_name add column id int(11) PRIMARY KEY AUTO_INCREMENT;
去掉重复元素 关键字 distinct
select distinct name from studentwhere sum>=200;
使用别名表示学生分数。
selectNAME as 姓名总分 from student;
查询每个学生的总分
selectsum(score) from student group by name;
约束
#自增长
auto_increment
#非空
not null
#默认值
default 'xx'
#唯一
unique
#指定字符集
charset
#主键
primary key
#外键
增加两个表之间的联系
SELECTDISTINCT a.money from students a ;#去重
SELECT COUNT(*) '学生人数' from students where sex='女'; #统计行数
SELECT MAX(a.money) 钱最多 from students a; #最大值
SELECT min(money) 钱最少 from students;#最小值
SELECT AVG(a.money) 平均多少钱 from students a; #平均数
SELECT sum(a.money) 总共多少钱 from students a;#总和
SELECT sex 性别,count(*) 人数 from students GROUP BY sex; #分组
模糊查询
select * fromstudent where name like "%s";
查询 两者之间的数据
select * fromstudent where score in (85,95);
升序排列
select * fromstudent order by stuid asc; 降序为desc
合并
select stuid fromstudent union all select name from student;
union all 有重复数据 union 去掉重复数据
Delete 可以回滚 truncate 不可回滚(快速删除)
联合主键(多个列何在一起设为主键) primary key
约束关键字 constraint
外加
Alter tablestudent add constraint pk_in primary key(stuid,course);
创表时添加
Create tableteacher(
Id varchar(32),
Name varchar(32).
Sex varchar(32),
Primary key(id,name)
)