SQL Data Definition
DDL
sql DDL to not only define a set of relationships, but also to define the information for each relationship, including:
- Each mode of relationship
- The value of each property type
- Integrity constraints
- Maintenance index for each set of relationship
- Security and permissions for each relationship information
- Each relationship between physical storage structure on disk
basic type
sql support of common types:
- char (n-): fixed length string, n is its length and may be provided, not when the string length n, add a space automatically;
- VARCHAR (n-): variable-length strings, up to n-;
- int : Integer
- smallint : small integer
- numeric (p, d) : fixed point, the total number of p bits there are d bits to the right of the decimal point. EG, numeric (3,1) can store 44.5, 0.32,333.3 not stored;
- Real, Double, Precision : floating-point double-precision floating-point
- a float (n): floating-point precision of at least n bits
The basic schema definition
create table defines the relationship
create table student (s_id varchar(8), s_name varchar(20), dept_ment varchar(20), class varchar(10), dormitory varchar(20), primary key (s_id), foreign key (dept_name) references department);
Defines the relationship between the student, the student containing ID, name, department, class, bedroom. In this relationship, s_id main keys, are indicated by the primary key (s_id);
dept_name is a foreign key, a foreign ket (dept_name) references department noted dept_name and is the primary key of the department;
- Key Primary (A, B, C, ?????): A, B, C ????? is the primary key of a relation, the primary key attribute must be non-null and unique , usually choose a primary key attribute
- Key Foreign (A, B, C ··) References other_table: shows the relationship of A, B, C ··· on attribute values must correspond to a tuple in a main key relationship other_table
- Values, the values do not appear other_table not allowed;
- null not : represent null values are not allowed on the property
insert into the insertion tuple
insert into student values(20229991,'Wang Yang','CS','CS-01','B4-202');
Also can insert multiple rows
insert into student values(20229992,'Ming Wang','Math','Math-02','P2-301'), (20229993,'Zhang Shui','CS','CS-02','B4-411'), (20229993,'Liu Ping','CS','CS-03','B4-518');
Explain here, only numbers and must score at the end of the statement
If you do not remember the order, so you can also insert
insert into student(s_name,s_id,dept_name,dormitory,class) values('Li Mei',20229913,'Finance','G2-117','Fi-09');
Insert tuples on the basis of the query. For example, let each college entrance examination scores of 700 or more students to become excellent students scholarship and fellowship marked 6000
insert into prise select s_id,s_name,dept_name,6000 from studnet where CET_grade=6000;
delete delete tuples
delete from student;
Delete all tuples in relations student, but the student model there;
drop delete tuples
drop table student;
Delete all tuples student, delete student model, unless create table reconstruction student, otherwise you can not insert a tuple to which;
Deleting
delete from r where P;
P deleted tuples satisfying the condition from the relationship between r
/ * Remove s_name from the relationship student as Wang gang tuple * / Delete from student WHERE s_name = ' Wang gang ' ; Student / * Remove s_id at 20229950-20229999 intermediate tuples from the relation student in * / Delete from student WHERE s_id the BETWEEN 20.22995 million and 20,229,999 ; / * delete some students tuples from relations student, they are tied to a teaching office * / the delete from student the WHERE dept_name in ( the SELECT dept_name fromDepartment WHERE Building = ' DSl% ___ ' ); / * Delete all students entrance lower than the average scores of the student tuple * / Delete from Student WHERE CET_grade < ( SELECT Arg (CET_grade) from Student);
Usually delete can delete a specific query with tuples, more freedom
alter table for the existing relationship between the increase (decrease) property
alter table student add grade D;
Grade student attributes to the increase in the relationship, which is the domain D;
alter table student drop grade;
Remove property from the relations student in grade, but this command is not supported in many database systems;