你的位置:首页 > 数据库

[数据库]MYSQL常用简单语句


使用SQL语法大写,增加可读性(小写部分就是自己数据库写的表/字段喽,具体你懂得...)。

创建数据库:CREATE DATABASE mysql_db;
删除数据库:DROP DATABASE mysql_db;
查看数据库:SHOW DATABASES;
使用数据库:USE mysql_db;
查看数据库中的表:SHOW TABLES;

创建表:
CREATE TABLE user(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) UNIQUE NOT NULL,
sex ENUM('1','2','3') NOT NULL DEFAULT 3
);
查看表结构:①SELECT COLUMN FROM user;
      ②DESC user;


查看表创建语句(可查看各种自动生成的名字):SHOW CREATE TABLE user;
插入表记录:
①INSERT INTO user(id,username,sex) VALUES(1,'Tom',1);
②INSERT user(username) VALUES ('John');
查找表:SELECT * FROM user;
删除表中的数据(保留表结构):TRUNCATE TABLE user;
将查询结果写入新表:
INSERT [INTO] privinces(sex) SELECT sex FROM user GROUP BY sex;

(若表中有数据,请不要随意更改表名、列名)
修改表名:
①ALTER TABLE user RENAME [AS|TO] users;
②RENAME TABLE user TO users;
添加单列:
ALTER TABLE user
 ADD [COLUMN] age SMALLINT NOT NULL UNSIGNED DEFUALT 18
 [FIRST | AFTER sex];
删除列:ALTER TABLE user DROP sex[,DROP age];
修改列名称和定义:
ALTER TABLE user
 CHANGE [COLUMN] age a_ge TINYINT NOT NULL UNSIGNED AFTER id;


单表更新:UPDATE user SET age = age + 5,sex = 1 [WHERE id = 2];
单表删除:DELETE FROM user [WHERE id =3];
单(多)表连接:
SELECT p.userid,p.username FROM user AS p LEFT JOIN user AS s
 ON p.userid = s.age;
单(多)表删除:
DELETE * FROM user [AS] u1 LEFT JOIN (
 SELECT p.userid FROM user AS p LEFT JOIN user AS s
 ON p.userid =s.age GROUP BY p.userid HAVING COUNT(s.age)>1 ) [AS] u2
 ON u1.userid = u2.age
WHERE u1.userid > u2.userid;

查找记录:
SELETE select_expr [,select_expr]
 [
 FROM table_references
 [WHERE where_condition]
 [GROUP BY {col_name|position}[ASC|DESC],...] //查询结果分组
 [HAVING where_condition] //设置分组条件
 [ORDER BY {col_name|expr|position}[ASC|DESC]] //对结果排序
 LIMIT {[offset,]row_count|row_count OFFSET offset}]//限制记录数量
 ]


记录为2、3行:SELECT * FROM user LIMIT 1,2;


别名使用(在项目中一般都使用别名)AS alias_name:
  SELECT id AS uid FROM user AS u ;


子查询:
SELECT AVG(age) FROM user WHERE userid =
 [ALL|SOME|ANY|[NOT] IN|[NOT] EXISTS](SELECT uid FROM privinces WHERE pname = '河南');


外键约束:
(父表为user表,必须先在父表中插入数据,才能在子表中插入数据)
CASEADE:从父表中删除或更新行 且 自动级联删除或更新子表中匹配的行;
SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL;
RESTRICT:拒绝对父表的更新或删除操作;
CREATE TABLE privinces(
  pid SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  pname VARCHAR(20) NOT NULL ,
  uid SMALLINT,
  FOREIGN KEY (uid) REFERENCES user (id)    //privinces的字段uid与user表的字段id外键约束
ON DELETE CASEADE   //删除时 级联删除
);


增加主键、唯一、外键约束:
ALTER TABLE privinces
  ADD [CONSTRAINT uq_pname] UNIQUE(pname);
添加/删除默认约束:
ALTER TABLE privinces
  ALTER pname {SET DEFAULT literal | DROP DEFAULT}; 
删除主键约束:
ALTER TABLE privinces DROP PRIMARY KEY;
删除唯一(索引)约束:
     (查看约束名字key_name: SHOW INDEXS FROM privinces;)
ALTER TABLE privinces DROP {INDEX | KEY} 约束名;
删除外键约束:
     (查看约束名字:SHOW CREATE TABLE privinces;)
ALTER TABLE privinces DROP FOREIGN KEY 约束名;


多表更新:
UPDATE 表的参照关系 SET col_name = {[expr | value]};


表的参照关系:
tab1_reference {[INNER|CROSS] JOIN |{LEFT|RIGHT}[OUTER] JOIN} tab2_refenence ON conditional_expr;
内连接:INNER JOIN
左外连接:LEFT JOIN
右外连接:ROGHT JOIN
UPDATE user INNER JOIN privinces ON user_privince = pname
  SET user_privince = pid;//(更新user内连接privince,条件是 user_privince.=pname,设置user_privince=pid)

创建数据表的同时将查询结果写入数据表:
CREATE TABLE table_name
  [create_definition, ...]   //创建语句的定义
  select_statement;   //要插入查询结果的 查询语句


字符函数:
CONCAT()字符连接;CONCAT('I','LOVE','YOU');
CONCAT_WS()使用指定的分隔符进行字符连接;
CONCAT_WS('|','A','B');第一个为分割符
FORMAT();数字格式化;
FORMAT(99999.99,1);将数字格式化,并保留1位小数
LOWER();转换成小写字符
UPPER();转换成大写字符
LEFT();获取左侧字符
RIGHT();获取右侧字符
LENGTH();获取字符长度;
LTRIN();删除前导字符;
RTRIM();删除后续字符
TRIM();删除前导、后续字符;
SUBSTRING();字符串的截取;
SUBSTRING('MYSQL',1,2);从第一位中截取2位;(mysql不允许为负值)
[NOT] LIKE;模式匹配
SELECT * FROM user WHERE username LIKE '%1%%' ESCAPE '1';
REPLACE();替换字符串中的字符
REPLACE('M??Y??SQL','?','*');将M??Y??SQL中的?替换成*;


数值运算符:
CEIL();进一去整;
DIV;整数除法;
FLOOR();舍一去整;
MOD;取余数(模);
POWER();幂运算;
ROUND();四舍五入;ROUND(3.562,1);取一位小数
TRUNCATE();数字截取;TRUNCATE(125.89,1);小数后截取一位;


日期时间:
NOW();当前时间;
CURDATE();当前日期;
CURTIME();当前时间;
DATE_ADD();日期变化;
DATE_ADD('2015-6-23',INTERVAL 365 DAY);
DATEDIFF();俩日期之间的差值;
DATE_FORMAT();进行日期格式化;
SELECT DATE_FORMAT('2015-6-26','%m/%d/%Y');