Mysql常用sql语句

1、建表语句 

--建表语句
CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    age INT
);

2、插入语句

--插入测试数据
insert into test_2 values(1,'zhangsan');

3、查询语句

--查询语句
MySQL [test_drds_2]> select * from test_2;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
|    2 | lisi     |
+------+----------+

4、ALTER TABLE操作

--删除列
alter table table_name drop col_name;

--增加列(单列)
alter table table_name add col_name col_type comment 'xxx';

--增加列(多列)
alter table table_name add col_name col_type comment 'xxx', add col_name col_type(col_length) comment 'xxx';

--增加表字段并指明字段放置为第一列
alter table table_name add col_name col_type COMMENT 'sss' FIRST;

--增加表字段并指明字段放置为特定列后面
alter table table_name add col_name col_type after col_name_1;

--使用MODIFY修改字段类型
alter table table_name modify column col_name col_type;

--使用CHANGE修改字段类型
alter table table_name change col_name col_name col_type;

--使用CHANGE修改字段名称
alter table table_name change old_col_name new_col_name col_type;

--修改列类型、长度
alter table table_name change old_col_name new_col_name new_col_type;

5、查询数据库中的存储过程和函数

--查询数据库中的存储过程和函数
select `name` from mysql.proc where db = 'xx' and `type` = 'PROCEDURE' //存储过程
select `name` from mysql.proc where db = 'xx' and `type` = 'FUNCTION' //函数
show procedure status; //存储过程
show function status; //函数

--查看存储过程或函数的创建代码
show create procedure proc_name;
show create function func_name;

--查看视图
SELECT * from information_schema.VIEWS //视图
SELECT * from information_schema.TABLES //表

--查看触发器
SHOW TRIGGERS [FROM db_name] [LIKE expr]
SELECT * FROM triggers T WHERE trigger_name=”mytrigger” \G

 6、修改和删除索引

--删除表tb_stu_info中的索引
 DROP INDEX height ON tb_stu_info;

DROP PRIMARY KEY:表示删除表中的主键。一个表只有一个主键,主键也是一个索引。
DROP INDEX index_name:表示删除名称为 index_name 的索引。
DROP FOREIGN KEY fk_symbol:表示删除外键。

 7、CURRENT_TIMESTAMP时间戳的使用详解

 

你可能感兴趣的:(mysql,sql)