常用sql语句

来源:互联网 发布:淘宝会员号 编辑:程序博客网 时间:2024/06/05 15:15

表操作

CREATE TABLE test (    test_a INT NOT NULL AUTO_INCREMENT, --非空自增    test_b VARCHAR (255),    test_c INT,    PRIMARY KEY (test_a), -- 主键    KEY `index_a` (`test_a`, `test_b`) USING BTREE --索引);DROP TABLE test;

视图操作

在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表。
视图总是显示最近的数据。每当用户查询视图时,数据库引擎通过使用 SQL 语句来重建数据。

-- 创建操作create view test1 ASSELECT * from  student1-- 更新操作create or replace view test1 ASSELECT * from  student-- 删除操作drop view test1

索引操作

-- 创建索引CREATE INDEX index2 ON student1 (stu_id, class_id);ALTER TABLE student1 ADD index index3(stu_id);-- 删除索引ALTER TABLE student1 DROP INDEX index2

字段操作

-- 增加字段ALTER TABLE student1 add COLUMN test_a int not null-- 删除字段ALTER TABLE student1 DROP COLUMN test_a-- 修改字段类型ALTER TABLE student1 CHANGE test_a[old] test_a[new] VARCHAR(255)

外键操作

-- 创建外键alter table a add CONSTRAINT myforignkey FOREIGN key (subject) REFERENCES b (subject)-- 删除外键alter table a drop FOREIGN key myforignkey
0 1