190906mysql常用语法

1.创删数据库;

  create database mydb;

  复杂一点可以设置字符集和校对规则:create database mydb character set gbk collate gbk_chinese_ci;

  drop database mydb;

  使用数据库 use mydb;

2. 创删表格

  创建create table student(id int not null);

  删除 drop table student;

  增加主键 alter table student change id id int primary key;

  删除主键 alter table student drop primary key;

  如果主键时自增长,先要去除 alter table student modify id int not null;

扫描二维码关注公众号,回复: 7191278 查看本文章

  增加外键 alter table student add foreign key employee(b_id) references department(b_id);

  删除外键(先查看外键约束(有的没有约束))show create table;

              alter table score drop foreign key score_ibfk_1;

3. 备份与还原

  在cmd命令中备份, mysqldump -uroot -p mydb> d:/mydb.sql

        还原, mysqldump -uroot -p mydb < d:/mydb,sql

4. 增删改查

  增 alter table student add address varchar(127) not null after sex;  #增加一个字段,在sex字段后添加address字段

   insert into student values (11, '李磊', '男',23)  #插入一条信息

  删 alter table student drop address;  #删一个字段

    delete from student where age>40;  #按where条件删除整条信息

  改 alter table student modify id int not null;  #修改字段

    alter table student change id id b_id int;  #可以修改字段名称

    update student set id=4 where id=1;

    update staff set name='张三', id=5 where name='李四';

  查  select * from student where id =5 and sex='男' and age not between 20 and 30;  #按条件查询,where,between...and

     select b_name, b_id, count(y_name) from (select department.b_name, staff.* from department, staff where department.b_id=staff.b_id) as temp group by b_id order by count(y_name) desc, b_id asc;

    

猜你喜欢

转载自www.cnblogs.com/jakye/p/11478289.html