Xem danh sách các cơ sở dữ liệu/bảng đã có
Dùng lệnh:
Chạy file SQL có sẵn
Để chạy file SQL có sẵn (.sql), dùng một trong 2 lệnh sau:
Tạo/Xóa cơ sở dữ liệu
Để tạo và dùng cơ sở dữ liệu, dùng lệnh sau:
Để xóa, dùng lênh:
Tạo bảng
Cú pháp tổng quát:
Trong đó, dataType(size) có thể là:
char(size), varchar(size), tinytext, text, mediumtext, longtext, enum
int, tinyint, mediumint, bigint, float, double
date, timestamp(size), datetime, time, year(size)
Ví dụ:
Xem cấu trúc của cơ sở dữ liệu/bảng
Dùng một trong các lệnh sau:
Thay đổi một bảng có trước
Đổi tên bảng:
Thêm một trường vào trong bảng:
Xóa trường ra khỏi bảng:
Thay đổi thuộc tính của trường:
Xóa bảng:
Các lệnh SQL cơ bản
Lệnh select
Lệnh insert
Lệnh update
Lệnh delete
Dùng lệnh:
mysql> show databases;
mysql> show tables;
Chạy file SQL có sẵn
Để chạy file SQL có sẵn (.sql), dùng một trong 2 lệnh sau:
Ví dụ:mysql> SOURCE salary.sql
mysql> \. salary.sql
salary.sql
CREATE TABLE salary
(
employeeID INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(100),
salary INT,
PRIMARY KEY(prodID)
);
mysql> \. salary.sql
Tạo/Xóa cơ sở dữ liệu
Để tạo và dùng cơ sở dữ liệu, dùng lệnh sau:
mysql> CREATE DATABASE salary;
mysql> USE salary;
Để xóa, dùng lênh:
mysql> DROP DATABASE salary;
Tạo bảng
Cú pháp tổng quát:
CREATE TABLE tableName
(
fieldName1 dataType(size) [NULL | NOT NULL]
fieldName2 dataType(size) [NULL | NOT NULL]
);
Trong đó, dataType(size) có thể là:
char(size), varchar(size), tinytext, text, mediumtext, longtext, enum
int, tinyint, mediumint, bigint, float, double
date, timestamp(size), datetime, time, year(size)
Ví dụ:
CREATE TABLE salary
(
employeeID INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(100),
salary INT,
PRIMARY KEY(prodID)
);
Xem cấu trúc của cơ sở dữ liệu/bảng
Dùng một trong các lệnh sau:
mysql> DESCRIBE salary;
mysql> DESC salary;
mysql> SHOW FIELDS FROM salary;
mysql> SHOW TABLES;
Thay đổi một bảng có trước
Đổi tên bảng:
mysql> ALTER TABLE salary RENAME site;
Thêm một trường vào trong bảng:
mysql> ALTER TABLE salary ADD COLUMN modified TIMESTAMP;
mysql> ALTER TABLE salary ADD COLUMN modified TIMESTAMP FIRST;
mysql> ALTER TABLE salary ADD COLUMN modified TIMESTAMP AFTER some_field;
Xóa trường ra khỏi bảng:
mysql> ALTER TABLE salary DROP COLUMN modified;
mysql> ALTER TABLE salary DROP INDEX keyIndex;
Thay đổi thuộc tính của trường:
mysql> ALTER TABLE salary CHANGE name aName VARCHAR(255) NOT NULL;
mysql> ALTER TABLE salary CHANGE name name VARCHAR(200) NOT NULL;
Xóa bảng:
mysql> DROP TABLE salary;
Các lệnh SQL cơ bản
Lệnh select
Keyword | Description |
---|---|
SELECT | Retrieves fields from one or more tables. |
FROM | Tables containing the fields. |
WHERE | Criteria to restrict the records returned. |
GROUP BY | Determines how the records should be grouped. |
HAVING | Used with GROUP BY to specify the criteria for the grouped records. |
ORDER BY | Criteria for ordering the records. |
LIMIT | Limit the number of records returned |
Keyword | Description |
---|---|
INSERT | Inserts data into a table |
INTO | Specifies the name of the table to insert the data |
VALUES | The data for the fields in the ta |
Keyword | Description |
---|---|
UPDATE | Updates data in one or more tables. |
SET | Specifies the field names to be updates. If the fields belong to more that one table, the table name should be specified before the field name. (eg. search.Page). |
WHERE | Criteria to restrict the records updated. |
Keyword | Description |
---|---|
DELETE | Deletes the records in one or more tables. |
FROM | Tables containing the records. |
WHERE | Criteria to restrict the records deleted |