常用MySQL基本操作

数据库

1、 显示数据库:show databases;

2、 建数据库:create database  [if not exists] 数据库名;

3、 删除数据库库: drop database [if exists] 库名; 、

4、使用数据库:use 数据库名

1、建数据表: create table [if not exists] 表名 (字段名1   类型。。。。。。。。);      

      ex: create table student (
                        sno varchar(7) not null auto_increment,  字段不允许为空
                        sname varchar (20 )not null,
                        ssex char (1) default ‘t’,
                        sbirthday date,
                        sdepa char (20),
                        primary key (sno)       表的主键
          );

2、显示表: show tables;

3、删除表: drop table   [if exists] 表名;

4、修改表属性的类型:alter table 表名;

增加列StelAlter table Student ADD Stel Char(12)

删除列Stel:Alter Table Student DROP COLUMN Stel

5、显示表结构: desc 表名;

记录

1、增加记录:Insert into 表(字段表列表) values(字段值);

     ex: insert into student(sno, sname) 

               values(''123, 'liqiong');

2、删除记录:delete from 表 where 条件;

3、修改记录:update 表 set  字段=值,  字段=值 where 条件;

4、查询记录: 

 

        select  列名或列表达式序列

        from 基本表名和(或)视图序列

        [where 条件表达式]

        [group by  列名序列]

               [having  组条件表达式]

        [order by 列名[asc | desc]]

分组筛选,查询Student表中每个系有三个以上的学生的所在系。

Select  department  From  Student  Group  BY department  Having  COUNT(*) >= 3

去掉重复的行使用关键字:distinct,举例:

 

Select  distinct  department  From  student

查看分页

limit m,n:第m+1条记录开始,取n条记录(不包括第m条记录),

 ex:select * from dept order by deptno desc limit 3,2;

排序

order by:order by 字段 desc/asc

索引

 

1、在表Student中建立按年龄Sage升序建立索引

建立索引:Create  INDEX  S_INDEX  ON  Student(Sage)

2、删除索引

DROP  INDEX  Student  S_INDEX

 


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值