MySql的基本知识

来源:互联网 发布:南风知我意微盘 编辑:程序博客网 时间:2024/05/23 18:53
0.更改命令窗口乱码的问题    set names gbk;
--------------------------------------------------------------------------------------------------------------
1.选择使用的数据库        use test;
--------------------------------------------------------------------------------------------------------------
2.显示所有的数据表        show tables;
--------------------------------------------------------------------------------------------------------------
2.显示所有的数据库        show databases;
--------------------------------------------------------------------------------------------------------------
3.显示数据表的结构        desc Student;
--------------------------------------------------------------------------------------------------------------
3.将文件的内容导入到数据表    load data local infile '/path/pet.txt' into table pet;    //当用NULL时文件写入\N
--------------------------------------------------------------------------------------------------------------
4.查看当前时间            select curdate();                    // select sysdate();
--------------------------------------------------------------------------------------------------------------
5.修改表的名字            rename table student to students;
--------------------------------------------------------------------------------------------------------------
6.添加表的字段            alter table student add age int not null;
--------------------------------------------------------------------------------------------------------------
7.修改表的字段            alter table student modify age int;
--------------------------------------------------------------------------------------------------------------
8.对表添加主键            alter table student add constraint primary key(id);
--------------------------------------------------------------------------------------------------------------
9.对表添加外键            alter table student add constraint foreign key(id) references test(id);
--------------------------------------------------------------------------------------------------------------
10.查看相应的帮助语法        ? contents;        //查看其它的  ? 查询内容;
--------------------------------------------------------------------------------------------------------------
11.查询表格中的记录.从2条开始.显示3条记录        select * from student limit 2,3;    //默认从0
--------------------------------------------------------------------------------------------------------------
12.对字段的自增长        auto_increment;
--------------------------------------------------------------------------------------------------------------
13.设置控制台的结束符号        delimiter ;    //
--------------------------------------------------------------------------------------------------------------
14.创建存储过程            create procedure proc_name(out count int)        //可以有参数也可以没有参数
                begin
                    select count(*) into count from student;
                end;
                ---调用存储过程        call proc_name(@num);
                ---查看存储过程的值    select @num
--------------------------------------------------------------------------------------------------------------
15.创建函数            create function function_name(str varchar(11)) returns varchar(11)    //可以没有参数.但是得有返回值
                begin
                    //中间可以插入相应的sql 语句 select name into @name from student where id=1;
                    return upper(str);   //return @name;
                end;
                --调用方法        select function_name("hello word");
--------------------------------------------------------------------------------------------------------------
16.局部变量的声明        declare name varchar(10);            必须声明在局部代码块
--------------------------------------------------------------------------------------------------------------
17.全局变量的声明        @name
--------------------------------------------------------------------------------------------------------------
18.局部变量的赋值        set name="张三";
--------------------------------------------------------------------------------------------------------------
19.日期格式的转换    DATE_FORMA T(date, format) 根据格式串format 格式化日期或日期和时间值date,返回结果串。
            可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值,以便得到所希望的格式。根据format字符串格式化date值:
            %S, %s 两位数字形式的秒( 00,01, . . ., 59)
            %i 两位数字形式的分( 00,01, . . ., 59)
            %H 两位数字形式的小时,24 小时(00,01, . . ., 23)
            %h, %I 两位数字形式的小时,12 小时(01,02, . . ., 12)
            %k 数字形式的小时,24 小时(0,1, . . ., 23)
            %l 数字形式的小时,12 小时(1, 2, . . ., 12)
            %T 24 小时的时间形式(h h : m m : s s)
            %r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
            %p AM 或P M
            %W 一周中每一天的名称( S u n d a y, Monday, . . ., Saturday)
            %a 一周中每一天名称的缩写( Sun, Mon, . . ., Sat)
            %d 两位数字表示月中的天数( 00, 01, . . ., 31)
            %e 数字形式表示月中的天数( 1, 2, . . ., 31)
            %D 英文后缀表示月中的天数( 1st, 2nd, 3rd, . . .)
            %w 以数字形式表示周中的天数( 0 = S u n d a y, 1=Monday, . . ., 6=Saturday)
            %j 以三位数字表示年中的天数( 001, 002, . . ., 366)
            %U 周(0, 1, 52),其中Sunday 为周中的第一天
            %u 周(0, 1, 52),其中Monday 为周中的第一天
            %M 月名(J a n u a r y, February, . . ., December)
            %b 缩写的月名( J a n u a r y, February, . . ., December)
            %m 两位数字表示的月份( 01, 02, . . ., 12)
            %c 数字表示的月份( 1, 2, . . ., 12)
            %Y 四位数字表示的年份
            %y 两位数字表示的年份
            %% 直接值“%”
            select date_format(日期字段,’%Y-%m-%d’) as ‘日期’ from test
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
0 0