SQL
Structured Query Language:结构化查询语言,用于定义操作关系型数据库的规则。
常用SQL命令
-
SELECT - 从数据库中提取数据
- SELECT * FROM table_name;
- SELECT column_name,column_name FROM table_name;
-
UPDATE - 更新数据库中的数据
- UPDATE table_name SET column1=value1,column2=value2,… WHERE some_column=some_value;
-
DELETE - 从数据库中删除数据
- DELETE FROM table_name WHERE some_column=some_value;
-
INSERT INTO - 向数据库中插入新数据
- INSERT INTO table_name (column1,column2,column3,…) VALUES (value1,value2,value3,…);
-
CREATE DATABASE - 创建新数据库
- CREATE DATABASE dbname;
- CREATE DATABASE IF NOT EXISTS dbname CHARACTER SET 字符集;
-
ALTER DATABASE - 修改数据库
- ALTER DATABASE dbnameDEFAULT CHARACTER SET 字符集;
-
DROP DATABASE 删除数据库
- DROP DATABASE dbname
-
CREATE TABLE - 创建新表
-
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
…
); -
CREATE TABLE table_name
(
column_name1 data_type(size) AUTO_INCREMENT,
column_name2 data_type(size),
column_name3 data_type(size),
PRIMARY KEY (column_name1 ),
…
);
-
-
ALTER TABLE - 变更(改变)数据库表
- ALTER TABLE table_name ADD column_name datatype
- ALTER TABLE table_name DROP COLUMN column_name
-
DROP TABLE - 删除表
- DELETE FROM table_name WHERE some_column=some_value;
-
CREATE INDEX - 创建索引(搜索键)
- CREATE INDEX index_name ON table_name (column_name)
-
DROP INDEX - 删除索引
- DROP INDEX index_name ON table_name
-
SHOW DATABASE 查看所有数据库
- SHOW DATABASES ;
-
USE DATABASE 使用指定数据库
- USE dbname;
-
SHOW TABLE 展示当前库下所有表
- SHOW TABLES;
-
DESC TABLE 展示表结构
- DESC table_name