mysql 数据库使用

来源:互联网 发布:自动打铃软件 编辑:程序博客网 时间:2024/06/05 06:26

1. 常用命令

 

2. 创建表

create table test(    test_id int, test_price decimal);

或者

create table test2 as select * from test

修改表

alter table test2  //添加add(    test_a varchar(255) defaule 'aaa',    test_b varchar(255),);
alter table test2   //修改modify test_a varchar(255) default 'bbb';
alter table test2 drop test_a;  //删除
alter table test2 rename to test3;  //重命名
alter table test2 change test_b bb int; //修改列名test_b --> bb 类型修改为int
drop table test2;  //删除表test2
truncate test2;   //删除表里全部数据,但是保留结构。

3. 约束

非空约束:

create table test(    test_id int not null,  //不能为空    test_age int null   //默认为空);

unique唯一性约束,唯一性是指这列的值在这个表中要唯一。

create table test(    test_id int unique,  //直接加后面修饰    test_name int,    test_age int,    unique (test_name),   //unique()    constraint test_uk unique(test_age)   //[constraint 约束名] 约束定义);
alter table test add unique(test_id);  //修改约束性alter table test modify test_name int unique;alter table test drop index test_uk;   //删除约束性  drop index + 约束名

Primary Key 主键约束  == 非空约束 + 唯一约束, 每个表中只有一个主键,但这个主键可以由多个数列组成。

主键:指的是,唯一标识这张表的字段,例如,书号是图书表的主键
外间:指的是 可以跟其他表建立关系的字段,例如 图书表的书名 可以在 借阅信息表中出现,这样图书表和借阅信息表之间建立了关系。

create table test  //这仅是例子,正常一个table里只会有一个主键(    test_id int primary key,  //可以在primary key 前加 auto_increment 增加自增长功能。    test_age int,    test_name varchar(255),    primary key(test_age),    constraint test_pk primary key(test_name) // 可建组合约束);

修改主键约束和唯一性约束一样,只需要改为 primary key 即可。

Foreign Key外键约束,构建一个表的两个字段 或 两个表的两个字段之间的参照关系。


MySQL支持使用列级约束但是不会生效,只是为了保持兼容性,需要用表级约束语法。



4. 索引

//create index index_name on table_name (column ....);create index student_index on student_table2 (student_id); //建引索
drop index student_index on student_table2;    //删引索

5. 视图

create or replace view view_testas select teacher_name, teacher_id from teacher_tablewith check option;  //不允许修改视图数据 
drop view view_test;  //删除视图


6. DML 操作数据表的值

insert 插入

insert into teacher_table(teacher_name) values('kevin'); //插入一个insert into teacher_table values(null, 'xiang');  //插入多个值
insert  into test2(age) select test_id from test3; //用从句一次插入多个值

update 更新

update test2 set name='kevin';update test2 set name='xiang' where age>=18;  //接where从句,相当于if,满足条件才update

delete from 删除表里的数据

delete from test2;  //删除所有记录delete from test2 where age>18; //按条件删除


7. select语句和SQL函数

select concat(teacher_name, 'XX') from teacher_table;  //concat 字符连接
select teacher_id as My_ID from teacher_table;  //teacher_id as My_ID 别名
select distinct * from test3; //不显示重复行

使用where语句时支持的比较符。

select * from test3 where 21 between test_id and test_age; //选出test_id小于21和test_age大于21的列
select * from test3 where test_id in(12,18);
select * from test3 where 20 in(test_id,test_age); //选出test_id test_age == 20的列
select * from test3 where name like  'k%'; //like字符匹配, 通配符(_) 一个字符, (%)任意字符//转义符为'\',反斜杠的转义 必须用关键字 escape 显示转义

SQL还提供 and  or not 三个逻辑运算符

select * from test3 where (test_id=12) and name like 'kevin' ;

order by 排序

select * from test3 order by test_id;  //按test_id列的升序排列select * from test3 order by test_id desc;  //desc 降序排列


8. 常用函数





Case 控制流语句

select case name when 'kevin' then 1 when 'xiang' then 2 else 'no' endfrom test3; 

常用组函数



group by 分组 详解:http://blog.csdn.net/qianquanyiyan/article/details/8014406

select test_id,max(test_age) from test3 group by test_id;







 



 

原创粉丝点击