sql数据库基础语法


1.创建一个数据库

create database student

2.删除一个数据库

drop database student

3.使用数据库

use student

4.查看数据库

shou databases

5.创建一个表

create table student(
id int(4) not null auto_increment ,
name varchar(20) not null default’小红’ comment’名字’,
password varchar(20),
birthday datetime defalut null comment’出生日期’,
address varchar(100) defalut null comment’家庭地址’,
email varchar(50) default null comment’邮箱’
primary key(id)

7.show table student 查看表

8.desc student 显示表的结构

9.修改表名

alter table student rename as student1

10.增加表的字段

alter table student add age int(11)

11.修改表的字段 重命名

alter table student modify age varchar(20)
alter table student change age age1 int(1)

12.删除表的字段

alter table student drop age1

13.删除表

drop table student

14.外键

create table school(
gradeid int(10) not null commnet’年级id’,
gradename varchar(20) not null comment’年级名称’,
primary key(gradeid)
)
create table student(
id int(4) not null auto_increment ,
name varchar(20) not null default’小红’ comment’名字’,
password varchar(20),
birthday datetime defalut null comment’出生日期’,
gradeid int(10) not null commnet’学生的年级’,
address varchar(100) defalut null comment’家庭地址’,
email varchar(50) default null comment’邮箱’,
primary key(id),
key fk_gradeid(gradeid),
constraint fk_gradeid foreing key(gradeid) references shool(gradeid)

alter table student add constraint fk_gradeid foreing key (gradeid) references school(gradeid)

15.插入语句

insert into student (name,password) values(‘李类’,‘12312’)

16.修改语句

update student set name=‘大学’ where id=1

17.删除语句

delete from student where id=1

18清空表

truncate student

19.查询

select*from student
select studentid,name from student
select studentid as 学号,name as 姓名 from student as 学生表
select concat(‘姓名:’,name) as 新名字 from student
去重
select distinct studentid from student
select studentid,age+1 as 提分后 from student

20.where条件子句

select id,age from student where age>=95 and<=100
select id,age from student where age between 95 and 100
select id,age from student where age !=21

21.模糊查询

selectfrom student where name like ‘刘%’
select
from student where name like ‘刘_’
selectfrom student where name like ‘刘__’
select
from student where name like ‘%龙%’
selectfrom student where age in(13,14)
select
from student where address in(‘北京’)
selectfrom student where address like ‘%北京%’
select
from student where age is null selectfrom student where age=‘’
select
from student where age is not null

22.联表查询

select s.studentid,age,student from student s left join school sc where s.studentid=sc.studentid
1
select s.studentid,age,student from student s left join school sc on s.studentid=sc.studentid where

select s.studentid studentname,SubjectName,StudentResult from student s right join result r on s.studentid=r.studentid inner join subject sub on r.subjectno=sub.subjectno

23.自连接

SELECT s.name FROM student s INNER JOIN student a ON s.password=a.grateid

24.升降序

ASC DESC
select s.studentid,age,student from student s left join school sc on s.studentid=sc.studentid where age=11
order by age ASC/DESC
//limit 1,5

25.子查询

select StudentNo,StudentName from student where StudentNo= (
select StudentNo from result where StudentResult>=80
)

26.函数

select count(name)from student
select count(*)from student 忽略null
select count(1)from student null

select sum(name) from student
select AVG(name) from student
select max(name) from student
select min(name) from student
select SubjectName,AVG(StudentResult),MAX(StudentResult),min(StudentResult)from result r inner join subject sub on
r.Subjectno=sub.Subjectno
group by r.subjectno
Having AVG(StudentResult)>=80

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值