SQL最常用的语句

Linux环境下连接Mysql数据库:

mysql -h 140.143.88.144 -u root -p 123456    //连接数据库

新建表:

create table student(S_id int not null primary key, S_name char(20) not null);    //新建数据表student

删除表:

drop table student;    //删除student表

原有表基础上新加字段:

alter table student add(S_score int not null);    //新家字段S_score

原有表基础上删除字段:

alter table student drop column S_score;    //删除字段S_score

数据表插入数据项:

insert into student values(001,'张三'),(002,'李四');    //插入student表两条数据,如果插入数据不全且该字段不能为空,则插入失败,如果可以为空,则缺省字段为null

查询语句(1):

select * from student;    //单表全字段查询

查询语句(2):

select S_id from student;    //单表单字段查询

查询语句(3):

select student.S_id,student.S_score,teacher.T_name from student,teacher;    //多表查询:使用  表名.字段名  表示

条件查询语句(4):

select * from student where S_socre>90;    //使用where条件语句查询

条件嵌套查询语句(5):

select S_name from student where T_id=(select T_id from teacher where name='王老师');    //使用()里面可以嵌套查询语句进行条件查询

查询修饰符order by:

select * from student order by S_name;    //缺省为升序排序
select * from student order by S_name desc;    //desc为降序排序
select * from student order by S_name asc;    //asc为升序排序,和缺省一样

删除数据项:

delete from student where S_id=004;    //删除数据

更新数据项:

update student set S_score=90 where S_id=003;    //条件更新数据项

关于左右连接查询:明日补充

转载:https://www.cnblogs.com/wkrbky/p/5882975.html

    原文作者:辉东
    原文地址: https://www.cnblogs.com/MasterBai2018/p/9396161.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞