mysql 操作一

1. net start mysql
 2. 进入MySQL5.0目录,mysql -uroot -proot
 3.创建数据库, create database test_a;
 4.显示数据库,show databases;
 5.使用某个数据库,use test_a ;
 6,创建表   create table stu_info (
                    stuID  VARCHAR(15) primary key,
                     stuNAME VARCHAR(20) not null,
                     stuAGE  INT not null,
                     stuSEX VARCHAR(2) not null,
                     stuADD VARCHAR(20)
                      );
7.显示表  show tables;
8.外键    FOREIGN KEY(stuID) REFERENCES t_result(stuID) ON DELETE CASCADE
9.检查约束 CHECK(stuAGE between 10 and 18)
10.创建索引  create index i_stuid on stu_info(stuID)
11.删除索引  drop index i_stuid
12.表中添加一列  alter table stu_info add stuHEIGHT int not null
13.增加主键约束  alter table stu_info add primary key(stuID)
14.增加外键约束   alter table stu_info add foreign key(stuID) references t_restult(stuID)
15.增加索引   alter table stu_info add index i_stuid(stuID)
16.修改某一列 alter table stu_info modify stu_SEX VARCHAR(3)
17.删除某一列  alter table stu_info drop stu_SEX
18.删除主键约束 alter table stu_info drop primary key
19.删除表  drop table stu_info    drop table stu_info cascade constraints
20.显示表的结构 show columns from stu_info
21.查询表的信息
              select * from stu_info    查询所有信息
              select stuID,stuNAME from stu_info 查询目标列
              select distinct stuID from stu_info 去掉重复行
              select stuID as 编号,stuNAME as 姓名 from stu_info 别名查询 (如果别名中保护空格特殊字符,需要用单引号,‘编  号’)
              select teacID as 老师编号,salary*12 as 年收入 from teac_info  带有运算符操作
              select stuID,stuNAME||stuAGE from stu_info 连接符操作
22.带条件查询
              select * from stu_info  where stuID>=4
               select * from stu_info  where stuNAME !='张三'
               select * from stu_info  where stuAGE between 15 and 18
               select * from t_dept where deptNAME in('计算机系','法学系')      在这两个中
                select * from t_dept where deptNAME not in('计算机系','法学系')   不在这两个中
               select * from t_dept where deptNAME='计算机系' or deptNAME='法学系'
              select * from stu_info where stuID>=4 and stuNAME !='张三'   多条件查询  都满足
               select * from stu_info where stuID>=4 or stuNAME !='张三'     满足一个即可
               select * from stu_info where stuID is null    空值查询
               select * from stu_info where stuID like '1__3'  相似查询,一个下划线匹配一个字符 ,前面有两个下划线  1003,1013 等等
               select * from stu_info where stuID not like '1__3'   不相似查询
               select * from stu_info where stuNAME like '王%'  匹配0个或者多个字符,王,王一,王二小 等等
               select * from stu_info where stuID like '%&_%' escape '&'  转义字符&可以任意定义,&后的转义,结果 13_1,5_2

posted @ 2017-04-25 16:42  1916  阅读(40)  评论(0)    收藏  举报