ALTER TABLE基本语法来添加新的列到现有表如下:
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE用于DROP COLUMN在现有表的基本语法如下:
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE的基本语法更改列的数据类型(DATA TYPE)在表中如下:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
ALTER TABLE添加NOT NULL约束到一个表的列的基本语法如下:
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
ALTER TABLE添加唯一约束到表的基本语法如下:
ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);
ALTER TABLE添加CHECK约束到表的基本语法如下:
ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);
ALTER TABLE添加PRIMARY KEY约束到表的基本语法如下:
ALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);
ALTER TABLE从表中删除约束的基本语法如下:
ALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint;
如果使用MySQL,参考代码如下:
ALTER TABLE table_name DROP INDEX MyUniqueConstraint;
ALTER TABLE从表中删除主键约束的基本语法如下:
ALTER TABLE table_name DROP CONSTRAINT MyPrimaryKey;
如果使用MySQL,代码如下:
ALTER TABLE table_name DROP PRIMARY KEY;
2.SQL truncate table 清空表
语法
truncate table的基本语法如下:
TRUNCATE TABLE table_name;
3.HAVING子句
允许您指定过滤器的条件,分组结果会出现在最终结果。
WHERE子句所选列的条件,而由GROUP BY子句条件创建分组的HAVING子句。
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
考虑CUSTOMERS表具有以下记录:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
下面是示例,这将显示记录age大于或等于2的数据量:
SQL > SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;
这将产生以下结果:
+----+--------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
+----+--------+-----+---------+---------+