重温基本的sql

1、创建数据库

create database users

  

2、删除数据库
 
drop database users

  

3、use命令可以让我们来使用数据库。

use world;

 

4、 create table 创建表

 

create table命令格式:create table <表名> (<字段名1> <类型1> [,..<字段名n> <类型n>]);

create table user(
     id int(4) not null primary key auto_increment,
     name char(20) not null,
     sex int(4) not null default '0',
     degree double(16,2)
);

 

5、drop table

drop table命令格式:drop table <表名>;

drop table user;

  

6、insert into命令用于向表中插入数据

insert into user (id, name, age, sex, degree) values ('4', 'tt', '20', '1', '3.0');

insert into user values ('5', 'bb', '30', '1', '5.0');

insert into user values ('6', 'cc', '30', '2', '1.0'), ('7', 'dd', '30', '1', '15.0');

  

7. delete from命令用于删除表中的数据

delete from userwhere id=1;

  

8、select from命令用来查询表中的数据

所有:select * from user;

排序:select * from user order by id limit 0,2;

模糊:select * from user where name like '%t%';

总数:select count(*) as totalCount from user;

求和:select sum(salary) as salarys from user;

平均:select avg(salary) as avgvalue from user;

最大:select max(salary) as maxval from user;

最小:select min(salary) as minval from user;

  

 

9、update set命令用来修改表中的数据。

 

update set命令格式:update 表名 set 字段=新值,… where 条件;

update user set sex=2 where id=2

  

10、alter add命令用来增加表的字段。

1) 加索引
   mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);

alter table user add index emp_name(name);



2) 加主关键字的索引
    mysql> alter table 表名 add primary key (字段名);

例子: mysql> alter table employee add primary key(id);

3) 加唯一限制条件的索引
   mysql> alter table 表名 add unique 索引名 (字段名);

alter table user add primary key(id);



4) 删除某个索引
   mysql> alter table 表名 drop index 索引名;

alter table user drop index emp_name;



5) 增加字段
    mysql> ALTER TABLE 表名 ADD 字段名 字段类型;

alter table user add salary double(16,2);



6) 修改原字段名称及类型
    mysql> ALTER TABLE 表名 CHANGE old_field_name  new_field_name  field_type;

alter table user change sex sex1 int(4);



7) 删除字段
    mysql> MySQL ALTER TABLE 表名 DROP field_name;

alter table user drop salary;

  

posted @ 2016-07-01 17:13  三宝123  阅读(164)  评论(0编辑  收藏  举报