安装MySQL
从MSSQL导数据到MySQL:
用可视化工具SQLyog
可参考:
MySQL与MSSQL语法区别:
区别 | MSSQL | MySQL |
注释 | 单行注释:-- 段落注释:/*abc*/ | 单行注释://或者#或者-- (多一个空格) 多行注释:/*abc*/ |
存储过程 | CREATE PROC test1(@s BIGINT) BEGIN ... END | CREATE PROCEDURE test1(IN s BIGINT) BEGIN ... END; |
修改列 | alter table 表 alter column 列 类型 | ALTER TABLE 表 CHANGE 旧列名 新列名 类型; |
删除主键 | alter table 表 drop 主键名
| alter table 表 drop primary key; |
函数:
LOCATE
LOCATE(查找的字符串,所在的字符串,起始位置)-- 跟MSSQL的charindex函数一样
mysql> select LOCATE('ab','abcdabc',1);
+-------------------------------+
| LOCATE('ab','abcdabc',1) |
+-------------------------------+
| 1 |
+-------------------------------+
mysql> select LOCATE('ab','abcdabc',3);
+-------------------------------+
| LOCATE('ab','abcdabc',3) |
+-------------------------------+
| 5 |
+-------------------------------+
LIMIT
mysql> SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
//如果只给定一个参数,它表示返回最大的记录行数目,LIMIT n 等价于 LIMIT 0,n:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行
使用Navicat For MySQL
按F6(工具-命令列界面)进入命令列
或者打开查询窗口
常用SQL:
查看命令:
1. mysql> show databases;
2. mysql> show tables;
3. mysql> desc w_answer;
4. mysql> show create procedure cs_ro_getquestionlist;
5. mysql> show triggers
表操作:
创建表:
mysql> create table t1(id bigint,name nvarchar(200));
Query OK, 0 rows affected
修改表:
mysql> alter table t1 add addtime datetime;
mysql> alter table t1 drop column addtime;
mysql> alter table t1 change name score int;
查看表结构:
mysql> desc t1;
mysql> show columns from t1;
mysql> show create table t1;
删除表:
mysql> drop table t1;
索引操作:
新增索引:mysql> create index xID on t1(id);
mysql> alter table t1 add index xID (id);
删除索引:mysql> drop index xID on t1;
mysql> alter table t1 drop index xID;
查看索引:mysql> show index from t1;
重建索引:mysql> repair table t1 quick;
索引统计:右键-对象信息
主键操作:
添加主键:mysql> alter table t1 add primary key(id);
删除主键:mysql> alter table t1 drop primary key;
外键操作:
添加外键:
mysql> alter table testtag add constraint fk_test foreign key (ClassID) references testclass(ID);
删除外键:mysql> ALTER TABLE testtag DROP FOREIGN KEY `fk_test`;
触发器:
创建触发器:
mysql> create trigger tri_class_i before insert on w_questionclass for each row
-> begin
-> set new.px=6;
-> end;
查看触发器:mysql> show triggers;
删除触发器:mysql> drop trigger tri_class_i;
数据操作:
新增数据:
mysql> insert into t1(id,name)
-> select 123456,'ssssk';
mysql> insert into t1
-> values(111111,'asd');
mysql> insert into t1
-> select 111112,'abc' union all
-> select 111113,'abcd';
查询数据:
mysql> select * from t1 where id=123456;
mysql> SELECT ID FROM TEST ORDER BY ID LIMIT 0,3;//显示前三行
mysql> select distinct id from testclass;
mysql> select addy,count(1)as digit from test group by addy order by count(1);
更新数据:
mysql> update t1 set name='ssss' where id=123456;
删除数据:
mysql> delete from t1 where id=111111;
存储过程:
(1)输出参数:
mysql> create procedure test(out s int)
-> begin
-> select count(1)into s from tabletest;
-> end;
调用存储过程
mysql> call test(@a);
mysql> select @a;
(2)输入参数:
mysql> CREATE PROCEDURE test1(IN s BIGINT)
-> BEGIN
-> SELECT ID FROM tabletest WHERE ID=s;
-> END;
mysql> CALL test1(610815855784);
(3)表连接:
mysql> CREATE PROCEDURE test2(IN s BIGINT)
-> BEGIN
-> SELECT a.ID,b.ID,c.ID from testtag a left join testontag b on a.ID=b.tagid left join test c on b.questionid=c.ID WHERE c.ID=s;
-> END;
mysql> call test2(24);
注意:
MySQL没有WITH(NOLOCK)