Article Directory
Foreword '· ᴗ · `
- This section talked about the creation of the database table and so modify and delete
- In this part will help you learn ...
- 0 DDL understand why the concept is called the data definition language
- 1 Create Modify Delete Library
- 2 Create Modify Delete table
- 3
the CREATE create
ALTER modify
DROP delete - 4 How to use DQL statement replication table
DDL
DDL data definition language
the name makes sense from a fact table represents a relationship between our real lives (relation), such as who the goddess boyfriend, such as what time of entry relationship with his staff bonus rate Yes. . . These relations turn into concrete realities abstract table will need to define the definition
Closer to home the core content is to create modify delete tables and tables of collections - Libraries
Library Operation
We played at the code to know in order to perform each line:
CREATE DATABASE IF NOT EXISTS db1;
RENAME DATABASE db1 TO db2;
DROP DATABASE db2;
DROP DATABASE IF EXISTS [旧表名];
CREATE DATABASE [新表名];
IF NOT EXISTS good sentence (remember we mentioned in Section 21 EXISTS)
refers to if we do not create a db1
any, we have to consider (Do not get no more data)
The second line is the change of name
The third sentence is deleted
changes to the database in fact, nothing to talk about
general we want to create a new database of the same name and also do not care about past data error not occurred we would write:
DROP DATABASE IF EXISTS [库名];
CREATE DATABASE [库名];
# 安全创造新的库
USE [库名]; # 库内再操作表的问题
DROP TABLE IF EXISTS [表名];
CREATE TABLE [表名];
It is noteworthy that create modify delete the table with exactly the same keywords
Create a table
Of course, do not forget to watch is to have fields (columns) attributes you so Create a table with no arguments really embarrassed
us look at a table is created Zeyang:
CREATE TABLE `departments` (
`department_id` INT(4) NOT NULL AUTO_INCREMENT,
`department_name` VARCHAR(3) DEFAULT NULL,
`manager_id` INT(6) DEFAULT NULL,
`location_id` INT(4) DEFAULT NULL,
)
- The first column of the field attribute name
- The second column indicates the data type will be behind the panellists data type of
- The third column indicates the constraint is also behind the panellists will be
constrained (constrain) is not well understood improper actions such as gender attribute data of the objective world only "male", "female",
added here is to prevent constraints do not fly to happen
serious it XSS attack is sql injection attacks
XSS attacks: the js code to the server database via POST then the database and then return when the js code to the user of the client can be in the browser is running because it is returned by the server data General safety review is not strict like this js code may have high authority on your PC hands and feet :)
So the network security grabbed from the database (funny)
Here we are talking about several common data types give you a feel:
Code | Explanation |
---|---|
int | Plastic |
double float | Float |
char | Fixed length character string |
varchar | Variable-length string |
text | Longer string |
date | date |
time | time |
timestamp | Timestamp |
Wherein the double (5,2) means 5 digit 2 decimal places such as 999.99
and is very similar to other common programming languages such as C js, which do not go into detail
varchar char text where applicable each have their own are relatively common
Then there are basically these constraints:
Code | Explanation | English | Chinese |
---|---|---|---|
DEFAULT NULL | The default value is empty | default | Defaults |
NOT | non- | not | denial |
UNIQUE | The only value | unique | Unparalleled |
AUTO_INCREMENT | The default increment in steps of 1 | auto increase | Automatic growth |
PRIMARY KEY | Primary key | primary key | The main keys |
FOREIGN KEY | Foreign key | foreign key | External 键位 |
note
- These modifiers can be superimposed such as the default value is non-empty:
NOT NULL
(not effect a keyword DEFAULT) - auto increment step size can be changed
SET auto_increment_increment=3
# 3 step
but change the "Environment Variables" step all the tables are the same as the little pit is not recommended - auto increment works: I want to achieve from the beginning has been id 10 111 213 ...
can first insert data INSERT a id = 10 ( on a section taught )
and then later insert other data when id there to fill in NULL without writing 1112 13 ... tables are automatically generated for you
Structure of the table
Our structure is called before the specified property name data type plus constraints
so some people say that modify the "table" is actually a modified structure of the table
so we create table to check how its structure is it desirable?
normal method:
DESC [表名]
In addition to this key constraints as well as our custom key can check out
The question is key to key what is?
Is essentially a row but we selected one or more columns then assign attributes
such as primary key PRIMARY KEY is the value of the only non-empty is
also a foreign key of course is the only value but also linked with other tables
there UNIQUE key is a unique value of
The constraints we will go into that section describes how to define the key , data definition language is defined dry live!
Also called key index (index) is actually a time just looking for something to say when the data put things said by the index key
so you can see there is a command to all of the index (key primary key foreign key and our own definition of key)
SHOW INDEX FROM [表名]
The above two commands can be combined to determine the overall look at the results
Modify table
Modification (the ALTER) is related to the structure on the column properties:
- Add column attributes
to add a table stu classname columns:
the ALTER TABLE stu the ADD (classname VARCHAR (100)); - Modify column attribute name
modified stu table name to the gender column Sex:
the ALTER TABLE stu Sex Change gender CHAR (2); - (Therefore, the same data type is used to modify the constraint column attributes modifications) or modify the data type constraint
modification stu gender column of the table column type is CHAR (2):
the ALTER TABLE stu the MODIFY gender CHAR (2); - Delete Column property
to delete stu table classname columns:
the ALTER TABLE DROP stu classname;
Anyway,
alter table [表名] add|drop|modify|change column [列名] [列类型] [约束]
so be sure to write
Copy the table
- Copy table structure
that the white column data type attribute + + constraint
CREATE TABLE [复制出来的表] LIKE [被复制的表]
- Copy all (+ Data Structure)
CREATE TABLE [复制出来的表] SELECT * FROM [被复制的表];
- Copy part of the data in conjunction with DQL you will do anything
that we check out the tables is a + a copy of the data if it is added to create a memoryCREATE TABLE 【复制出来的表】
that case we can write:
CREATE TABLE [复制出来的表]
SELECT [column]
FROM [table1] AS [table1_name]
[join type] JOIN [table2] AS [table2_name]
ON [join condition]
WHERE [condition]
GROUP BY [grouping fields]
HAVING [grouping condition]
ORDER BY [order fields]
LIMIT [offset] [size]
Cattle breaking, right? :)
DQL DQL out why important because where results can be put to use!
Summary'◡`
In this section we know a bit emotional language DDL next section we look at the special data type specific content:
Next stop: the database learning MySQL (twenty-four) - DDL (b) data types
In addition,
-
Well I want to learn database? You may wish to start from MySQL
MySQL column -
python so fire you want to play with python-depth study of a simple application thing? I can see the column continues to update oh:
Python application -
Child before doing all adults choose to be! Backend interested? Accept it now :)
hands-on learning with your back-end (server) -
Thank you, big brother support! Meng new courtesy :)