MySQL的基本语句


ALTER TABLE USER DROP hahal;
ALTER TABLE USER ADD UNIQUE Ha haha;
ALTER TABLE `user` ADD haha INT;
ALTER TABLE USER ADD CONSTRAINT FOREIGN KEY(haha) REFERENCES user_info(id);
ALTER TABLE teachar RENAME TO  teacher; //修改表名
ALTER TABLE teacher CHANGE _name NAME VARCHAR(255); //修改字段名

CREATE TABLE test(
    id INT NOT NULL,
    age INT,
    NAME VARCHAR(255)
    
);

ALTER TABLE test ADD CONSTRAINT PRIMARY KEY (id);
ALTER TABLE test DROP PRIMARY KEY;
ALTER TABLE test ALTER age SET DEFAULT 18;
ALTER TABLE test MODIFY id INT AUTO_INCREMENT;

INSERT test VALUES(NULL,20,'bdl');
INSERT test (id,NAME) VALUES (NULL,'bb');
INSERT test VALUES(NULL,21,'yf'),(NULL,9,'wo');

UPDATE test SET age=10 WHERE NAME = 'wo';

SELECT NAME AS '名字' FROM test WHERE id = 4;
SELECT DISTINCT NAME FROM test;
SELECT * FROM test LIMIT 3; //前三条记录
SELECT * FROM test LIMIT 1,2; //第二条记录开始,一共两条记录
SELECT * FROM test WHERE age BETWEEN 15 AND 20;
SELECT * FROM test WHERE name LIKE 'b%';
SELECT DISTINCT NAME FROM test ORDER BY age DESC;

//聚合函数;
SELECT COUNT(id) FROM test;
 
ALTER TABLE test ADD score INT(11) AFTER NAME;
UPDATE test SET score=80 WHERE id=1;
UPDATE test SET score=90 WHERE id=2;
UPDATE test SET score=70 WHERE id=3;
UPDATE test SET score=85 WHERE id=4;
UPDATE test SET score=79 WHERE id=5;
UPDATE test SET score=99 WHERE id=6;

SELECT  SUM(score) AS '总分',
    AVG(score) AS '平均分',
    MAX(score) AS '最高分'
FROM test;


//内连接,外连接;
CREATE TABLE test2(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    test_id INT NOT NULL,
    weight INT 
);
INSERT test2 VALUES(NULL,1,60),(NULL,2,65),(NULL,3,64),(NULL,5,70),(NULL,8,55);

//内连接;
SELECT test.*,test2.* FROM test INNER JOIN test2 ON test.id=test2.test_id;
//左连接;
SELECT test.*,test2.* FROM test LEFT JOIN test2 ON test.id=test2.test_id;
//右连接;
SELECT test.*,test2.* FROM test RIGHT JOIN test2 ON test.id=test2.test_id;


//日期时间函数;
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT SYSDATE();
SELECT DAYNAME('2015-09-22');
SELECT DAYOFWEEK('2018-06-05');
SELECT WEEKDAY('2018-06-05');
SELECT DAYOFWEEK('2018,06,05');
SELECT HOUR(20080808222301);


//数学函数;
SELECT ABS(-1000) AS '绝对值';
SELECT SIGN(-10);
SELECT CEIL(2.5);
SELECT FLOOR(2.5);
SELECT ROUND(3.5);
SELECT ROUND(3.525,2);
SELECT TRUNCATE(3.526,2);
SELECT MOD(3,2);
SELECT POW(2,3);
SELECT EXP(2);
SELECT SQRT(25);
SELECT SIN(RADIANS(30));

//字符串函数;
SELECT CHAR_LENGTH('abdafgfdsare');
SELECT LENGTH('abc123');
SELECT CONCAT('ab','cd','ef');
SELECT CONCAT('ab','cd',NULL);
SELECT CONCAT_WS('-','ab','cd','ef');
SELECT CONCAT_WS('-','ab','cd',NULL);
SELECT INSERT('abcefgh',3,2,'==');
SELECT REPLACE('ABCDEABHGAB','AB','%%%');


 

你可能感兴趣的:(MySQL)