mysql的添加语句怎么写_⼀句简单的MySql插⼊语句怎么写?insert into 表名 values(default,"名字","2011-04-15 12:22:25"); //default可以换成null
------------------------------
insert into 表名 (name,datetime) values("名字","2011-04-15 12:22:25");
使⽤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 ,
简单的mysql语句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');分类: mysql
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论