SQL数据库常用命令
数据库操作:
创建库:create database 数据库名
删除库:drop database 数据库名
选择库:use 数据库名 Database changed
关闭安全模式:set sql_safe_updates = 0;
表操作:
创建表:CREATE TABLE table_name (column_name column_type);
删除表:DROP TABLE table_name;
查询表数据:SELECT*FROM table_name;查看整个表数据
SELECT comlun_name comlun_name from table_name;查看表指定模块数据
插入:insert into
语法:INSERT INTO table_name
VALUES (value1,value2,value3,...);
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
INSERT INTO table_name (field1, field2,...fieldN) VALUES (valueA1,valueA2,...valueAN),(valueB1,valueB2,...valueBN),(valueC1,valueC2,...valueCN)......;
AUTO_INCREMENT(自动增加) 属性
INSERT INTO Websites (name, url, alexa, country) VALUES ('百度','https://www.baidu.com/','4','CN');
删除delete
语法:DELETE FROM table_name
WHERE some_column=some_value;
DELETE FROM Websites WHERE name='Facebook' AND country='USA';
更新:update
语法:UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
UPDATE Websites SET alexa='5000', country='USA' WHERE name='菜鸟教程';
查询:where
语法:SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
SELECT * FROM Websites WHERE country='CN';
跨表联查:JOIN ON
INNER JOIN(内连接)
LEFT JOIN(左连接)
RIGHT JOIN(右连接)
CROSS JOIN(交叉连接)
SELECT * FROM table1 JOIN table2 ON table1.column = table2.column;