数据库基本语法

create database  数据库; 创建数据库

show databases; 查看数据库列表

use  数据库 ;  选择数据库

create table 表名(); 创建表

show tables; 查看表

desc 表名; 查看表

drop table 表名; 删除表

修改表

alter table 旧表名 rename 新表名; 修改表名

alter table 表名 add 字段名 数据类型 属性; 添加字段

alter table 表名 change 原字段 新字段 数据类型 属性; 修改字段

alter table 表名 drop 字段名; 删除删除字段

添加主键

alter table 表名 add constraint 主键名 primary key 表名 (主键字段)

添加外键

alter table 表名 add comstraint 外建名 foreign key (外键字段) peferences关联表名(关联字段);

set names gbk; 改中文

insert into 表名 (字段名列表)  values(值列表); 插入单条数据

多条同上

select *from 表名 ;  查看表内的内容

create table 新表名(select  字段..... from 源表名);将查询结果插入新表

update 表名 set 字段1=值1,字段2=值2,... where 条件;  更新数据记录

select  列名 as 新列名, ... from 表名 ; 使用AS命名列

select 列名 from 表名  where ‘列名’ is null;查询空行

函数名作用
AVG()返回某字段的平均值
COUNT()返回某字段的行数
MAX()返回某字段的最大值
MIN()返回某字段的最小值
SUM()返回某字段的和

select 字段名列表 from 表名 where 条件 order by 列名 limit 0,0;查找信息条件判断 显示几条信息

selsec 字段名 from 表名 where 字段名=xxx  ;    子查询

select  列名 from 表名 where tid=(select tid from 表名 where sname=‘名字‘);

left 左   right右  inner共有

select avg(studentresult),subjectno from result group by subjectno having avg(studentresult)>70;分组查询用

数据库基本语法

取A B两表共同的地方

select 字段名

from tableA A

inner join tableB B 

ON A.key=B.key;

select s.sid, s.sname,s.tid ,t.name from students s inner join teachr t on s.tid=t.tid;

数据库基本语法

查询A表 所有的地方

select 字段名

from tableA A

left join tableB B

on a.key=b.key;

select s.sid, s.sname,s.tid ,t.name from students s left join teachr t on s.tid=t.tid;

 数据库基本语法

查询a表私有的地方

select 字段名

from tableA A

left join TableB B

on a.key=B.key

where B.key is null:

数据库基本语法

select 字段名

from tableA A

right join TableB B

on A.KEY=B.KEY

数据库基本语法

SELECT 字段名

from TanleA A

right join tableB B

on A.Key=B.key

where A.key is null

相关推荐